0% found this document useful (0 votes)
5 views12 pages

C++ Inheritance Types Explained

Inheritance in C++ is an OOP feature that allows a derived class to acquire properties and behaviors from a base class, promoting code reusability. There are several types of inheritance including single, multiple, multilevel, hierarchical, and hybrid, each with its own structure and example implementations. Access specifiers (public, protected, private) determine the visibility of base class members in derived classes, affecting how they can be accessed.

Uploaded by

ns6966757
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

C++ Inheritance Types Explained

Inheritance in C++ is an OOP feature that allows a derived class to acquire properties and behaviors from a base class, promoting code reusability. There are several types of inheritance including single, multiple, multilevel, hierarchical, and hybrid, each with its own structure and example implementations. Access specifiers (public, protected, private) determine the visibility of base class members in derived classes, affecting how they can be accessed.

Uploaded by

ns6966757
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Inheritance in C++

Definition:

Inheritance is an object-oriented programming (OOP) feature that allows a class (derived


class) to acquire the properties and behaviors (data members and member functions) of
another class (base class). It promotes code reusability and hierarchical classification.

Types of Inheritance:

1.​ Single Inheritance​


A derived class inherits from only one base class.

Flow Chart:

Base Class → Derived Class

Program:

#include <iostream>

using namespace std;

class Animal {

public:

void eat() {

cout << "Eating..." << endl;

};

class Dog : public Animal {

public:

void bark() {

cout << "Barking..." << endl;

}
};

int main() {

Dog d;

[Link]();

[Link]();

return 0;

2. Multiple Inheritance​
A derived class inherits from more than one base class.

Flow Chart:

Base1 →

→ Derived Class

Base2 →

Program:

#include <iostream>

using namespace std;

class A {

public:

void displayA() {

cout << "Class A" << endl;

};
class B {

public:

void displayB() {

cout << "Class B" << endl;

};

class C : public A, public B {

public:

void displayC() {

cout << "Class C" << endl;

};

int main() {

C obj;

[Link]();

[Link]();

[Link]();

return 0;

3. Multilevel Inheritance​
​ A derived class is created from another derived class.

Flow Chart:

Base → Intermediate → Derived


Program:

#include <iostream>

using namespace std;

class A {

public:

void showA() {

cout << "Class A" << endl;

};

class B : public A {

public:

void showB() {

cout << "Class B" << endl;

};

class C : public B {

public:

void showC() {

cout << "Class C" << endl;

};

int main() {
C obj;

[Link]();

[Link]();

[Link]();

return 0;

4. Hierarchical Inheritance​
​ Multiple derived classes inherit from a single base class.

Flow Chart:

​ ​ → Derived1

Base →

→ Derived2

​ #include <iostream>

using namespace std;

class Parent {

public:

void property() {

cout << "Parent Property" << endl;

};

class Son : public Parent {

public:

void car() {
cout << "Son has a car" << endl;

};

class Daughter : public Parent {

public:

void bike() {

cout << "Daughter has a bike" << endl;

};

int main() {

Son s;

Daughter d;

[Link]();

[Link]();

[Link]();

[Link]();

return 0;

5. Hybrid Inheritance​
​ Combination of two or more types of inheritance (e.g., multiple + multilevel).

Flow Chart:

Base1 →​ → Derived1​ ​

→ Final Derived

Base2 →
Program:

#include <iostream>

using namespace std;

class A {

public:

void showA() {

cout << "Class A" << endl;

};

class B : public A {

public:

void showB() {

cout << "Class B" << endl;

};

class C {

public:

void showC() {

cout << "Class C" << endl;

};

class D : public B, public C {


public:

void showD() {

cout << "Class D" << endl;

};

int main() {

D obj;

[Link]();

[Link]();

[Link]();

[Link]();

return 0;

Accessibility of Base Class Members in Derived Class:

Access Specifier in Base Public Protected Private


Class Inheritance Inheritance Inheritance

Public Public Protected Private

Protected Protected Protected Private

Private Not Inherited Not Inherited Not Inherited


Examples: Accessibility with Different Inheritance Modes

1. Public Inheritance

#include <iostream>

using namespace std;

class Base {

public:

int a;

protected:

int b;

private:

int c;

};

class DerivedPublic : public Base {

public:

void show() {

a = 10; // Accessible (public)

b = 20; // Accessible (protected)

cout << "a = " << a << ", b = " << b << endl;

};

int main() {

DerivedPublic obj;

[Link]();
// obj.a = 30; // Accessible (public)

// obj.b = 40; // Not accessible (protected)

2. Protected Inheritance

#include <iostream>

using namespace std;

class Base {

public:

int a;

protected:

int b;

};

class DerivedProtected : protected Base {

public:

void display() {

a = 10; // Accessible as protected

b = 20; // Accessible as protected

cout << "a = " << a << ", b = " << b << endl;

};

int main() {

DerivedProtected obj;
[Link]();

// obj.a = 30; // Not accessible (protected)

3. Private Inheritance

#include <iostream>

using namespace std;

class Base {

public:

int a;

protected:

int b;

};

class DerivedPrivate : private Base {

public:

void show() {

a = 10; // Accessible but becomes private in derived

b = 20; // Accessible but becomes private in derived

cout << "a = " << a << ", b = " << b << endl;

};

int main() {

DerivedPrivate obj;
[Link]();

// obj.a = 30; // Not accessible (private)

●​ Inheritance promotes code reuse and modularity.


●​ Access specifiers define how members are visible in derived classes.
●​ Accessibility depends on the inheritance type (public, protected, or private).

​ ​ ​ ​

You might also like