Aim :Consider the following class definition: class father { protected age; public; father (int x) {age = x;} virtual void iam() { cout<<“I AM THE FATHER ” ; cout<<“My age is:” << age << endl; } }; Derive the two classes son and daughter from the above class and for each, define iam () to write similar but appropriate messages. You should also define suitable constructors for these classes. Now, write a main () that creates objects of the three classes and then calls iam( ) for them. Declare a pointer to father. Successively, assign addresses of objects of the two derived classes to this pointer and in each case, call iam () through the pointer to demonstrate polymorphism in action.

Theory

1. Virtual Functions :

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.

Rules for Virtual Functions

1.Virtual functions cannot be static.
2.A virtual function can be a friend function of another class.
3.Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime polymorphism.
4.The prototype of virtual functions should be the same in the base as well as the derived class.
5.They are always defined in the base class and overridden in a derived class. It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used.
6.A class may have a virtual destructor but it cannot have a virtual constructor.

2.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 in C++. In this article, we will learn about inheritance in C++, its modes and types along with the information about how it affects different properties of the class.
Syntax:
class derived_class_name : access-specifier base_class_name
{
// body ....
};

Multiple Inheritance in C++

Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B’s constructor is called before A’s constructor. A class can be derived from more than one base class.
Syntax: class A
{
... .. ...
};
class B
{ ... .. ...
};
class C: public A,public B {
... ... ...
};
Program :

Conclusion : Hence we have performed our practical successfully