


Khusboo Tayal
OOP is a programming paradigm that organizes code into objects, each representing a real-world entity or concept. It provides a structured approach to code organization and promotes code reusability.
A class is a blueprint for creating objects. It defines properties (data members) and methods (member functions) that the objects will have.
class ClassName {
// Access Specifiers (public, private, protected)
data_members;
member_functions;
};
#include <iostream>
using namespace std;
// Defining a class
class Car {
private:
string brand;
int speed;
public:
// Method to set car details
void setDetails(string b, int s) {
brand = b;
speed = s;
}
// Method to display car details
void displayDetails() {
cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << endl;
}
};
// Main function
int main() {
Car car1;
car1.setDetails("Tesla", 200);
car1.displayDetails();
return 0;
}
Brand: Tesla, Speed: 200 km/h
Access specifiers define the accessibility of data members and member functions of a class.
#include <iostream>
using namespace std;
class Example {
private:
int privateVar;
public:
int publicVar;
protected:
int protectedVar;
};
int main() {
Example obj;
obj.publicVar = 10; // Accessible
// obj.privateVar = 20; // Error: Not accessible
// obj.protectedVar = 30; // Error: Not accessible
return 0;
}
A constructor is a special member function that is automatically called when an object of a class is created. It initializes the object.
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
public:
// Default Constructor
Student() {
name = "Unknown";
age = 0;
}
// Parameterized Constructor
Student(string n, int a) {
name = n;
age = a;
}
// Copy Constructor
Student(const Student &s) {
name = s.name;
age = s.age;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Main function
int main() {
Student s1; // Default constructor
Student s2("Alice", 21); // Parameterized constructor
Student s3 = s2; // Copy constructor
s1.display();
s2.display();
s3.display();
return 0;
}
Name: Unknown, Age: 0 Name: Alice, Age: 21 Name: Alice, Age: 21
A destructor is a special member function that is automatically called when an object is destroyed. It is used to clean up resources.
#include <iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Constructor called." << endl;
}
~Demo() {
cout << "Destructor called." << endl;
}
};
int main() {
Demo obj;
return 0;
}
Constructor called. Destructor called.
Inheritance is a feature of OOP that allows you to create a new class (derived class) from an existing class (base class), inheriting its properties and methods.
#include <iostream>
using namespace std;
// ================== Single Inheritance ==================
class SingleBase {
public:
void showSingle() {
cout << "Single Inheritance: Base Class Method." << endl;
}
};
// Derived class (Single Inheritance)
class SingleDerived : public SingleBase {
public:
void showDerived() {
cout << "Single Inheritance: Derived Class Method." << endl;
}
};
// ================== Multiple Inheritance ==================
class MultipleBase1 {
public:
void showBase1() {
cout << "Multiple Inheritance: Base1 Method." << endl;
}
};
class MultipleBase2 {
public:
void showBase2() {
cout << "Multiple Inheritance: Base2 Method." << endl;
}
};
// Derived class (Multiple Inheritance)
class MultipleDerived : public MultipleBase1, public MultipleBase2 {
public:
void showMultipleDerived() {
cout << "Multiple Inheritance: Derived Method." << endl;
}
};
// ================== Multilevel Inheritance ==================
class MultilevelBase {
public:
void showMultilevelBase() {
cout << "Multilevel Inheritance: Base Class Method." << endl;
}
};
// Derived from MultilevelBase
class Intermediate : public MultilevelBase {
public:
void showIntermediate() {
cout << "Multilevel Inheritance: Intermediate Class Method." << endl;
}
};
// Further Derived from Intermediate
class MultilevelDerived : public Intermediate {
public:
void showMultilevelDerived() {
cout << "Multilevel Inheritance: Derived Class Method." << endl;
}
};
// ================== Hierarchical Inheritance ==================
class HierarchicalBase {
public:
void showHierarchicalBase() {
cout << "Hierarchical Inheritance: Base Class Method." << endl;
}
};
// First Derived Class from Base (Hierarchical)
class HierarchicalDerived1 : public HierarchicalBase {
public:
void showDerived1() {
cout << "Hierarchical Inheritance: Derived1 Class Method." << endl;
}
};
// Second Derived Class from Base (Hierarchical)
class HierarchicalDerived2 : public HierarchicalBase {
public:
void showDerived2() {
cout << "Hierarchical Inheritance: Derived2 Class Method." << endl;
}
};
// ================== Hybrid Inheritance ==================
class HybridBase1 {
public:
void showHybridBase1() {
cout << "Hybrid Inheritance: Base1 Method." << endl;
}
};
class HybridBase2 {
public:
void showHybridBase2() {
cout << "Hybrid Inheritance: Base2 Method." << endl;
}
};
// Hybrid Inheritance using Multiple and Hierarchical
class HybridDerived1 : public HybridBase1 {
public:
void showHybridDerived1() {
cout << "Hybrid Inheritance: Derived1 Method." << endl;
}
};
// Derived from HybridBase2 and HybridDerived1
class HybridDerived2 : public HybridBase2, public HybridDerived1 {
public:
void showHybridDerived2() {
cout << "Hybrid Inheritance: Final Derived Method." << endl;
}
};
// ================== Main Function ==================
int main() {
// Single Inheritance
cout << "=== Single Inheritance ===" << endl;
SingleDerived single;
single.showSingle();
single.showDerived();
// Multiple Inheritance
cout << "\n=== Multiple Inheritance ===" << endl;
MultipleDerived multiple;
multiple.showBase1();
multiple.showBase2();
multiple.showMultipleDerived();
// Multilevel Inheritance
cout << "\n=== Multilevel Inheritance ===" << endl;
MultilevelDerived multilevel;
multilevel.showMultilevelBase();
multilevel.showIntermediate();
multilevel.showMultilevelDerived();
// Hierarchical Inheritance
cout << "\n=== Hierarchical Inheritance ===" << endl;
HierarchicalDerived1 hier1;
HierarchicalDerived2 hier2;
hier1.showHierarchicalBase();
hier1.showDerived1();
hier2.showHierarchicalBase();
hier2.showDerived2();
// Hybrid Inheritance
cout << "\n=== Hybrid Inheritance ===" << endl;
HybridDerived2 hybrid;
hybrid.showHybridBase1();
hybrid.showHybridBase2();
hybrid.showHybridDerived1();
hybrid.showHybridDerived2();
return 0;
}
=== Single Inheritance === Single Inheritance: Base Class Method. Single Inheritance: Derived Class Method. === Multiple Inheritance === Multiple Inheritance: Base1 Method. Multiple Inheritance: Base2 Method. Multiple Inheritance: Derived Method. === Multilevel Inheritance === Multilevel Inheritance: Base Class Method. Multilevel Inheritance: Intermediate Class Method. Multilevel Inheritance: Derived Class Method. === Hierarchical Inheritance === Hierarchical Inheritance: Base Class Method. Hierarchical Inheritance: Derived1 Class Method. Hierarchical Inheritance: Base Class Method. Hierarchical Inheritance: Derived2 Class Method. === Hybrid Inheritance === Hybrid Inheritance: Base1 Method. Hybrid Inheritance: Base2 Method. Hybrid Inheritance: Derived1 Method. Hybrid Inheritance: Final Derived Method.
Polymorphism means "many forms". It allows you to perform a single action in different ways.
#include <iostream>
using namespace std;
// Compile-time Polymorphism: Function Overloading
class Calculator {
public:
// Overloaded add() function for integers
int add(int a, int b) {
return a + b;
}
// Overloaded add() function for doubles
double add(double a, double b) {
return a + b;
}
};
// Run-time Polymorphism: Virtual Functions
class Animal {
public:
// Virtual function for sound (will be overridden)
virtual void sound() {
cout << "Animal makes a sound." << endl;
}
};
// Derived class: Dog
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks." << endl;
}
};
// Derived class: Cat
class Cat : public Animal {
public:
void sound() override {
cout << "Cat meows." << endl;
}
};
int main() {
// Compile-time Polymorphism
Calculator calc;
cout << "Compile-time Polymorphism (Function Overloading):" << endl;
cout << "Sum of 2 and 3: " << calc.add(2, 3) << endl;
cout << "Sum of 2.5 and 3.5: " << calc.add(2.5, 3.5) << endl;
cout << endl;
// Run-time Polymorphism
cout << "Run-time Polymorphism (Virtual Functions):" << endl;
Animal *animalPtr;
Dog dog;
Cat cat;
animalPtr = &dog;
animalPtr->sound(); // Calls Dog's sound()
animalPtr = &cat;
animalPtr->sound(); // Calls Cat's sound()
return 0;
}
Compile-time Polymorphism (Function Overloading): Sum of 2 and 3: 5 Sum of 2.5 and 3.5: 6 Run-time Polymorphism (Virtual Functions): Dog barks. Cat meows.
At run-time, the correct version of sound() is called based on the object type (Dog or Cat).
Abstraction hides complex implementation details and shows only essential information.
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function (abstract)
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing Circle" << endl;
}
};
int main() {
Circle c;
c.draw();
return 0;
}
Drawing Circle
In this tutorial, you learned: