Inheritance
Inheritance
Inheritance
In C++
Introduction
• Reusability: one of the important features of OOPs. We can reuse a class
that has already been tested, debugged and used many times can save us the
effort of developing and testing the same again.
• It means that creating new classes, reusing the properties of the existing
classes. This is called INHERITANCE. The old class is called Base Class
and new Class is called Derived Class.
• The derived class can inherit all or some of the properties of the base class.
It can also be done from multiple or multilevel classes. Depending on it we
have different types of Inheritance.
Types of Inheritance;
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance
Types of Inheritance
Hierarchical Inheritance
Multilevel Inheritance
int main()
{
derived ob(3);
ob.set(1, 2); // ??
ob.show(); //??
return 0;
}
Protected Access Specifier
• The protected keyword is included in C++ to provide greater flexibility.
• When a member of a class is declared as protected, that member is not accessible
by other, non-member elements of the program.
• Similar to private member with one exception.
• If the base class is inherited as public, then the base class' protected members
become protected members of the derived class and are, therefore, accessible by the
derived class.
• By using protected, you can create class members that are private to their class but
that can still be inherited and accessed by a derived class.
Example
#include <iostream>
public:
using namespace std; // derived may access base's i and j
class base { void setk()
ember {
protected: k=i*j;
int i, j; // private to base, but accessible }
by derived void showk()
{
public:
cout << k << "\n";
void set(int a, int b) }
{ };
i=a; j=b; int main()
} {
void show() derived ob;
ob.set(2, 3); // OK, known to derived
{
ob.show(); // OK, known to derived
cout << i << " " << j << "\n"; ob.setk();
} ob.showk();
}; return 0;
class derived : public base { }
int k;
Single Inheritance
• Here a single class is inherited to another class. We can have the inheritance in
Public Mode or Private Mode.
• Inheritance in Public Mode: Here as the inheritance is in public mode the public
members of the base class becomes the public members of the Derived class,
hence they can be accessed by the functions and objects of the derived class.
• Protected Mode: Class can also be inherited in protected mode, both the public and
protected members of the base class become protected members of the derived
class.
• The keywords private, public, protected may appear any no. of time in the class
declaration.
Single Inheritance
• Access Control to the Private and Protected members of a class, what are the
various functions that can have access to these members?
Students
class A
{
.........
};
class B : public A
{
..........
};
class C
{
...........
};
class D : public B, public C
{
...........
};
Ambiguity Resolution in Inheritance
• Sometimes we face the problem in using the Multiple Inheritance, when a function
with the same name appears in more than one base classes. E.g.
class M
{
void display(void)
{
cout<<“Class M\n”;
}
};
class N
{
void display(void)
{
cout<<“Class N\n”;
}
};
Which of the display function is used by the derived class, when we inherit these two
classes.
Ambiguity Resolution in Inheritance
• We can solve this problem by defining a named instance within the derived class, using
the class Resolution Operator with the function:
•When an object of a derived class is created, if the base class contains a constructor, it
will be called first, followed by the derived class' constructor.
•When a derived object is destroyed, its destructor is called first, followed by the base
class' destructor, if it exists.
•Constructors are called in order of derivation, left to right, as specified in derived's inheritance
list. Destructors are called in reverse order, right to left.
Multilevel Inheritance Multiple Inheritance
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
class base { class base1 {
public: public:
base() { cout << "Constructing base\n"; } base1() { cout << "Constructing base1\n"; }
~base() { cout << "Destructing base\n"; } ~base1() { cout << "Destructing base1\n"; }
}; };
class base2 {
class derived1 : public base {
public:
public: base2() { cout << "Constructing base2\n"; }
derived1() { cout << "Constructing derived1\n"; } ~base2() { cout << "Destructing base2\n"; }
~derived1() { cout << "Destructing derived1\n"; } };
}; class derived: public base1, public base2 {
class derived2: public derived1 { public:
derived() { cout << "Constructing derived\n"; }
public:
~derived() { cout << "Destructing derived\n"; }
derived2() { cout << "Constructing derived2\n"; } };
~derived2() { cout << "Destructing derived2\n"; }
}; int main()
{
int main() derived ob;
// construct and destruct ob
{
return 0;
derived2 ob; }
// construct and destruct ob
return 0;
}
Passing Parameters to Base-Class Constructors
• If base class constructor does not takes any argument, the derived class need not have a
constructor function.
• However, if any base class contains a constructor with one or more arguments , then it
becomes mandatory for the derived class to have a constructor and pass the arguments to the
base class constructor.
derived-constructor(arg-list) : base1(arg-list),
base2(arg-list),
// ...
baseN(arg-list)
{
// body of derived constructor
}
#include <iostream>
using namespace std;
class base {
protected:
int i;
public:
base(int x) { i=x; cout << "Constructing base\n"; }
~base() { cout << "Destructing base\n"; }
};
class derived: public base {
int j;
public:
// derived uses x; y is passed along to base.
derived(int x, int y): base(y)
{
j=x;
cout << "Constructing derived\n";
}
~derived() { cout << "Destructing derived\n"; }
void show() { cout << i << " " << j << "\n"; }
};
int main()
{
derived ob(3, 4);
ob.show(); // displays 4 3
return 0;}
Passing arguments to base class constructor in case of
Multiple Base classes
#include <iostream>
using namespace std; class derived: public base1, public base2 {
int j;
class base1 {
public:
protected:
int i; derived(int x, int y, int z): base1(y), base2(z)
public: {
base1(int x) j=x;
{ cout << "Constructing derived\n";
}
i=x;
~derived() {
cout << "Constructing base1\n"; } cout << "Destructing derived\n";
~base1() { cout << "Destructing base1\n"; } }
};
class base2 { void show() {
protected: cout << i << " " << j << " " << k << "\n";
}
int k;
};
public:
base2(int x) int main()
{ {
k=x; derived ob(3, 4, 5);
cout << "Constructing base2\n"; } ob.show();
return 0;
~base2() { cout << "Destructing base1\n"; }
}
};
A derived class' constructor function is free to make use of any and all
parameters that it is declared as taking, even if one or more are passed along to
a base class.