Experiment 11 - Inheritance, virtual classes and virtual functions
Experiment 11 - Inheritance, virtual classes and virtual functions
Theory:
In C++, inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. In such a way, you can reuse, extend or
modify the attributes and behaviors which are defined in other classes.
In C++, the class which inherits the members of another class is called derived class and
the class whose members are inherited is called base class. The derived class is the
specialized class for the base class.
Advantages of 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
● Single inheritance
● Multiple inheritance
● Hierarchical inheritance
● Multilevel inheritance
● Hybrid inheritance
Derived class
A Derived class is defined as the class derived from the base class.
Syntax:
}
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.
When the base class is privately inherited by the derived class, public members of the
base class become the private members of the derived class. Therefore, the public
members of the base class are not accessible by the objects of the derived class only
by the member functions of the derived class.
When the base class is publicly inherited by the derived class, public members of the
base class also become the public members of the derived class. Therefore, the public
members of the base class are accessible by the objects of the derived class as well as
by the member functions of the base class.
Single Inheritance
Where 'A' is the base class, and 'B' is the derived class.
PROGRAM
//241060029
#include <iostream>
using namespace std;
class B
{
int a;//private;cannot be inherited
public:
int b;
void set_ab();
int get_a();
void show_a();
};
void B :: set_ab()
{
a=5; b=10;
}
int B :: get_a()
{
return a;
}
void B :: show_a()
{
cout<<"a= "<<a<<endl;
}
void D :: mul()
{
c=b*get_a();
}
void D :: display()
{
cout<<"a= "<<get_a()<<endl;
cout<<"b= "<<b<<endl;
cout<<"c= "<<c<<endl;
}
int main()
{
D d;
d.set_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();
return 0;
}
Output
How to make a Private Member Inheritable?
The private member is not inheritable. If we modify the visibility mode by making it public,
this takes away the advantage of data hiding.
C++ introduces a third visibility modifier, i.e., protected. The member which is declared
as protected will be accessible to all the member functions within the class as well as the
class immediately derived from it.
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.
PROGRAM
//241060029
#include <iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number();
};
int main()
{
result student1;
student1.get_number(111);
student1.get_marks(75.0, 59.5);
student1.display();
return 0;
}
Output:
Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits the attributes
from two or more classes.
Syntax:
PROGRAM
//241060029
#include <iostream>
using namespace std;
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
void M :: get_m(int x)
{
m=x;
}
void N :: get_n(int y)
{
n=y;
}
void P :: display()
{
cout<<"m= "<<m<<endl;
cout<<"n= "<<n<<endl;
cout<<"m*n= "<<m*n<<endl;
}
int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}
Output:
Hybrid Inheritance
PROGRAM
//241060029
#include <iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number()
{
cout<<"Roll number = "<<roll_number<<endl;
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score()
{
cout<<"sports weightage: "<<score<<endl;
}
};
int main()
{
result student1;
student1.get_number(123);
student1.get_marks(55.5,67.3);
student1.get_score(6.0);
student1.display();
return 0;
}
Output:
Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than one class from a
base class.
Syntax:
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.
}
PROGRAM
//241060029
#include<iostream>
using namespace std;
class father //Base class declaration
{
int age;
char name [15];
public:
void get()
{
cout<< "Enter Father's name= ";
cin >> name;
cout<< "Enter Father's age= ";
cin >> age;
}
void show ( )
{
cout << "\n Father's name is : "<<name<<endl;
cout << "Father's age is: "<< age<<endl;;
}
};
son S1;
daughter D1 ;
S1. get ( ) ;
D1. get ( ) ;
S1 .show( ) ;
D1. show ( ) ;
}
Output:
Virtual functions
○ A C++ virtual function is a member function in the base class that you redefine in
a derived class. It is declared using the virtual keyword.
○ It is used to tell the compiler to perform dynamic linkage or late binding on the
function.
○ There is a necessity to use the single pointer to refer to all the objects of the
different classes. So, we create the pointer to the base class that refers to all the
derived objects. But, when the base class pointer contains the address of the
derived class object, it always executes the base class function. This issue can
only be resolved by using the 'virtual' function.
○ A 'virtual' is a keyword preceding the normal declaration of a function.
○ When the function is made virtual, C++ determines which function is to be
invoked at the runtime based on the type of the object pointed by the base class
pointer.
Let's see the simple example of C++ virtual function used to invoke the derived class in
a program.
//241060029
#include <iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number()
{
cout<<"Roll Number: "<<roll_number<<endl;
}
};
int main()
{
result student1;
student1.get_number(678);
student1.get_marks(73.5,86.0);
student1.get_score(7.0);
student1.display();
return 0;
}
Output:
○ A virtual function is not used for performing any task. It only serves as a
placeholder.
○ When the function has no definition, such function is known as "do-nothing"
function.
○ The "do-nothing" function is known as a pure virtual function. A pure virtual
function is a function declared in the base class that has no definition relative to
the base class.
○ A class containing the pure virtual function cannot be used to declare the objects
of its own, such classes are known as abstract base classes.
○ The main objective of the base class is to provide the traits to the derived classes
and to create the base pointer used for achieving the runtime polymorphism.
#include <iostream>
class Base
public:
virtual void show() = 0;
};
public:
void show()
std::cout << "Derived class is derived from the base class." << std::endl;
};
int main()
Base *bptr;
//Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
Output:
Problem statement
Create a base class called shape. Use this class to store two double type values that
could be used to compute the area of figures. Derive two specific classes called triangle
and rectangle from the base shape. Add to the base class, a member function
get_data() to initialize base class data members and another member function
display_area() to compute and display the area of figures.Make display_area() as a
virtual function and redefine this function in the derived classes to suit their
requirements.
#include<iostream>
class Shape
void get_data ()
cin>>a>>b;
virtual void display_area () = 0; //Shape is an abstract class as it’s a pure virtual function
};
};
};
int main()
Triangle t;
st->get_data();
st->display_area();
Rectangle r;
sr->get_data();
sr->display_area();
return 0;
Output:
Conclusion:
Thus, the study of Inheritance, virtual functions and virtual classes is complete.