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.
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 .
.
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 ~
.
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.
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()
.
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; }
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.
private
, public
, protected
.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!