Inheritance Final1
Inheritance Final1
Inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically called inheritance In such way, it can
reuse, extend or modify the attributes and behaviors which are defined in other
class.
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
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
Derived Classes
A Derived class is defined as the class derived from the base class.
Where,
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.
Where 'A' is the base class and 'B' is the derived class.
When one class inherits another class, it is known as single level inheritance.
#include <iostream>
using namespace std;
class Account {
public: float salary = 60000;
cout<<"salary="<<salary;
}
class Programmer:: public Account {
public:
float bonus = 5000;
cout<<"bonus="<<bonus;
}
int main() {
Programmer p1;
P1.salary;
P1.bonus;
return 0;
}
Test it Now
Output: Salary: 60000
Bonus: 5000
#include <iostream>
using namespace std;
class Animal {
public: void eat() {
cout<<"Eating..";
}
};
class Dog::public Animal
{
public: void bark(){
cout<<"Barking...";
}
};
int main() {
Dog d1;
d1.eat();
d1.bark();
return 0;
}
Test it Now
Output:
Eating...
Barking...
Visibility modes can be classified into three categories:
2) Multilevel Inheritance
#include <iostream>
using namespace std;
class Animal {
public: void eat() {
cout<<"Eating...;
}
};
class Dog:: public Animal
{
public: void bark(){
cout<<"Barking...;
}
};
class BabyDog::public Dog
{
public: void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
Test it Now
Output:
Eating...
Barking...
Weeping...
3) Multiple Inheritances
Multiple inheritances is the process of deriving a new class that inherits the
attributes from two or more classes.
class B
{
protected: int b;
public: void getb(int n)
{
b = n;
}
};
class C :: public A,public B
{
public: void display()
{
cout << "The value of a is : " <<a;
cout << "The value of b is : " <<b;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.geta(10);
c.getb(20);
c.display();
return 0;
}
Test it Now
Output:
The value of a is : 10
The value of b is : 20
Addition of a and b is : 30
In the above example, class 'C' inherits two base classes 'A' and 'B' in a public
mode.
4) Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than one class
from a base class.
Syntax of Hierarchical inheritance:
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}
5) Hybrid Inheritance
include <iostream>
using namespace std;
class A
{
protected: int a;
public: void geta()
{
cout << "Enter the value of 'a' : " ;
cin>>a;
}
};
class B :: public A
{
protected: int b;
public: void getb()
{
cout << "Enter the value of 'b' : " ;
cin>>b;
}
};
class C
{
protected: int c;
public: void getc()
{
cout << "Enter the value of c is : " ;
cin>>c;
}
};
#include <iostream>
using namespace std;
class A
{
public: void display()
{
cout << "Class A" ;
}
};
class B
{
public: void display()
{
cout << "Class B" ;
}
};
class C : public A, public B
{
void view()
{
display ();
}
};
int main()
{
C c;
c.display();
return 0;
}
Test it now
Output:
oThe 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:
class C : public A, public B
{
void view()
{
A :: display(); // Calling the display() function of class A.
B :: display(); // Calling the display() function of class B.
}
};