Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Inheritance

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Inheritance in C++

The capability of a class to derive properties and characteristics


from another class is called Inheritance. Inheritance is one of the
most important features of Object-Oriented Programming.
the derived class inherits all the properties of the base class,
without changing the properties of base class and may add new
features to its own.
 Sub Class: The class that inherits properties from another class is
called Subclass or Derived Class.
 Super Class: The class whose properties are inherited by a
subclass is called Base Class or Superclass.

The Syntax of Derived class:

1. class derived_class_name : visibility-mode base_class_name


2. {
3. // body of the derived class.
4. }
derived_class_name: It is the name of the derived class.

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.

base_class_name: It is the name of the base class.

(Data members of base class )

Visibility Private Protected Public


modifiers
Private No Private Private
inheritance
Protected No Protected Protected
Inheritance
Public No Protected Public
Inheritance

1. Public Mode: If we derive a subclass from a public base class.


Then the public member of the base class will become public in
the derived class and protected members of the base class will
become protected in the derived class.
2. Protected Mode: If we derive a subclass from a Protected base
class. Then both public members and protected members of the
base class will become protected in the derived class.
3. Private Mode: If we derive a subclass from a Private base class.
Then both public members and protected members of the base
class will become Private in the derived class.
Note:
o In C++, the default mode of visibility is private.

o The private members of the base class are never inherited.

Advantage of C++ Inheritance


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

Types Of Inheritance
C++ supports five types of inheritance:

o Single inheritance
o Multiple inheritance
o Hierarchical inheritance
o Multilevel inheritance
o Hybrid inheritance
C++ Single Inheritance

Single inheritance is defined as the inheritance in which a derived


class is inherited from the only one base class.
1. #include <iostream>
2. using namespace std;
3. class Animal {
4. public:
5. void eat() {
6. cout<<"Eating..."<<endl;
7. }
8. };
9. class Dog: public Animal
10. {
11. public:
12. void bark(){
13. cout<<"Barking...";
14. }
15. };
16. int main(void) {
17. Dog d1;
18. d1.eat();
19. d1.bark();
20. return 0;
21. }

C++ Multilevel Inheritance


Multilevel inheritance is a process of deriving a class from another
derived class.
1. #include <iostream>
2. using namespace std;
3. class Animal {
4. public:
5. void eat() {
6. cout<<"Eating..."<<endl;
7. }
8. };
9. class Dog: public Animal
10. {
11. public:
12. void bark(){
13. cout<<"Barking..."<<endl;
14. }
15. };
16. class BabyDog: public Dog
17. {
18. public:
19. void weep() {
20. cout<<"Weeping...";
21. }
22. };
23. int main(void) {
24. BabyDog d1;
25. d1.eat();
26. d1.bark();
27. d1.weep();
28. return 0;
29. }

Output:

Eating...
Barking...
Weeping...
C++ Multiple Inheritance
Multiple inheritance is the process of deriving a new class that
inherits the attributes from two or more classes.
Syntax of the Derived class:

1. class D : visibility B-1, visibility B-2, ?


2. {
3.
4. }

simple example of multiple inheritance.

1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. protected:
6. int a;
7. public:
8. void get_a(int n)
9. {
10. a = n;
11. }
12. };
13.
14. class B
15. {
16. protected:
17. int b;
18. public:
19. void get_b(int n)
20. {
21. b = n;
22. }
23. };
24. class C : public A,public B
25. {
26. public:
27. void display()
28. {
29. std::cout << "The value of a is : " <<a<< std::endl;
30. std::cout << "The value of b is : " <<b<< std::endl;
31. cout<<"Addition of a and b is : "<<a+b;
32. }
33. };
34. int main()
35. {
36. C c;
37. c.get_a(10);
38. c.get_b(20);
39. c.display();
40.
41. return 0;
42. }

Output:

The value of a is : 10
The value of b is : 20
Addition of a and b is : 30

Ambiquity Resolution in Inheritance

Ambiguity can be occurred in using the multiple inheritance when a


function with the same name occurs in more than one base class.

Let's understand this through an example:

1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. public:
6. void display()
7. {
8. std::cout << "Class A" << std::endl;
9. }
10. };
11. class B
12. {
13. public:
14. void display()
15. {
16. std::cout << "Class B" << std::endl;
17. }
18. };
19. class C : public A, public B
20. {
21. void view()
22. {
23. display();
24. }
25. };
26. int main()
27. {
28. C c;
29. c.display();
30. return 0;
31. }

Output:

error: reference to 'display' is ambiguous


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:

1. class C : public A, public B


2. {
3. void view()
4. {
5. A :: display(); // Calling the display() function of class A.
6. B :: display(); // Calling the display() function of class B.
7.
8. }
9. };

Hierarchical Inheritance: In this type of inheritance, more than


one subclass is inherited from a single base class. i.e. more than one
derived class is created from a single base class.

Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented


by combining more than one type of inheritance. For example:
Combining Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple
inheritances:
A special case of hybrid inheritance: Multipath inheritance:
Diamond Problem in C++(When employing numerous inheritances,
a diamond problem can arise in computer languages, particularly in
C++. When the code is exceedingly long, many inheritances in C++
are frequently utilized as a technique)

A derived class with two base classes and these two base classes
have one common base class is called multipath inheritance.
Ambiguity can arise in this type of inheritance.
#include <iostream>
using namespace std;

class ClassA {
public:
int a;
};

class ClassB : public ClassA {


public:
int b;
};

class ClassC : public ClassA {


public:
int c;
};

class ClassD : public ClassB, public ClassC {


public:
int d;
};

int main()
{
ClassD obj;

// obj.a = 10; // Statement 1, Error


// obj.a = 100; // Statement 2, Error

obj.ClassB::a = 10; // Statement 3


obj.ClassC::a = 100; // Statement 4

obj.b = 20;
obj.c = 30;
obj.d = 40;

cout << " a from ClassB : " << obj.ClassB::a;


cout << "\n a from ClassC : " << obj.ClassC::a;

cout << "\n b : " << obj.b;


cout << "\n c : " << obj.c;
cout << "\n d : " << obj.d << '\n';
}
Output
a from ClassB : 10
a from ClassC : 100
b : 20
c : 30
d : 40
Output:
a from ClassB : 10
a from ClassC : 100
b : 20
c : 30
d : 40

In the above example, both ClassB and ClassC inherit ClassA, they
both have a single copy of ClassA.
However Class-D inherits both ClassB and ClassC, therefore Class-D
has two copies of ClassA, one from ClassB and another from
ClassC.
If we need to access the data member of ClassA through the object
of Class-D, we must specify the path from which a will be
accessed, whether it is from ClassB or ClassC, bcoz compiler can’t
differentiate between two copies of ClassA in Class-D.

There are 2 Ways to Avoid this Ambiguity:


1) Avoiding ambiguity using the scope resolution operator: Using
the scope resolution operator we can manually specify the path
from which data member a will be accessed, as shown in
statements 3 and 4, in the above example.
obj.ClassB::a = 10; // Statement 3

obj.ClassC::a = 100; // Statement 4

You might also like