CPP Tutorial



C++ OOP


Object-Oriented Programming (OOP) in C++

OOP is a programming paradigm that uses objects and classes to model real-world entities. C++ supports OOP which helps organize code for better modularity, reusability, and scalability.


1. Basic Concepts of OOP

  • Class: Blueprint or template for creating objects.
  • Object: Instance of a class.
  • Encapsulation: Bundling data (variables) and methods (functions) inside a class.
  • Inheritance: Creating new classes from existing ones to promote code reuse.
  • Polymorphism: Ability to take many forms (mainly through function overloading and overriding).
  • Abstraction: Hiding complex implementation details and showing only the necessary parts.

2. Defining a Class and Creating Objects

Let's create a simple Car class with attributes and functions:

class Car {
public:
    string brand;
    string model;
    int year;

    void displayInfo() {
        cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << endl;
    }
};

int main() {
    Car myCar;
    myCar.brand = "Toyota";
    myCar.model = "Corolla";
    myCar.year = 2022;

    myCar.displayInfo();

    return 0;
}
  

Here, Car is a class, and myCar is an object. We access members using the dot operator ..


3. Constructor and Destructor

Constructors initialize objects automatically when created. Destructors clean up when objects are destroyed.

class Car {
public:
    string brand;
    int year;

    // Constructor
    Car(string b, int y) {
        brand = b;
        year = y;
    }

    // Destructor
    ~Car() {
        cout << "Destructor called for " << brand << endl;
    }

    void display() {
        cout << brand << " - " << year << endl;
    }
};

int main() {
    Car car1("Honda", 2021);
    car1.display();
    return 0;
}
  

Notice the constructor has the same name as the class and no return type. The destructor is preceded by ~.


4. Encapsulation and Access Specifiers

Use private, public, and protected to control access to class members.

class BankAccount {
private:
    double balance;

public:
    void setBalance(double amount) {
        if (amount >= 0) balance = amount;
        else cout << "Invalid balance!" << endl;
    }

    double getBalance() {
        return balance;
    }
};

int main() {
    BankAccount acc;
    acc.setBalance(5000.75);
    cout << "Balance: $" << acc.getBalance() << endl;

    return 0;
}
  

balance is private and can only be accessed through public methods.


5. Inheritance

Inheritance allows a class (derived class) to inherit properties and behavior from another class (base class).

class Vehicle {
public:
    void start() {
        cout << "Vehicle started" << endl;
    }
};

class Car : public Vehicle {
public:
    void honk() {
        cout << "Car honking!" << endl;
    }
};

int main() {
    Car myCar;
    myCar.start();  // Inherited method
    myCar.honk();   // Own method
    return 0;
}
  

Car inherits Vehicle’s method start().


6. Polymorphism

Polymorphism means β€œmany forms.” In C++, this is mainly done by function overloading and overriding.

Function Overloading (Compile-time Polymorphism)
class Print {
public:
    void show(int i) {
        cout << "Integer: " << i << endl;
    }
    void show(double f) {
        cout << "Float: " << f << endl;
    }
};

int main() {
    Print p;
    p.show(5);    // Calls show(int)
    p.show(3.14); // Calls show(double)
    return 0;
}
  
Function Overriding (Runtime Polymorphism)
class Base {
public:
    virtual void show() {
        cout << "Base class show()" << endl;
    }
};

class Derived : public Base {
public:
    void show() override {
        cout << "Derived class show()" << endl;
    }
};

int main() {
    Base* ptr;
    Derived d;
    ptr = &d;

    ptr->show();  // Calls Derived's show() due to virtual function
    return 0;
}
  

7. Abstraction

Abstraction hides implementation details. Abstract classes contain at least one pure virtual function and cannot be instantiated.

class Shape {
public:
    virtual void draw() = 0;  // Pure virtual function
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing Circle" << endl;
    }
};

int main() {
    Circle c;
    c.draw();
    return 0;
}
  

You cannot create an object of Shape, but you can use pointers to it for polymorphism.


Summary

  • Class: Template for creating objects.
  • Object: Instance of a class.
  • Constructor & Destructor: Special functions to initialize and clean up.
  • Encapsulation: Control access with private, public, protected.
  • Inheritance: Derive classes to reuse code.
  • Polymorphism: Same function name, different behaviors.
  • Abstraction: Hide complexity via abstract classes.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review