Inheritance
Inheritance
Inheritance
If neither is specified,
PRIVATE is taken as default.
Note: A derived class doesn’t inherit access to private data members. However, it does
inherit a full parent object, which contains any private members which that class
declares.
Example:
1. class ABC : private XYZ //private derivation
{ }
2. class ABC : public XYZ //public derivation
{ }
3. class ABC : protected XYZ //protected derivation
{ }
4. class ABC: XYZ //private derivation by default
{ }
Note:
# When a base class is privately inherited by the derived class, public members of the
base class becomes the private members of the derived class and therefore, the public
members of the base class can only be accessed by the member functions of the derived
class. They are inaccessible to the objects of the derived class.
# On the other hand, 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 derived class.
#include <iostream>
using namespace std;
class Person {
int id;
char name[100];
public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
};
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}
void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
Output:
Enter the Id: 101
Enter the Name: Dev
Enter the Course Name: GCS
Enter the Course Fee:70000
Id: 101
Name: Dev
Course: GCS
Fee: 70000
#include<iostream>
using namespace std;
class Person
{
int id;
char name[100];
public:
void set_p();
void display_p();
};
void Person::set_p()
{
cout<<"Enter the Id:";
cin>>id;
cout<<"Enter the Name:";
cin>>name;
}
void Person::display_p()
{
cout<<endl<<"id: "<< id<<"\nName: "<<name;
}
public:
void set_s();
void display_s();
};
void Student::set_s()
{
set_p();
cout<<"Enter the Course Name:";
cin>>course;
cout<<"Enter the Course Fee:";
cin>>fee;
}
void Student::display_s()
{
display_p();
cout<<"\nCourse: "<<course<<"\nFee: "<<fee<<endl;
}
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
Output:
Enter the Id: 101
Enter the Name: Dev
Enter the Course Name: GCS
Enter the Course Fee: 70000
Id: 101
Name: Dev
Course: GCS
Fee: 70000
#include<iostream>
#include<string.h>
using namespace std;
class Person
{
int id;
char name[100];
public:
void set_p(int,char[]);
void display_p();
};
void Person::display_p()
{
cout<<endl<<id<<"\t"<<name;
}
void Student::display_s()
{
display_p();
cout<<"t"<<course<<"\t"<<fee;
}
main()
{
Student s;
s.set_s(1001,"Ram","B.Tech",2000);
s.display_s();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
// Base class
class Parent {
public:
int id_p;
};
// main function
int main()
{
Child obj1;
Output
Child id is: 7
Parent id is: 91
Output:
Child id is: 7
Parent id is: 91
In the above program, the ‘Child’ class is publicly inherited from the ‘Parent’ class so
the public data members of the class ‘Parent’ will also be inherited by the class ‘Child’.
Modes of Inheritance: There are 3 modes of 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: The private members in the base class cannot be directly accessed in the derived
class, while protected members can be directly accessed. For example, Classes B, C, and
D all contain the variables x, y, and z in the below example. It is just a question of access.
// C++ Implementation to show that a derived class
// doesn’t inherit access to private data members.
// However, it does inherit a full parent object.
class A {
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A {
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A {
// x is protected
// y is protected
// z is not accessible from C
};
The below table summarizes the above three modes and shows the access specifier of the
members of the base class in the subclass when derived in public, protected and private
modes:
Types Of Inheritance:-
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
Syntax:
class subclass_name : access_mode base_class
{
// body of subclass
};
OR
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output
This is a Vehicle
// Example:
#include<iostream>
using namespace std;
class A
{
protected:
int a;
public:
void set_A()
{
cout<<"Enter the Value of A=";
cin>>a;
}
void disp_A()
{
cout<<endl<<"Value of A="<<a;
}
};
class B: public A
{
int b,p;
public:
void set_B()
{
set_A();
cout<<"Enter the Value of B=";
cin>>b;
}
void disp_B()
{
disp_A();
cout<<endl<<"Value of B="<<b;
}
void cal_product()
{
p=a*b;
cout<<endl<<"Product of "<<a<<" * "<<b<<" = "<<p;
}
};
main()
{
B _b;
_b.set_B();
_b.cal_product();
return 0;
#include<iostream>
using namespace std;
class A
{
protected:
int a;
public:
void set_A(int x)
{
a=x;
}
void disp_A()
{
cout<<endl<<"Value of A="<<a;
}
};
class B: public A
{
int b,p;
public:
void set_B(int x,int y)
{
set_A(x);
b=y;
}
void disp_B()
{
disp_A();
cout<<endl<<"Value of B="<<b;
}
void cal_product()
{
p=a*b;
cout<<endl<<"Product of "<<a<<" * "<<b<<" = "<<p;
}
};
main()
{
B _b;
_b.set_B(4,5);
_b.cal_product();
return 0;
}
Output
Product of 4 * 5 = 20
Syntax:
class subclass_name : access_mode base_class1, access_mode
base_class2, ....
{
// body of subclass
};
class B
{
... .. ...
};
class C
{
... .. ...
};
class A: public B, public C
{
... ... ...
};
Here, the number of base classes will be separated by a comma (‘, ‘) and the access mode
for every base class must be specified.
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle
// Example:
#include<iostream>
using namespace std;
class A
{
protected:
int a;
public:
void set_A()
{
cout<<"Enter the Value of A=";
cin>>a;
void disp_A()
{
cout<<endl<<"Value of A="<<a;
}
};
class B: public A
{
protected:
int b;
public:
void set_B()
{
cout<<"Enter the Value of B=";
cin>>b;
}
void disp_B()
{
cout<<endl<<"Value of B="<<b;
}
};
class C: public B
{
int c,p;
public:
void set_C()
{
cout<<"Enter the Value of C=";
cin>>c;
}
void disp_C()
{
cout<<endl<<"Value of C="<<c;
}
void cal_product()
{
p=a*b*c;
cout<<endl<<"Product of "<<a<<" * "<<b<<" * "<<c<<" = "<<p;
}
};
main()
{
C _c;
_c.set_A();
_c.set_B();
_c.set_C();
_c.disp_A();
_c.disp_B();
_c.disp_C();
_c.cal_product();
return 0;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle
// Example:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
cout << "Enter the value of 'a' : ";
cin>>a;
}
};
class B : public A
{
protected:
int b;
public:
void get_b()
{
cout << "Enter the value of 'b' : ";
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
cout << "Enter the value of c is : ";
cin>>c;
}
};
int main()
{
D d;
d.mul();
return 0;
}
class ClassA {
public:
int a;
};
int main()
{
ClassD obj;
obj.b = 20;
obj.c = 30;
obj.d = 40;
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.
int main()
{
ClassD obj;
obj.b = 20;
obj.c = 30;
obj.d = 40;
Output:
a : 100
b : 20
c : 30
d : 40
According to the above example, Class-D has only one copy of ClassA, therefore,
statement 4 will overwrite the value of a, given in statement 3.