Geek Slack

Learn C++
    About Lesson


    C++ Object-Oriented Programming (OOP)


    C++ Object-Oriented Programming (OOP)

    1. Classes and Objects

    Example of defining a class and creating objects:

    #include <iostream>
    #include <string>
    
    // Class declaration
    class Car {
    private:
        std::string brand;
        int year;
    
    public:
        // Constructor
        Car(std::string b, int y) {
            brand = b;
            year = y;
        }
    
        // Member function
        void displayInfo() {
            std::cout << "Brand: " << brand << ", Year: " << year << std::endl;
        }
    };
    
    int main() {
        // Create objects of class Car
        Car car1("Toyota", 2020);
        Car car2("BMW", 2022);
    
        // Call member function on objects
        car1.displayInfo();
        car2.displayInfo();
    
        return 0;
    }

    2. Inheritance

    Example of inheritance in classes:

    #include <iostream>
    #include <string>
    
    // Base class
    class Animal {
    protected:
        std::string type;
    
    public:
        Animal(std::string t) : type(t) {}
    
        void sound() {
            std::cout << "Animal sound" << std::endl;
        }
    };
    
    // Derived class
    class Dog : public Animal {
    public:
        Dog() : Animal("Dog") {}
    
        void bark() {
            std::cout << "Woof!" << std::endl;
        }
    };
    
    int main() {
        Dog myDog;
    
        // Access base class member function
        myDog.sound();
    
        // Access derived class member function
        myDog.bark();
    
        return 0;
    }

    3. Polymorphism

    Example of polymorphism using virtual functions:

    #include <iostream>
    
    // Base class
    class Shape {
    public:
        virtual void draw() {
            std::cout << "Drawing shape" << std::endl;
        }
    };
    
    // Derived class
    class Circle : public Shape {
    public:
        void draw() override {
            std::cout << "Drawing circle" << std::endl;
        }
    };
    
    int main() {
        Shape *shapePtr;
        Circle circleObj;
    
        // Pointing to derived class object
        shapePtr = &circleObj;
    
        // Accessing overridden function using base class pointer
        shapePtr->draw();
    
        return 0;
    }

    4. Encapsulation

    Example of encapsulation using private and public access specifiers:

    #include <iostream>
    #include <string>
    
    // Class declaration
    class Employee {
    private:
        std::string name;
        double salary;
    
    public:
        // Constructor
        Employee(std::string n, double s) : name(n), salary(s) {}
    
        // Public member function to display employee details
        void displayInfo() {
            std::cout << "Name: " << name << ", Salary: $" << salary << std::endl;
        }
    
        // Public member function to update salary
        void updateSalary(double newSalary) {
            salary = newSalary;
        }
    };
    
    int main() {
        // Create object of class Employee
        Employee emp("John Doe", 50000);
    
        // Access and modify private member using public functions
        emp.displayInfo();
        emp.updateSalary(60000);
        emp.displayInfo();
    
        return 0;
    }

    Conclusion

    Object-Oriented Programming (OOP) in C++ provides a powerful paradigm for designing and organizing code into reusable, modular components. This chapter covered fundamental OOP concepts including classes, objects, inheritance, polymorphism, and encapsulation. Understanding these concepts allows you to create efficient, maintainable, and scalable C++ programs by leveraging abstraction, inheritance, and polymorphism.