0% found this document useful (0 votes)
11 views22 pages

Inheritance

The document provides an overview of inheritance in C++, explaining its purpose for code reusability and the relationship between base and derived classes. It covers various forms of inheritance including single, multilevel, multiple, hierarchical, and hybrid inheritance, along with examples of each. Additionally, it discusses the visibility modes and access mechanisms associated with inherited members.

Uploaded by

karan.singh2024a
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)
11 views22 pages

Inheritance

The document provides an overview of inheritance in C++, explaining its purpose for code reusability and the relationship between base and derived classes. It covers various forms of inheritance including single, multilevel, multiple, hierarchical, and hybrid inheritance, along with examples of each. Additionally, it discusses the visibility modes and access mechanisms associated with inherited members.

Uploaded by

karan.singh2024a
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

INHERITANCE

1
INTRODUCTION

C++ provides an opportunity to reuse the code functionality and fast


implementation time.
C++ classes can be reused
Reusability can be achieved through Inheritance.

The mechanism of deriving a new class from an old one is called


Inheritance.

The old class is referred as base class and the new one is called derived
class.

Inheritance is the process by which objects of one class acquired the


properties of objects of another classes.

2
INTRODUCTION

The derived class inherits some or all of the traits from the base class.

A class can inherit properties from one class or more number classes or
from more than one level.

3
Forms of Inheritance

4
Defining Derived Classes

The general form of defining a derived class is:

Class derived_classname : visibility_mode base_classname


{
// members of the derived class
};

visibility_mode – optional or it can be public or private

Visibility mode specifies whether the features of the base class are
privately derived or publicly serived.

5
Defining Derived Classes

For example:

Class derived : private base


{
// members of the derived class
};

Class derived : public base


{
// members of the derived class
};

- default scenario could be a private derivation 6


Modes of Inheritance

A protected member can be accessible by the member function within its


class and any class immediately derived from it.
7
Effect of Inheritance on Members Visibility

8
Access Mechanism in classes

9
Single Inheritance
#include <iostream>
using namespace std;
class Parent
{
public:
int id_p; Output
};
class Child : public Parent Child id is 7
{ Parent id is 91
public:
int id_c;
};
int main()
{
Child obj1;
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is " << obj1.id_c << endl;
cout << "Parent id is " << obj1.id_p << endl;
return 0;
10
}
Single Inheritance : Public
void B :: set_ab(void) int main()
#include <iostream>
{ {
using namespace std;
a=5; b=10; D d;
class B
} d.set_ab();
{
int B :: get_a() [Link]();
int a;
{ return a; d.show_a();
public:
} [Link]();
int b;
void B :: show_a() d.b=20;
void set_ab();
{ cout<<"a= "<<a<<"\n"; [Link]();
int get_a(void);
} [Link]();
void show_a(void);
void D :: mul() return 0;}
};
{ c = b*get_a();
Output
class D : public B }
a= 5
{ void D :: display()
a= 5
int c; { cout<<"a= "<<get_a()<<"\n";
b= 10
public: cout<<"b= "<<b<<"\n";
c= 50
void mul(void); cout<<"c= "<<c<<"\n";
a= 5
void display(void); }
b= 20
}; c= 10011
Single Inheritance : Private
#include <iostream> void B :: get_ab(void) void D :: display()
using namespace std; { { show_a();
class B cout<<"Enter Values for cout<<"b= "<<b<<"\n";
{ A and B: "; cout<<"c= "<<c<<"\n";
int a; cin>>a>>b; }
public: }
int b; int main()
void get_ab(); int B :: get_a() {
int get_a(void); { D d;
void show_a(void); return a; } [Link]();
}; [Link]();
class D : private B void B :: show_a() return 0;}
{ {
int c; cout<<"a= "<<a<<"\n";
public: } Output
void mul(void); void D :: mul() Enter Values for A and B: 10
void display(void); { 4
}; get_ab(); a= 10
c = b*get_a(); b= 4
} c= 40 12
Multilevel Inheritance
If a class is derived from another derived class then it is called multilevel
inheritance.

Syntax:

class A
{
// Base class
};

class B : A
{
// Intermediate class
};

class C : B
{
// Derived class
}; 13
Multilevel Inheritance
class compare: public get_data
#include<iostream>
{
using namespace std;
public:
void compare_data()
class get_data //Base class
{
{
if (pin==1234)
public:
{
int pin;
cout<<"\n Your pin number is valid. ";
void get_value()
}
{
else
cout<<"\n\t Welcome to the Bank ";
{
cout<<"\n\n Enter your pin number: ";
cout<<"\n Your pin number is invalid. ";
cin>>pin;
}
}
}
};
};

14
Multilevel Inheritance
class cash_withdrawal: public compare int main()
{ {
public: cash_withdrawal c;
int amt; c.get_value();
int cash; c.compare_data();
void get_cash() c.get_cash();
{ return 0;
amt=2000; }
if (pin==1234)
{
cout<<"\n\n Enter an amount for cash withdrawal: ";
cin>>cash;
amt=amt-cash; Output
cout<<"\n Your current balance : "<<amt; Welcome to the Bank
}
else Enter your pin number: 1234
{ Your pin number is valid.
cout<<"\n Please enter correct pin number..."; Enter an amount for cash
} withdrawal: 500
} }; 15
Your current balance : 1500
Multiple Inheritance
A class can inherit the attributes of two or more classes and is known as
multiple inheritance.

Syntax:

class D : visibility_mode Base_class1, visibility_mode Base_class2


{
// members
};

Example:

class C : public A, private B


{
//members
};

16
class C: public A, public B
Multiple Inheritance {
public:
#include <iostream> int c = 20;
using namespace std; C() {
class A cout << "Constructor for class C“ << endl;
{ cout<<"Class C inherits from
public: class A and class B" << endl;
int a = 5; } };
A() int main()
{ {
cout << "Constructor for class A" << endl; C obj;
} }; cout<<"a = "<< obj.a <<endl;
class B cout<<"b = "<< obj.b <<endl;
{ cout<<"c = "<< obj.c <<endl;
public: return 0;
int b = 10; }
B() { Output
cout << "Constructor for class B" << Constructor for class A
endl; Constructor for class B
} }; Constructor for class C
Class C inherits from class A and class B
17
a = 5 b = 10 c = 20
Hierarchical Inheritance
In hierarchical inheritance, multiple classes can be derived from the same
class.

Syntax:
class base_class
{
...
};
class first_derived_class : access_specifier base_class
{
...
};
class second_derived_class : access_specifier base_class
{
...
};
class third_derived_class : access_specifier base_class
{
...
18
};
class Triangle : public Shape
Hierarchical Inheritance {
public:
#include <iostream> Triangle()
using namespace std; {
class Shape cout<<"Derived Class - Triangle“ <<endl;
{ } };
public:
Shape() class Circle : public Shape
{ {
cout<<"Base Class - Shape"<<endl; public:
} Circle()
}; {
cout<<"Derived Class - Circle“ <<endl;
class Rectangle : public Shape } };
{
public: int main() Output
Rectangle() { Base Class - Shape
{ Rectangle r; Derived Class - Rectangle
cout<<"Derived Class - Rectangle“ Triangle t; Base Class - Shape
<<endl; Circle c; Derived Class - Triangle
} return 0; Base Class - Shape
}; } 19
Derived Class - Circle
Hybrid Inheritance
In hybrid inheritance, we can combine two or more types of inheritance.

Example:

class A
{…
};

class B : public A
{…
};

class C
{…};

class D : public B, C
{…
};

20
Hybrid Inheritance
Can you try to create an example template for the below fig.?

21
Hybrid Inheritance class Racing
{
public:
#include <iostream>
Racing()
using namespace std;
{
class vehicle
cout<< "This is for Racing\n";
{
}
public:
};
vehicle()
class Ferrari: public Car, public Racing
{
{
cout<< "This is a vehicle\n";
public:
}
Ferrari()
};
{
cout<< "Ferrari is a Racing Car\n";
class Car: public vehicle
}
{
public:
};
Car()
int main()
{ Output
{
cout<< "This is a car\n"; This is a vehicle
Ferrari f;
} This is a car
return 0;
}; This is for Racing
} 22
Ferrari is a Racing Car

You might also like