inheritance
inheritance
Shape
FIRST HIERARCHY
2D 3D
C++ Inheritance
In C++, inheritance is a process in which one object acquires all the properties and behaviors of its
parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors
which are defined in other class.
In C++, the class which inherits the members of another class is called derived class and the class whose
members are inherited is called base class. The derived class is the specialized class for the base class.
Code reusability: Now you can reuse the members of your parent class. So, there is no need to define
the member again. So less code is required in the class.
Transitivity property.
Types Of Inheritance
o Single inheritance
o Multiple inheritance
o Hierarchical inheritance
o Multilevel inheritance
o Hybrid inheritance
Derived Classes
A Derived class is defined as the class derived from the base class.
2. {
4. }
Where,
visibility mode: The visibility mode specifies whether the features of the base class are publicly inherited
or privately inherited. It can be public or private.
o When the base class is privately inherited by the derived class, public members of the base class
becomes the private members of the derived class. Therefore, the public members of the base
class are not accessible by the objects of the derived class only by the member functions of the
derived class.
o When the base class is publicly inherited by the derived class, public members of the base class
also become the public members of the derived class. Therefore, the public members of the
base class are accessible by the objects of the derived class as well as by the member functions
of the base class.
Note:
Single inheritance is defined as the inheritance in which a derived class is inherited from the only one
base class.
Where 'A' is the base class, and 'B' is the derived class.
1. #include <iostream>
3. class Account {
4. public:
6. };
8. public:
10. };
15. return 0;
16. }
Output:
Salary: 60000
Bonus: 5000
1. #include <iostream>
3. class Animal {
4. public:
5. void eat() {
6. cout<<"Eating..."<<endl;
7. }
8. };
10. {
11. public:
13. cout<<"Barking...";
14. }
15. };
18. d1.eat();
19. d1.bark();
20. return 0;
21. }
Output:
Eating...
Barking...
1. #include <iostream>
3. class A
4. {
5. int a = 4;
6. int b = 5;
7. public:
8. int mul()
9. {
10. int c = a*b;
11. return c;
12. }
13. };
15. {
16. public:
18. {
21. }
22. };
24. {
25. B b;
26. b.display();
27. return 0;
28. }
Output:
Multiplication of a and b is : 20
The private member is not inheritable. If we modify the visibility mode by making it public, but this takes
away the advantage of data hiding.
C++ introduces a third visibility modifier, i.e., protected. The member which is declared as protected will
be accessible to all the member functions within the class as well as the class immediately derived from
it.
o Private: When the member is declared as private, it is accessible within the class only.
o Protected: When the member is declared as protected, it is accessible within its own class as
well as the class immediately derived from it.
When one class inherits another class which is further inherited by another class, it is known as multi
level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all
its base classes.
1. #include <iostream>
3. class Animal {
4. public:
5. void eat() {
6. cout<<"Eating..."<<endl;
7. }
8. };
10. {
11. public:
13. cout<<"Barking..."<<endl;
14. }
15. };
17. {
18. public:
20. cout<<"Weeping...";
21. }
22. };
25. d1.eat();
26. d1.bark();
27. d1.weep();
28. return 0;
29. }
Output:
Eating...
Barking...
Weeping...
Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more
classes.
2. {
4. }
#include <iostream>
class A
protected:
int a;
public:
void get_a(int n)
a = n;
};
class B
protected:
int b;
public:
void get_b(int n)
b = n;
};
class C : public A,public B
public:
void display()
};
int main()
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
Output:
The value of a is : 10
The value of b is : 20
Addition of a and b is : 30
In the above example, class 'C' inherits two base classes 'A' and 'B' in a public mode.
Ambiguity can be occurred in using the multiple inheritance when a function with the same name occurs
in more than one base class.
#include <iostream>
using namespace std;
class A
public:
void display()
};
class B
public:
void display()
};
void view()
display();
};
int main()
C c;
c.display();
return 0;
Output:
display();
o The above issue can be resolved by using the class resolution operator with the function. In the
above example, the derived class code can be rewritten as:
2. {
3. void view()
4. {
7.
8. }
9. };
1. class A
2. {
3. public:
4. void display()
5. {
6. cout<<?Class A?;
7. }
8. } ;
9. class B
10. {
11. public:
13. {
15. }
16. } ;
In the above case, the function of the derived class overrides the method of the base class. Therefore,
call to the display() function will simply call the function defined in the derived class. If we want to
invoke the base class function, we can use the class resolution operator.
1. int main()
2. {
3. B b;
6. }
#include <iostream>
class A
protected:
int a;
public:
void get_a()
cin>>a;
};
class B : public A
protected:
int b;
public:
void get_b()
cin>>b;
};
class C
protected:
int c;
public:
void get_c()
cin>>c;
};
protected:
int d;
public:
void mul()
get_a();
get_b();
get_c();
};
int main()
D d;
d.mul();
return 0;
Output:
Hierarchical inheritance is defined as the process of deriving more than one class from a base class.
class B : public A
// body of class B.
class C : public A
// body of class C.
class D : public A
// body of class D.
#include <iostream>
public:
int a;
int b;
{
a= n;
b = m;
};
public:
int rect_area()
return result;
};
public:
int triangle_area()
return result;
};
int main()
Rectangle r;
Triangle t;
int length,breadth,base,height;
std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
std::cout << "Enter the base and height of the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
return 0;
Output: