0% found this document useful (0 votes)
3 views13 pages

Inheritance Final1

C++ inheritance allows one class to acquire properties and behaviors from another, promoting code reusability. There are five types of inheritance in C++: single, multilevel, multiple, hierarchical, and hybrid. Visibility modes for inherited members can be public, private, or protected, and ambiguity can arise in multiple inheritance scenarios, which can be resolved using class resolution operators.

Uploaded by

ramboraja1111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Inheritance Final1

C++ inheritance allows one class to acquire properties and behaviors from another, promoting code reusability. There are five types of inheritance in C++: single, multilevel, multiple, hierarchical, and hybrid. Visibility modes for inherited members can be public, private, or protected, and ambiguity can arise in multiple inheritance scenarios, which can be resolved using class resolution operators.

Uploaded by

ramboraja1111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

C++ Inheritance

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.

Advantage of C++ Inheritance

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

C++ supports five 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.

The Syntax of Derived class:

class derived_class_name :: visibility - mode base_class_name


{
// body of the derived class.
}

Where,

derived_class_name: It is the name of the derived class.

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.

base_class_name: It is the name of the base class.


Type of inheritance

1) Single inheritance is defined as the inheritance in which a derived class is


inherited from the only one base class.

Where 'A' is the base class and 'B' is the derived class.

Single Level Inheritance

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

Employee is the base class and Programmer is the derived class.

Single Level Inheritance Example:

#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:

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.

2) Multilevel Inheritance

Multilevel inheritance is a process of deriving a class from another derived class.

Multi Level Inheritance


When one class inherits another class which is further inherited by another class,
it is known as multi-level inheritance in C++. Inheritance is transitive so the last
derived class acquires all the members of all its base classes.
example of multi-level inheritance in C++.

#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.

Syntax of the Derived class:

class D :: visibility B-1, visibility B-2, ?


{
// Body of the class;
}

Example of multiple inheritance.


#include <iostream>
using namespace std;
class A
{
protected: int a;
public: void geta(int n)
{
a = n;
}
};

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.
}

Example of hierarchical inheritance


#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public: int a,b;
void get_data(int x,int y)
{
a= x;
b = y;
}
};
class Rectangle :: public Shape
{
public: void rect_area()
{
int result = a*b;
cout<<"Area of rectangle:"<<result;
}
};
class Triangle :: public Shape // inheriting Shape class
{
public: void triangle_area()
{
float result2 = 0.5*a*b;
cout<<"Area of triangle:"<<result2;
}
};
int main()
{
Rectangle r;
Triangle t;
int l,b,h;
cout << "Enter the length and breadth of a rectangle: ";
cin>>l>>b;
r.get_data(l,b);
r.rect_area();
cout<<"\n";
cout << "Enter the base and height of the triangle: " ;
cin>>b>>h;
t.get_data(b, h);
t.triangle_area();
return 0;} }

5) Hybrid Inheritance

Hybrid inheritance is a combination of more than one type of inheritance.


A simple example:

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;
}
};

class D :: public B, public C


{
protected: int d;
public: void mul()
{
cout << "Multiplication of a,b,c is : " <<a*b*c;
}
};
int main()
{
D d;
d.mul();
return 0;
}
Test it Now
Output:

Enter the value of 'a' :


10
Enter the value of 'b' :
20
Enter the value of c is :
30
Multiplication of a,b,c is : 6000

Ambiguity Resolution in Inheritance


Ambiguity can be occurred in using the multiple inheritances when a function
with the same name occurs in more than one base class.

#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.

}
};

You might also like