Introduction to C++ Programming

Section 4: Introduction to C++ Programming

Lesson 1: Object-Oriented Concepts in C++

1.1 Classes, Objects, and Inheritance

C++ is an object-oriented programming (OOP) language that extends the capabilities of C. Key OOP concepts include classes, objects, and inheritance.

Example (Class and Object): 

#include <iostream>


// Class declaration

class Car {

public:

    // Member variables

    std::string brand;

    int year;


    // Member function (method)

    void displayInfo() {

        std::cout << "Brand: " << brand << ", Year: " << year << std::endl;

    }

};


int main() {

    // Object creation

    Car myCar;


    // Accessing members and invoking methods

    myCar.brand = "Toyota";

    myCar.year = 2022;

    myCar.displayInfo();


    return 0;

}


1.2 Encapsulation and Polymorphism

Encapsulation involves bundling data (attributes) and methods that operate on the data within a single unit (class). Polymorphism allows objects to be treated as instances of their base class.

Example (Encapsulation and Polymorphism): 

#include <iostream>


// Base class (Shape)

class Shape {

public:

    // Virtual function for polymorphism

    virtual void draw() {

        std::cout << "Drawing a shape." << std::endl;

    }

};


// Derived class (Circle)

class Circle : public Shape {

public:

    void draw() override {

        std::cout << "Drawing a circle." << std::endl;

    }

};


int main() {

    // Polymorphism: Shape pointer pointing to a Circle object

    Shape* shapePtr = new Circle();

    shapePtr->draw();


    // Cleanup

    delete shapePtr;


    return 0;

}


Lesson 2: C++ Standard Template Library (STL)

2.1 Overview of Containers and Algorithms

The C++ Standard Template Library (STL) provides generic classes and functions for handling various data structures and algorithms.

Example (STL Overview): 

#include <iostream>

#include <vector>

#include <algorithm>


int main() {

    // STL container: vector

    std::vector<int> numbers = {5, 2, 8, 1, 3};


    // STL algorithm: sort

    std::sort(numbers.begin(), numbers.end());


    // Displaying sorted elements

    for (int num : numbers) {

        std::cout << num << " ";

    }


    return 0;

}


2.2 Using Vectors, Lists, and Maps

STL containers like vectors, lists, and maps offer dynamic arrays, linked lists, and associative arrays, respectively.

Example (Using Vectors, Lists, and Maps): 

#include <iostream>

#include <vector>

#include <list>

#include <map>


int main() {

    // STL vector

    std::vector<int> vectorNumbers = {1, 2, 3};


    // STL list

    std::list<std::string> stringList = {"apple", "banana", "orange"};


    // STL map

    std::map<std::string, int> ages = {

        {"Alice", 25},

        {"Bob", 30},

        {"Charlie", 22}

    };


    // Accessing elements in containers

    std::cout << "Vector Element: " << vectorNumbers[1] << std::endl;

    std::cout << "List Element: " << *std::next(stringList.begin(), 2) << std::endl;

    std::cout << "Age of Bob: " << ages["Bob"] << std::endl;


    return 0;

}

Understanding the object-oriented features of C++ and leveraging the C++ Standard Template Library (STL) is essential for building efficient and modular C++ programs. Practice creating classes, working with inheritance, and using STL containers and algorithms.