Inheritance Abstract
Inheritance Abstract
INHERITANCE
•Inheritance is an ―”is-a” relation, which inherits the attributes and behavior’s from its parent
class.
•For example, dog is an animal. It means animal is a parent class and Dog is the child class.
• The child class ―”Dog” inherits the attributes like age and weight from the parent class ,
which is an animal.
: Definition
■ allows sharing of the behavior of the parent class into its child classes, code sharing
between classes through inheritance
■ child class can add new behavior or override existing behavior from parent. The new
class is a specialized version of the existing class
• All the data and methods available to the parent class also appear in the child
• superclass, base class, parent class: terms to describe the parent in the relationship,
which shares its functionality
• subclass, derived class, child class: terms to describe the child in the relationship,
which accepts functionality from its parent
• Methods of the base class are still accessible in the derived class:
• Protected and public members of a public base class are protected or public, respectively, in a
derived class.
• Protected and public members of a protected base class are protected members of a derived
class.
• Protected and public members of a private base class are private members of a derived class.
Modes of Inheritance
1. Public mode: If we derive a sub class from a public base class. Then the public
member of the base class will become public in the derived class and protected members of
2. Protected mode: If we derive a sub class from a Protected base class. Then both
public member and protected members of the base class will become protected in derived
class.
3. Private mode: If we derive a sub class from a Private base class. Then both public
member and protected members of the base class will become Private in 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 below example. It is just question of access.
class A
{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A
// x is public
// y is protected
};
class C : protected A
// x is protected
// y is protected
};
// x is private
// y is private
};
o Public: When the member is declared as public, it is accessible to all the functions of the
program.
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.
Types of 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 Account {
4. public:
5. float salary = 60000;
6. };
7. class Programmer: public Account {
8. public:
9. float bonus = 5000;
10. };
11. int main(void) {
12. Programmer p1;
13. cout<<"Salary: "<<p1.salary<<endl;
14. cout<<"Bonus: "<<p1.bonus<<endl;
15. return 0;
16. }
Multilevel Inheritance
Multilevel inheritance is a process of deriving a class from another derived class.
// Multilevel Inheritance
#include <iostream.h>
// base class
class Vehicle
public:
Vehicle()
};
{ public:
fourWheeler()
};
public:
car()
};
// main function
int main()
{
Car obj;
return 0;
output:
This is a Vehicle
2)
#include <iostream>
public:
int x;
void getdata()
};
{
public:
int y;
void readdata()
};
private:
int z;
public:
void indata()
void product()
};
int main()
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
} //end of program
Output
Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18
Multiple Inheritance:
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes.
i.e one sub class is inherited from more than one base class.
Syntax:
//body of subclass
};
Here, the number of base classes will be separated by a comma (‘, ‘) and access mode for
// multiple inheritance
#include <iostream>
class Vehicle {
public:
Vehicle()
};
class FourWheeler {
public:
FourWheeler()
};
// sub class derived from two base classes
};
// main function
int main()
Car obj;
return 0;
Output:
This is a Vehicle
2)
#include<iostream.h>
#include<conio.h>
class student {
protected:
void get() {
cout << "Enter the two marks :"; cin >> m1>>m2;
}};
class sports
protected:
void getsm() {
}};
public:
void display() {
<< tot;
};
void main() {
esult obj;
obj.get();
obj.getsm();
obj.display();
}
Hierarchical Inheritance:
In this type of inheritance, more than one sub class is inherited from a single base class. i.e.
more than one derived class is created from a single base class.
1. class A
2. {
3. // body of the class A.
4. }
5. class B : public A
6. {
7. // body of class B.
8. }
9. class C : public A
10. {
11. // body of class C.
12. }
13. class D : public A
14. {
15. // body of class D.
16. }
Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.
Consider the following program:
#include<iostream.h>
#include<conio.h>
class ClassA
public:
int a;
};
public:
int b;
};
{
public:
int c;
};
public:
int d;
};
void main()
ClassD obj;
obj.b = 20;
obj.c = 30;
obj.d = 40;
Output:
A from ClassB : 10
B : 20
C : 30
D : 40
»according to the types of variables (references) x and y – static method binding (will call the
method of person in both cases)
»according to the types of objects s and p to which x and y refer – dynamic method binding
(will call the methods of student / professor)
•Example (C++):
student s;
professor p;
...
person * x = &s;
person * y = &p;
x->print_mailing_label (); // ??
y->print_mailing_label (); // ??
Virtual Function
...
public:
...
};
#include <iostream>
// Abstract class
class Shape {
protected:
float dimension;
public:
void getDimension() {
};
// Derived class
public:
float calculateArea() {
};
// Derived class
public:
float calculateArea() {
};
int main() {
Square square;
Circle circle;
square.getDimension();
circle.getDimension();
return 0;