OOP Using C++ – C++ Programming Polymorphism based Questions and Answers

Programming In C++ Long Questions and AnswersHere in this section of Programming In C++ Long Questions and Answers,We have listed out some of the important Long Questions with Answers on C++Programming Polymorphism which will help students to answer it correctly in their University Written Exam.

 

Lists of Long Descriptive type Questions that may be asked in this format in Written Exams.

  • 1. Write a short note on Polymorphism
  • 2. Explain pointers to objects with example.
  • 3. Explain ‘this’ pointer with example.
  • 4. Explain pointer to derived class
  • 5. Explain virtual function with example.
  • 6. Explain Run time polymorphism with example.
  • 7. Explain pure virtual functions.

 

Question – 1.  Write a short note on Polymorphism.

 

Polymorphism means the ability to take more than one form.

It allows a single name to be used for more than one related purpose.

It means ability of operators and functions to act differently in different situations.

 

Different types of polymorphism are

 

Polymorphism

 

  1. Compile time (Early Binding)
    • Function Overloading
    • Operator Overloading
  1. Run time (Late Binding)
  • Virtual Functions (Dynamic Binding)

 

Compile time:

Compile time polymorphism is function and operator overloading.

 

Function Overloading:

Function overloading is the practice of declaring the same function with different signatures.

The same function name will be used with different number of parameters and parameters of different type.

Overloading of functions with different return type is not allowed.

 

Operator Overloading:

Operator overloading is the ability to tell the compiler how to perform a certain operation based on its corresponding operator’s data type.

Like + performs addition of two integer numbers, concatenation of two string variables and works totally

different when used with objects of time class.

 

Dynamic Binding (Late Binding):

Dynamic binding is the linking of a routine or object at runtime based on the conditions at that moment.

It means that the code associated with a given procedure call is not known until the time of the call.

At run-time, the code matching the object under current reference will be called.

 

Virtual Function:

Virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes.

The whole function body can be replaced with a new set of implementation in the derived class.

It is declared as virtual in the base class using the virtual keyword.

 

 

Question – 2.  Explain pointers to objects with example.

 

  • A pointer can point to an object created by a class.
  • Object pointers are useful in creating objects at run time.
  • We can also use an object pointers to access the public members of an object.

 

For example,

item x;

item *ptr = &x;

Here pointer pter is initialized with the address of x.

x.display();

Above call is equivalent to,

ptr->show();

 

 

Example:

#include<iostream>

using namespace std;

class item

{

int code;

float price;

public:

void getdata(int a,float b)

{

code =a;

price=b;

}

void show(void)

{

cout<<”code”<<code<<”\n”:

cout<<”price”<<price<<”\n”;

}

};

const int size =2;

int main()

{

item *p =new item[size];

item *d = p;

int x, i;

float y;

for(i=0; i<size; i++)

{

cout<<”input code and price for item”<<i+1;

cin>>x>>y;

p->getdata(x,y);

p++;

}

for(i=0; i<size; i++)

{

cout<<”item:”<<i+1<<”\n”;

d->show();

d++;

}

return 0;

}

 

 

Question – 3. Explain ‘this’ pointer with example.

 

  • ‘this’ pointer represent an object that invoke or call a member function.
  • It will point to the object for which member function is called.
  • It is automatically passed to a member function when it is called.
  • It is also called as implicit argument to all member function.

 

For example: S.getdata();

Here S is an object and getdata() is a member function. So, ‘this’ pointer will point or set to the address of object S.

Suppose ‘a’ is private data member, we can access it only in public member function like as follows a=50;

We access it in public member function by using ‘this’ pointer like as follows this->a=50;

Both will work same.

 

Example:

#include <iostream>

using namespace std;

class sample

{

int a;

public:

sample()

{

a=10;

}

void disp(int a)

{

cout<<"The value of argument a="<<a;

cout<<"\nThe value of data member a="<<this->a;

}

};

int main()

{

sample S;

S.disp(20);

return 0;

}

 

Output:

The value of argument a=20

The value of data member a=10

 

The most important advantage of ‘this’ pointer is, If there is same name of argument and data member than you can differentiate it.

By using ‘this’ pointer we can access the data member and without ‘this’ we can access the argument in same function.

 

Question – 4. Explain pointer to derived class.

 

We can use pointers not only to the base objects but also to the objects of derived classes.

A single pointer variable can be made to point to objects belonging to different classes.

 

For example:

B *ptr //pointer to class B type variable

B b; //base object

D d; // derived object

ptr = &b; // ptr points to object b

 

 

In above example B is base class and D isa derived class from B, then a pointer declared as a pointer to B and point to the object b.

We can make ptr to point to the object d as follow ptr = &d;

We can access those members of derived class which are inherited from base class by base class pointer.

But we cannot access original member of derived class which are not inherited by base class pointer.

We can access original member of derived class which are not inherited by using pointer of derived class.

 

Example:

#include <iostream>

using namespace std;

class base

{

public:

int b;

void show()

{

cout<<"\nThe value of b"<<b;

}

};

class derived:public base

{

public:

int d;

void show()

{

cout<<"\nThe value of b="<<b

 <<"\nThe value of d="<<d;

}

};

int main()

{

base B;

derived D;

base *bptr;

bptr=&B;

cout<<"\nBase class pointer assign address of base class object";

bptr->b=100;

bptr->show();

bptr=&D;

bptr->b=200;

cout<<"\nBase class pointer assign address of derived class object";

bptr->show();

derived *dptr;

dptr=&D;

cout<<"\nDerived class pointer assign address of derived class object";

dptr->d=300;

dptr->show();

return 0;

}

 

 

Output:

Base class pointer assign address of base class object

The value of b100

Base class pointer assign address of derived class object

The value of b200

Derived class pointer assign address of derived class object

The value of b=200

The value of d=300

 

Question – 5. Explain virtual function with example.

 

  • It is a run time polymorphism.
  • Base class and derived class have same function name and base class pointer is assigned address of derived class object then also pointer will execute base class function.
  • To execute function of derived class, we have to declare function of base class as virtual.
  • To declare virtual function just uses keyword virtual preceding its normal function declaration.
  • After making virtual function, the compiler will determine which function to execute at run time on the basis of assigned address to pointer of base class.

 

Rules for virtual function

  1. The virtual functions must be member of any class.
  2. They cannot be static members.
  3. They are accessed by using object pointers.
  4. A virtual function can be a friend of another class.
  5. A virtual function in a base class must be defined, even though it may not be used.
  6. If two functions with the same name have different prototypes, C++ considers them as overloaded functions, and the virtual function mechanism is ignored.
  7. We cannot have virtual constructors, but we can have virtual destructors.
  8. The derived class pointer cannot point to the object of base class.
  9. When a base pointer points to a derived class, then also it is incremented or decremented only relative to its base type. Therefore we should not use this method to move the pointer to the next object.
  10. If a virtual function is defined in base class, it need not be necessarily redefined in the derived class. In such cases, call will invoke the base class.

 

Example:

#include <iostream>

using namespace std;

class base

{

public:

void disp()

{

cout<<"\nSimple function in base class";

}

virtual void show()

{

cout<<"\nVirtual function of Base class";

}

};

class derived: public base

{

public:

void disp()

{

 cout<<"\nSame name with simple function of base class in derived

class";

}

void show()

{

 cout<<"\nSame name with virtual function of base class in derived

class";

}

};

int main()

{

base B;

derived D;

base *bptr;

bptr=&B;

cout<<"\nBase class pointer assign address of base class object";

bptr->disp();

bptr->show();

bptr=&D;

cout<<"\nBase class pointer assign address of derived class object";

bptr->disp();

bptr->show();

return 0;

}

 

Output:

Base class pointer assign address of base class object

Simple function in base class

Virtual function of Base class

Base class pointer assign address of derived class object

Simple function in base class

Same name with virtual function of base class in derived class

 

 

Question – 6. Explain Run time polymorphism with example.

 

  • We must access virtual functions through the use of a pointer declared as a pointer to the base class.
  • We can also use object name and dot operator to call virtual functions.
  • But, runtime polymorphism using virtual functions is achieved only when a virtual function is accessed through a pointer to the base class.

 

Example:

#include<iostream>

#include<cstring>

using namespace std;

class media

{

protected:

char title[50];

float price;

public:

media(char *s, float a)

{

strcpy(title, s);

price = a;

}

virtual void display() { }

};

class book: public media

{

int pages;

public:

book(char *s, float a, int p):media(s, a)

{

pages = p;

}

void display();

};

class tape:public media

{

Float time;

Public:

Tape(char *s, float a, float t):media(s, a)

{

Time = t;

}

Void display();

};

void book::display()

{

cout<<”\nTitle:”<<title;

cout<<”\nPages:”<<pages;

cout<<”\nPrice:”<<pages;

}

void tape::display()

{

cout<<”\nTitle:”<<title;

cout<<”\nPlay time:”<<pages;

cout<<”\nPrice:”<<pages;

}

int main()

{

char * title=new char[30];

float price, time;

int pages;

// book details

cout<<”\n enter book details\n”;

cout<<”\n title: ”;

cin>>title;

cout<<”\n price: ”;

cin>>price;

cout<<”\n pages: ”;

cin>>pages;

book book1(title, price, pages);

cout<<”\n enter tape details \n”;

cout<<”Title: “;

cin>>title;

cout<<”Price: ”;

cin>>price;

cout<<”Play time (mins):”;

cin>>time;

tape tape1(title, price, time);

media* list[2];

list[0] = &book1;

list[1] = &tape1;

cout<<”\n MEDIA DETAILS”;

cout<<”\n ………BOOK………”;

list[0] -> display();

cout<<”\n …………TAPE………”;

list[i] -> display();

return 0;

}

 

Question – 7. Explain pure virtual functions.

 

  • A pure virtual function means ‘do nothing’ function.
  • We can say empty function. A pure virtual function has no definition relative to the base class.
  • Programmers have to redefine pure virtual function in derived class, because it has no definition in base class.
  • A class containing pure virtual function cannot be used to create any direct objects of its own.
  • This type of class is also called as abstract class.

 

Syntax:

virtual void display() = 0; OR virtual void display() {}