0% found this document useful (0 votes)
2 views

Module 3 Inheritance

The document provides an overview of inheritance in C++, detailing various types such as single, multilevel, multiple, hierarchical, and hybrid inheritance. It explains the concept of a base class and derived class, emphasizing the reusability of code through inheritance. Additionally, it includes code examples to illustrate the implementation of these inheritance types and mentions exception handling in C++.

Uploaded by

rakshitharvr95
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 3 Inheritance

The document provides an overview of inheritance in C++, detailing various types such as single, multilevel, multiple, hierarchical, and hybrid inheritance. It explains the concept of a base class and derived class, emphasizing the reusability of code through inheritance. Additionally, it includes code examples to illustrate the implementation of these inheritance types and mentions exception handling in C++.

Uploaded by

rakshitharvr95
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Class base

{
Data+functions};

Class derive:public base


{
Functions of base class;
New funcions;
};
Inheritance
Inheritance: Defining derived class, single inheritance, Multilevel Inheritance, Multiple
Inheritance, Hierarchical Inheritance, Hybrid Inheritance, Virtual base classes.
Inheritance allows a class to inherit the properties
and behaviour from another class
• C++ strongly supports the concept of reusability. The C++ classes can
be reused in several ways. Once a class has been written and tested, it
can be adopted by other programmers to suit their requirements.
• This is basically done by creating new classes, reusing the properties
of the existing ones.
• The mechanism of deriving a new class from an old one is called
inheritance (or derivation).
• The old class is referred as base class and the new one is called the
derived class or subclass.
Example 1:

#include<iostream>
string name="deon";
using namespace std; public:
class father{ void disp(){
protected: cout <<name<<" "<<surname<<endl;}
string surname="disouza"; };
}; int main(){
class son1:father{ son1 s1;
s1.show();
string name="dane";
son2 s2;
public:
s2.disp();
void show(){ return 0;
cout <<name<<" "<<surname<<endl;} }
};
class son2:father{
Types of
Inheritance The derived class inherits some or all traits from the
base class.
A class can also inherits properties from more than
one class or from more than one level.

A derived class with only one base class, is called


single inheritance.

A derived class with several base classes is called


multiple inheritance.

A traits of one class may be inherited by more than


one class is called hierarchical inheritance.

The mechanism of deriving a class from another


derived class is known as multilevel inheritance.

It is the combination of more than one type of


inheritance is called hybrid inheritance.
Defining derived Classes: A derived class can be defined by specifying its relationship with the base class in addition to its own details. The
general form of defining a derived class is:
Making a private member inheritable.
Single
inheritance
A class which contain only one base class and only one derive class is called
single inheritance.
Example 2: Single Heritance
#include<iostream> void disp(){
using namespace std;
class base
cout<<"m"<<" "<<m<<" "<<"n"<<" "<<n<<endl;
{
private: }};
int a, b; int main()
public: {
void input() base ob;
{ derive ob1;
cout<<"enter values"<<endl; Note:
//ob.input();
cin>>a>>b; 1. Single inheritance with
//ob.show();
} one base and one
ob1. input();
void show(){ derived class
cout<<"a"<<" "<<a<<" "<<"b"<<" "<<b<<endl;} ob1.show();
2. Accessing the function
}; ob1.getdata();
of base by the derived
class derive:public base{ ob1.disp();
objects
private: return 0;
int m,n; 3. Displaying the values
}
public: of base class in the
void getdata() derived disp function.
{ 4. Accesing all the
cout<<"enter values"<<endl; functions of base class
cin>>m>>n; only by the derived
} objects.
#include<iostream> void disp(){
using namespace std;
class base cout<<"m"<<" "<<m<<" "<<"n"<<" "<<n<<endl;
{ cout<<"a"<<" "<<a<<" "<<"b"<<" "<<b<<endl;
private: }};
int a, b; int main()
public: {
void input() base ob;
{ derive ob1;
cout<<"enter values"<<endl; //ob.input();
cin>>a>>b; //ob.show();
} ob1. input();
/*void show(){ //ob1.show();
cout<<"a"<<" "<<a<<" "<<"b"<<" "<<b<<endl;}*/ ob1.getdata();
}; ob1.disp();
class derive:public base{ return 0;
private: }
int m,n;
public:
void getdata()
{
cout<<"enter values"<<endl;
cin>>m>>n;
}
Multilevel inheritance: A class which contain only one base class
and multiple derive class is called multilevel inheritance.
#include<iostream> void show1(){ int main(){
using namespace std;
cout<<"b"<<" "<<b<<endl;} /*base ob;
class base
{
}; ob.input();
private: class derive2:public derive1 ob.show();
int a; { derive1 ob1;
public: private: ob1.input();
void input() int c; ob1.show();*/
{ public: derive2 ob2;
cout<<"enter the values of base class"<<endl; void input2() ob2.input();//base functions by derived 2 class
cin>>a;
{ ob2.show();
}
void show(){ cout<<"enter the values of derive 2 ob2.input1();//derive1 functions by derived 2
cout<<"a"<<" "<<a<<endl;} class"<<endl; class
}; cin>>c; ob2.show1();
class derive1:public base } ob2.input2();
{ void show2(){ ob2.show2();
private: cout<<"c"<<" "<<c<<endl;} return 0;
int b; public: }; }
void input1()
{
cout<<"enter the values of derive 1
class"<<endl;
cin>>b;
}
#include<iostream> class derive1:public base
using namespace std; {
class base protected:
{ int b;
protected: public:
int a; void input1()
public: {
void input() cout<<"enter the values of derive 1 class"<<endl;
{ cin>>b;
cout<<"enter the values of base class"<<endl; }
cin>>a; /*void show1(){
} cout<<"a"<<a<<"b"<<" "<<b<<endl;}*/
/*void show(){ };
cout<<"a"<<" "<<a<<endl;} class derive2:public derive1
*/ {
}; private:
int c;
public:
void input2()
{
cout<<"enter the values of derive 2 class"<<endl;
cin>>c;
}
Cont’d
void show2(){
cout<<"a"<<a<<"b"<<b<<"c"<<" "<<c<<endl;}
};
int main(){ Note:
/*base ob; 1. Multilevel inheritance
ob.input(); with one base and
ob.show(); multiple derived class
derive1 ob1; 2. Accessing the function
ob1.input(); of base and derive1
ob1.input1(); by the derive2 objects
ob1.show1();*/ 3. Displaying the values
derive2 ob2; of base class in the
ob2.input();//base functions by derived 2 class derive1 show
//ob2.show(); function.
ob2.input1();//derive1 functions by derived 2 class 4. Accesing all the
//ob2.show1(); functions of base class
ob2.input2(); and derive 1 class by
ob2.show2(); derive 2 objects.
return 0;
}
Multiple Inheritance A class which contain more base class and only one derive class is
called multiple inheritance.
public:
#include<iostream> int main(){
void input1()
using namespace std; /*base1 ob;
{
class base1 ob. input();
cout<<"enter the values of base2 class"<<endl;
{ ob.show();
cin>>a>>b;
private: base2 ob1;
}
int a,b,c; ob1. input1();
void show1(){
public: ob1.show1();*/
c=a-b;
void input() derive ob2;
cout<<"difference"<<c<<endl;}
{ ob2. input2();
};
cout<<"enter the values of base1 class"<<endl; ob2.show2();
class derive:public base1,public base2
cin>>a>>b; ob2. input1();
{
} ob2.show1();
private:
void show(){ ob2. input();
int a,b,c;
c=a+b; ob2.show();
public:
cout<<"sum"<<c<<endl;} return 0;
void input2()
}; }
{
class base2 cout<<"enter the values of derive class"<<endl;
{ cin>>a>>b;
private: }
int a,b,c; void show2(){
c=a*b;
cout<<"product"<<c<<endl;}
};
Hierarchical inheritance: A class which contains only one base class and multiple derive class but each derive
class can access base class is called hierarchical inheritance.
#include<iostream>
using namespace std; void show1(){ int main(){
class base cout<<"b"<<" "<<b<</*"sum"<<a+c<<*/endl;} derive1 ob1;
{ }; derive2 ob2;
private: class derive2:public derive1 ob1.input();
int a,c; { ob1.input1();
public: private: ob1.show();
void input() int y; ob1.show1();
{ public:
cout<<"enter the values of base class"<<endl; void input2() ob2.input();
cin>>a>>c; { ob2.input2();
} cout<<"enter the values of derive 2 class"<<endl; ob2.show();
void show(){ cin>>y; ob2.show2();
cout<<"sum"<<a+c<<endl;} } return 0;
}; void show2(){ }
class derive1:public base cout<<"y"<<" "<<y/*<<"sum"<<a+c*/<<endl;}
{ };
private:
int b; Note:
public: 1. Try to access the function of base class by derive1 and derive2 class.
void input1() 2. Try to display the show function of base in derive 1 and derive 2
{
cout<<"enter the values of derive 1 class"<<endl;
cin>>b;}
Hybrid Inheritance: It is the combination of more than one type of inheritance is called hybrid
inheritance.
#include<iostream>
int main(){
using namespace std;
A a; B b; C c; D d;
class A
a.input();
{
a.show();
private:
b.input();
int a;
b.show();
public:
c.input();
void input()
c.show();
{
/*d.input();
cout<<"enter values"<<endl;
d.show();*/
cin>>a;
return 0;
}
}
void show(){
cout<<"a"<<" "<<a<<endl;}
};
class B:/*virtual*/ public A{};
class C:/*virtual*/ public A{};
class D:public B, public C{};
Exception handling
• An exception is unexpected/unwanted/abnormal situation that occurs
at runtime
• C++ provides a try-catch-block for handling exceptions
Try
{
--------//put risky code that might throw an exception.
}catch(exceptiontype e){//code to handle the exception.
}

You might also like