Inheritance 1
Inheritance 1
Inheritance
• Inheritance is the ability of one class to inherit the properties of
another class. A new class can be created from an existing class. The
existing class is called BASE CLASS or SUPER CLASS and the new class
is called DERIVED CLASS or SUB CLASS.
• Example:
Car Bicycle
2-door 4-door
Advantages of inheritance
• When a class inherits from another class, there are three benefits:
(1) You can reuse the methods and data of the existing class
(2) You can extend the existing class by adding new data and new
methods
(3) You can modify the existing class by overloading its methods
with your own implementations
Inheritance is the property that allows the
reuse of an existing class to build a new class
Private Yes No No
Inheritance
Person
Employee
Multilevel
Inheritance
A class be can derived from a derived class which is known as multilevel
inheritance.
Vehicle Person
Car Employee
It is the process of creating new class from more than one base
classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
Person Employee
// members;
public :
//memebers;
};
Teacher
Hierarchical Inheritance
If more than one class is inherited from a base class, it's known as hierarchical
inheritance. In general, all features that are common in child classes are included
in base class in hierarchical inheritance.
Employee
Permanent Temporary
Employee Employee
Hybrid Inheritance
Hybrid is nothing but the combination of Multilevel
and multiple Inheritance
Multipath problem void sum()
class B: virtual public A {
#include<iostream>
{ d=a+b+c;
using namespace std; protected:
}
class A int b;
public: void display()
{
void setb() {
protected: {
b=20; cout<<"Sum of a,b,c is: " << d ;
int a;
} }
public: };
};
void seta() class C: virtual public A
{ int main()
{
protected: {
a=10; int c;
public: D d1;
}
void setc() d1.seta();
}; {
d1.setb();
c=30;
} d1.setc();
}; d1.sum();
class D:public B,public C
{ d1.display();
private: return 0;
int d;
}
Same Data Member Name in Base
and Derived Class
#include<iostream> class B:public A
using namespace std; { main()
{
class A int x; B b;
{ public: A a;
protected: B(){x=10;} a.print();
int x; b.print();
public: }
void print()
A(){x=5;}
{
void print()
cout<<"inside B"<<endl;
{
cout<<x<<endl;
cout<<"inside
A"<<endl;} }
Order of Constructor Call
Base class constructors are always called in the derived class constructors.
Whenever you create derived class object, first the base class default
constructor is executed and then the derived class's constructor finishes
execution.
Points to Remember
1.Whether derived class's default constructor is called or parameterised is
called, base class's default constructor is always called inside them.
{
int main()
x = i; {
cout << "Base Parameterized Constructor\n"; Base b;
} Derived d1;
Derived d2(10);
};
Base class Parameterized Constructor in Derived class Constructor
We can explicitly mention to call the Base class's parameterized constructor
when Derived class's parameterized constructor is called.
Constructor call in Multiple Inheritance
Its almost the same, all the Base class's constructors are called inside derived class's
constructor, in the same order in which they are inherited.
class A : public B, public C ;
In this case, first class B constructor will be called, then class C constructor and then
class A constructor.
3 Types of Access Specifiers
Base Derived
Base Derived
Base Derived
class Base {
public: int a;
protected: int b;
private: int c;
};
class Derived:public Base {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};
int main() {
Derived obj;
obj.a = 10; //Allowed
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}
Private Inheritance:
All Public members of the Base Class become Private Members of the Derived class &
All Protected members of the Base Class become Private Members of the Derived Class.
Class Base {
public: int a;
protected: int b;
private: int c; };
class Derived:private Base //defaults access specifier is private {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error }
};
class Derived2:public Derived {
void doSomethingMore() {
a = 10; //Not Allowed, Compiler Error, a is private member of Derived now
b = 20; //Not Allowed, Compiler Error, b is private member of Derived now
c = 30; //Not Allowed, Compiler Error }
};
int main() {
Derived obj; obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error }
• Protected Inheritance:
All Public members of the Base Class become Protected Members of the derived class
& All Protected members of the Base Class become Protected Members of the Derived
Class.
Class Base {
public: int a;
protected: int b;
private: int c; };
class Derived:protected Base {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error }
};
class Derived2:public Derived {
void doSomethingMore() {
a = 10; //Allowed, a is protected member inside Derived & Derived2 is
public derivation from Derived, a is now protected member of Derived2
b = 20; //Allowed, b is protected member inside Derived & Derived2 is public
derivation from Derived, b is now protected member of Derived2
c = 30; //Not Allow ed, Compiler Error }
};
int main() {
Derived obj; obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}