Inheritance int main() {
Inheritance is the property that allows the reuse of an Derived obj;
existing class to build a new class. obj.displayBase1(); // Calling function from Base1
Syntax: obj.displayBase2(); // Calling function from Base2
class BaseClass { obj.show(); // Calling derived class function
// Base class members return 0;
}; }
Multi-level Inheritance
class DerivedClass : access_specifier BaseClass { A class inherits from another derived class.
// Derived class members Syntax:
}; #include <iostream>
Single Inheritance using namespace std;
A derived class inherits from only one base class. // Base class
Syntax: class Base {
#include <iostream> public:
using namespace std; void display() {
// Base class cout << "Base class display function." << endl;
class Base { }
public: };
void display() { // Intermediate class
cout << "Base class display function." << endl; class Intermediate : public Base {
} public:
}; void show() {
// Derived class cout << "Intermediate class show function." <<endl;
class Derived : public Base { }
public: };
void show() { // Derived class
cout << "Derived class show function." << endl; class Derived : public Intermediate {
} public:
}; void print() {
int main() { cout << "Derived class print function." << endl;
Derived obj; }
obj.display(); // Calling base class function };
obj.show(); // Calling derived class function int main() {
return 0; Derived obj;
} obj.display(); // Calling base class function
Multiple Inheritance obj.show(); // Calling intermediate class function
A derived class inherits from more than one base class. obj.print(); // Calling derived class function
Syntax: return 0;
#include <iostream> }
using namespace std; Hierarchical Inheritance
// Base class 1 Multiple derived classes inherit from a single base class.
class Base1 { Syntax:
public: #include <iostream>
void displayBase1() { using namespace std;
cout << "Base1 class display function." << endl; // Base class
} class Base {
}; public:
// Base class 2 void display() {
class Base2 { cout << "Base class display function." << endl;
public: }
void displayBase2() { };
cout << "Base2 class display function." << endl; // Derived class 1
} class Derived1 : public Base {
}; public:
// Derived class void show() {
class Derived : public Base1, public Base2 { cout << "Derived1 class show function." << endl;
public: }
void show() { };
cout << "Derived class show function." << endl; // Derived class 2
} class Derived2 : public Base {
}; public:
void print() { Inheritance and Constructors
cout << "Derived2 class print function." << endl; When a derived class object is created, the base class
} constructor is called first to ensure proper initialization.
}; #include <iostream>
int main() { using namespace std;
Derived1 obj1; // Parent class definition
class Parent {
Derived2 obj2;
public:
obj1.display(); // Calling base class function
// Constructor of Parent class
obj1.show(); // Calling derived class 1 function Parent() {
obj2.display(); // Calling base class function cout << "Parent Constructor" << endl; // This will
obj2.print(); // Calling derived class 2 function be printed when the Parent class is instantiated
return 0; }
} };
Multipath Inheritance // Child class definition that inherits from Parent
A derived class inherits from two or more classes that class Child : public Parent {
have a common base class. public:
#include <iostream> // Constructor of Child class
using namespace std; Child() {
// Base class cout << "Child Constructor" << endl; // This will be
class Base { printed when the Child class is instantiated
public: }
void display() { };
cout << "Base class display function." << endl; // Main function
} int main() {
}; Child obj; // Create an instance of the Child class
// Derived class 1 // Output: Parent Constructor, Child Constructor
class Derived1 : public Base { return 0;
public: }
void show() {
cout << "Derived1 class show function." << endl;
}
};
// Derived class 2
class Derived2 : public Base {
public:
void print() {
cout << "Derived2 class print function." << endl;
}
};
// Derived class 3
class Derived3 : public Derived1, public Derived2 {
public:
void output() {
cout << "Derived3 class output function." << endl;
}
};
int main() {
Derived3 obj;
obj.Derived1::display(); //Accessing base class
function through Derived1
obj.Derived2::display(); //Accessing base class
function through Derived2
obj.output(); // Calling derived class 3 function
return 0;
}