UNIT-III
Inheritance, Virtual Functions
& Polymorphism, Exception
Handling
Inheritance
• The capability of a class to derive properties and characteristics from
another class is called Inheritance. Inheritance is one of the most important
features of Object-Oriented Programming.
• Inheritance is a feature or a process in which, new classes are created from
the existing classes. The new class created is called “derived class” or
“child class” and the existing class is known as the “base class” or “parent
class”. The derived class now is said to be inherited from the base class.
• Sub Class: The class that inherits properties from another class is called
Subclass or Derived Class.
• Super Class: The class whose properties are inherited by a subclass is
called Base Class or Superclass.
Syntax:
class <derived_class_name> : <access-specifier> <base_class_name>
{
//body
}
Example:
1. class ABC : private XYZ //private derivation
{ }
2. class ABC : public XYZ //public derivation
{ }
3. class ABC : protected XYZ //protected derivation
{ }
4. class ABC: XYZ //private derivation by default
{ }
Types Of Inheritance
Single Inheritance
Single inheritance is defined as the inheritance in which a derived class
is inherited from the only one base class.
#include<iostream> void studisp() void marksdisp()
#include<conio.h> { {
using namespace std; cout<<"Roll Number: cout<<"Subject1 Marks:
"<<rolln<<endl; "<<sub1<<endl;
class student cout<<"Name: "<<name<<endl; cout<<"Subject2 Marks: "<<sub2;
{ }
protected: } };
int rolln; }; int main()
char name[50]; {
public: class result: public student result ob;
void studata() { ob.studata();
{ protected: ob.marks();
cout<<"Enter Student Roll int sub1, sub2; ob.studisp();
number: "; public: ob.marksdisp();
cin>>rolln; void marks() getch();
cout<<"Enter Student Name: "; { return 0;
cin>>name; cout<<"Enter subject1 marks: "; }
} cin>>sub1;
cout<<"Enter subject2 marks";
cin>>sub2;
}
Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits
the attributes from two or more classes.
#include<iostream> void studisp() class result: public student, int main()
#include<conio.h> { public faculty {
using namespace std; cout<<"Roll Number: { result ob;
"<<rolln<<endl; protected: ob.studata();
class student cout<<"Name: int sub1, sub2; ob.marks();
{ "<<name<<endl; public: ob.studisp();
protected: void marks() ob.marksdisp();
int rolln; } { ob.facshow();
char name[50]; }; cout<<"Enter subject1 getch();
public: class faculty marks: "; return 0;
void studata() { cin>>sub1; }
{ public: cout<<"Enter subject2
cout<<"Enter Student void facshow() marks";
Roll number: "; { cin>>sub2;
cin>>rolln; cout<<"Class Incharge: }
cout<<"Enter Student ABC"<<endl; void marksdisp()
Name: "; } {
cin>>name; }; cout<<"Subject1 Marks:
} "<<sub1<<endl;
cout<<"Subject2 Marks:
"<<sub2;
}
};
Multi Level Inheritance
• Multilevel inheritance is a process of deriving a class from another
derived class.
• 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.
#include<iostream> class C: public B{ int main()
using namespace std; protected: {
class A{ int c; C ob;
protected: public: ob.getdata_A();
int a; void getdata_C() ob.getdata_B();
public: { ob.getdata_C();
void getdata_A() cout<<"\n Enter the value of C: ob.sum();
{ ";
cout<<"Enter the value of A: "; cin>>c; return 0;
cin>>a; } }
}
}; void sum()
class B: public A{ {
protected: int sum=a+b+c;
int b; cout<<"\nSum= "<<sum;
public: }
void getdata_B() };
{
cout<<"\n Enter the value of B: ";
cin>>b;
}
};
Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than
one class from a base class.
#include<iostream> class C: public A{
using namespace std; public:
class A void showC()
{ {
public: cout<<"Class C"<<endl;
void showA() }
};
{
cout<<"Class A"<<endl;
} int main()
}; {
B ob;
class B: public A{ cout<<"Calling from B: ";
ob.showA();
public: ob.showB();
void showB()
{ C obj;
cout<<"Class B"<<endl; cout<<"Calling from C: ";
} obj.showA();
}; obj.showC();
return 0;
}
Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of
inheritance.
Inheritance Ambiguity in C++
In multiple inheritances, when one class is derived from two or more
base classes then there may be a possibility that the base classes have
functions with the same name, and the derived class may not have
functions with that name as those of its base classes. If the derived class
object needs to access one of the similarly named member functions of
the base classes then it results in ambiguity because the compiler gets
confused about which base’s class member function should be called.
#include<iostream> class C: public A, public B
using namespace std; {
public:
class A void input()
{ {
public: cout<<"\n This is class C";
void input() A::input();
{ B::input();
cout<<"\n This is class A"; }
} };
};
int main()
class B{ {
public: C obj;
void input() obj.input();
{ return 0;
cout<<"\n This is class B"; }
}
};
Virtual base class in C++
Virtual base classes are used in virtual inheritance in a way of
preventing multiple “instances” of a given class appearing in an
inheritance hierarchy when using multiple inheritances.
#include<iostream> class C: virtual public A int main()
using namespace std; { {
public: D ob;
class A void input_c() ob.show();
{ { ob.input();
public: cout<<"\n Class C"; ob.input_c();
void show() } ob.show_D();
{ }; return 0;
cout<<"Class A"; }
} class D: public B, public C
}; {
public:
class B: virtual public A void show_D()
{ {
public: cout<<"\n Class D";
void input() }
{ };
cout<<"\n Class B";
}
};
Overriding Member Function in C++
Function overriding in C++ is termed as the redefinition of base class
function in its derived class with the same signature i.e. return type and
parameters.
#include<iostream> int main()
using namespace std; {
B ob;
class A ob.show();
{ return 0;
public: }
void show()
{
cout<<"\n Class A";
}
};
class B: public A
{
public:
void show()
{
cout<<"\n Class B";
A::show();
}
};
Polymorphism
The word “polymorphism” means having many forms. In simple words,
we can define polymorphism as the ability of a message to be displayed
in more than one form. A real-life example of polymorphism is a person
who at the same time can have different characteristics. A man at the
same time is a father, a husband, and an employee. So the same person
exhibits different behavior in different situations.
Types of Polymorphism
Function Overloading
When there are multiple functions with the same name but different
parameters, then the functions are said to be overloaded, hence this is
known as Function Overloading. Functions can be overloaded
by changing the number of arguments or/and changing the type of
arguments.
#include<iostream>
using namespace std;
void add(int, int);
void add(int, int, int);
int main()
{
add(20, 30);
add(20, 30, 40);
return 0;
}
void add(int a, int b)
{
cout<<"Addition of two numbers are: "<<a+b<<endl;
}
void add(int a, int b, int c)
{
cout<<"Addition of three numbers are: "<<a+b+c;
}
Operator Overloading in C++
Operator overloading is a compile-time polymorphism. It is an idea of
giving special meaning to an existing operator in C++ without changing
its original meaning.
#include<iostream> int main()
using namespace std; {
class demo demo aa, bb, cc;
{ aa.getdata();
int a; bb.getdata();
public: cc=aa+bb;
void getdata() aa.putdata();
{ bb.putdata();
cout<<"Enter any value: "; cc.putdata();
cin>>a; return 0;
} }
void putdata()
{ OUTPUT
cout<<"The value is: "<<a<<endl;
} Enter any value: 3
demo operator+ (demo bb) Enter any value: 5
{ The value is: 3
demo cc; The value is: 5
cc.a=a+bb.a; The value is: 8
return cc;
}
};
Virtual Function in C++
A virtual function (also known as virtual methods) is a member function
that is declared within a base class and is re-defined (overridden) by a
derived class. When you refer to a derived class object using a pointer
or a reference to the base class, you can call a virtual function for that
object and execute the derived class’s version of the method.
#include<iostream> Class B: public A int main()
using namespace std; { {
class A void show() A *ptr;
{ { B ob;
public: cout<<"This is Derived Class ptr=&ob;
virtual void show() with Show function"<<endl; ptr->show();
{ ptr->print();
cout<<"This is Base Class with } return 0;
Show function"<<endl; void print() }
{
} cout<<"This is Derived Class
void print() with Print function"<<endl; OUTPUT:
{ } This is Derived Class with Show
cout<<"This is Base Class with }; function
Print function"<<endl; This is Base Class with Print
} function
};
Pure Virtual Functions
A pure virtual function (or abstract function) in C++ is a virtual function
for which we don’t have an implementation, we only declare it. A pure
virtual function is declared by assigning 0 in the declaration.
class Test {
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
Exception Handling in C++
• Exception Handling in C++ is a process to handle runtime errors. We
perform exception handling so the normal flow of the application can
be maintained even after runtime errors.
• In C++, exception is an event or object which is thrown at runtime.
C++ try and catch
• The try statement allows you to define a block of code to be tested for
errors while it is being executed.
• The throw keyword throws an exception when a problem is detected,
which lets us create a custom error.
• The catch statement allows you to define a block of code to be
executed if an error occurs in the try block.
• try {
// Block of code to try
throw exception; // Throw an exception when a
problem arise
}
catch () {
// Block of code to handle errors
}
#include<iostream> catch(int b)
using namespace std; {
int main() cout<<"Error: The second number is "<<b;
{ }
int a, b, c; return 0;
cout<<"Enter any two values: "; }
cin>>a>>b;
try{
if(b!=0)
{
c=a/b;
cout<<"The division of two
numbers:"<<c<<endl;
}
else{
throw b;
}
}
Multiple Catch Block
#include<iostream> catch(int x)
using namespace std; {
int main() cout<<"The value of x is: "<<x<<endl;
{ }
int x; catch(char x)
cout<<"Enter value of x: "; {
cin>>x; cout<<"The value of x is: "<<x<<endl;
try{ }
if(x==0) catch(float x)
throw(x); {
if(x==100) cout<<"The value of x is: "<<x;
throw('x'); }
if(x==1000) return 0;
throw(3.5); }
}
Single Catch Block
#include<iostream> catch(...)
using namespace std; {
int main() cout<<"Some erroe ocuured!!";
{ }
int x; return 0;
cout<<"Enter value of x: "; }
cin>>x;
try{
if(x==0)
throw(x);
if(x==100)
throw('x');
if(x==1000)
throw(3.5);