Wednesday, 29 April 2026

POLYMORPHISM WITH POINTERS in C++


POLYMORPHISM WITH POINTERS in C++
Pointers are also central to polymorphism in C++. To enable polymorphism, C++ allows a pointer in a base class to point to either a base class object or to any derived class object. The following program segment illustrates how a pointer is assigned to point to the object of the derived class.

class baseA {
-------
-------
};
class derivedD : public baseA
{ -------
-------
};
int main()
{
baseA *ptr;// pointer to baseA
derivedD objd;
ptr = &objd; //indirect reference objd to the pointer
-------
-------
return 0;
}
The pointer ptr points to an object of the derived class objd.

The following program segment illustrates how a different assignment of a pointer to an object of a base class be given to an object of a derived class.

(1) A base class pointer can point to the object of the same class or a derived class.
class baseA {
-------
-------
};
class derivedD: public baseA
{
-------
-------
};
int main()
{
baseA obja;
derivedD objd;
baseA *ptr;
ptr = &obja; //valid, pointer to a same class
ptr = &objd; //valid, pointer to a derived class
-------
-------
return 0;
}
(2) A derived class pointer cannot point to an object of a base class but it can point to the same class
object.
class baseA {
-------
-------
};
class derivedD: public baseA
{
-------
-------
};
int main()
{
baseA obja;
derivedD objd;
derivedD *ptr;
ptr = &obja; //error, cannot point to base class object
ptr = &objd; //valid,pointer to a same class object
-------
-------
return 0;
}
Note that a derived class pointer cannot point to an object of a base class.

Example 1.
A program to illustrate how to assign the pointer of the derived class to the object of a base class using explicit casting.

//demonstration of run time binding
//pointer of the derived class points to a base class object
//using explicit casting
#include <iostream>
using namespace std;
class square {
protected:
int x;
public:
virtual void getdata();
virtual void display();
virtual int area();
};
class rectangle: public square
{
protected:
int y;
public :
void getdata();
void display();
int area();
};
void square:: getdata()
{
cout << “ enter the value of side x? \n”;
cin >> x;
}
void square:: display()
{
cout << “ value of x = y = ” << x << endl;
cout << “ Area of the square = ” << area();
cout << endl;
}
int square :: area()
{
int temp = x*x;
return(temp);
}
void rectangle :: getdata()
{
cout << “ enter the value of sides x and y ? \n”;
cin >> x >> y;
}
void rectangle:: display()
{
cout << “ value of x = ” << x << “ and y = ” << y << endl;
cout << “ Area of the rectangle = ” << area();
cout << endl;
}
int rectangle :: area()
{
int temp = x*y;
return(temp);
}
int main()
{
square sqobj;
rectangle *ptr; //pointer of the derived class
ptr = (rectangle*)&sqobj; //explicit casting
ptr->getdata();
ptr->area();
ptr->display();
return 0;
}
Output of the above program
enter the value of side x?
value of x = y = 12
Area of the square = 144

VIRTUAL FUNCTIONS
A virtual function is one that does not really exist but it appears real in some parts of a program. Virtual functions are advanced features of the object-oriented programming concept and they are not necessary for each C++ program. This section presents how the polymorphic features are incorporated using the virtual functions.

The general syntax of the virtual function declaration is:
class user_defi ned_name {
private:
-------
-------
public:
virtual return_type function_name1(arguments);
virtual return_type function_name2(arguments);
virtual return_type function_name3(arguments);
-------
-------
};
To make a member function virtual, the keyword virtual is used in the methods while it is declared in the class defi nition but not in the member function defi nition. The keyword virtual should be preceded by a return type of the function name. The compiler gets information from the keyword virtual that it is a virtual function and not a conventional function declaration.

Some of the invalid declarations of the virtual functions in C++ are given below:
(1) The keyword virtual should not be repeated in the definition if the defi nition occurs outside the class declaration. The use of a function specifi er virtual in the function defi nition is invalid.
class sample {
private :
int x;
fl oat y;
public :
virtual void display();
};
virtual void sample:: display() //error
{
-------
-------
}
(2) A virtual function cannot be a static member because a virtual member is always a member of a particular object in a class rather than a member of the class as a whole.
class sample {
private:
int x;
fl oat y;
public:
virtual static int sum(); //error, due to static
};
int sample:: sum()
{
-------
-------
}
(3) A virtual function cannot have a constructor member function but it can have the destructor member function.
class sample {
private:
int x;
fl oat y;
public:
virtual sample (int xx, fl oat yy); //constructor
void getdata();
void display();
};
Note that it is an error to make a constructor virtual type.

(4) A destructor member function does not take any argument and no return type can be specifi ed for it not even void.
class sample {
private :
int x;
fl oat y;
public :
virtual ~ sample (int xx, fl oat yy); //invalid
void getdata();
void display();
};
Note that the destructor member function can be virtual even though it does not take any argument.

(5) It is an error to redefine a virtual method with a change of return data type in the derived class with the same parameter types as those of a virtual method in the base class.
class baseA {
private :
int x;
fl oat y;
public:
virtual int sum (int xx,fl oat yy); //error
};
class derivedD: public baseA {
private:
int z;
public:
virtual fl oat sum (int xx,fl oat yy);
};
The above declarations of two virtual functions are invalid due to the different type of return value. Even though these functions take identical arguments, the return data types are different.

The following manner, one can correct the above said error
virtual fl oat sum (int xx,fl oat yy); // base class
virtual fl oat sum (int xx,fl oat yy); // derived class

Both the above functions can be written with int data types in the base class as well as in the derived class as
virtual int sum (int xx,fl oat yy); // base class
virtual int sum (int xx,fl oat yy); // derived class

(6) Only a member function of a class can be declared as virtual. It is an error to declare a non-member function (non-method) of a class virtual.
virtual void display() //error, nonmember function
{
-------
-------
}

0 comments

Post a Comment