Loading....
Coupon Accepted Successfully!

 

Rules For Virtual Functions

  1. The virtual function should not be static and must be a member of a class.
  2. The virtual function may be declared as a friend for another class. An object pointer can access the virtual functions.
  3. A constructor cannot be declared as virtual, but a destructor can be declared as virtual.
  4. The virtual function should be defined in the public section of the class. It is also possible to define the virtual function outside the class. In such a case, the declaration is done inside the class, and the definition is outside the class. The virtual keyword is used in the declaration and not in the function declarator.
  5. It is also possible to return a value from virtual functions similar to other functions.
  6. The prototype of the virtual function in the base class and derived class should be exactly the same. In case of a mismatch, the compiler neglects the virtual function mechanism and treats these functions as overloaded functions.
  7. Arithmetic operations cannot be used with base class pointers.
  8. If a base class contains a virtual function and if the same function is not redefined in the derived classes, in such a case, the base class function is invoked.
  9. The operator keyword used for operator overloading also supports the virtual mechanism.

15.5 Write a program to declare virtual function and execute the same function defined in the base and derived class.

#include<iostream.h>

#include<conio.h>

class first

{

int b;

public:

first() {b=10;}

virtual void display() {cout<<“\n b=” <<b;}

};

class second : public first

{

int d;

public:

second() {d=20;}

void display() {cout<<“\n d=”<<d;}

};

int main()

{

clrscr();
first f,*p;
second s;
p=&f;
p->display();
p=&s;
p->display();
return 0;

}

OUTPUT
b = 10
d = 20


Explanation: The above program is similar to the previous one. The only difference is that a virtual keyword precedes the display() function of the base class as per the statement virtual void display() {cout<<“\n b = <<b;}. The virtual keyword does the run-time binding. In the first call, the display() function of the base class is executed and in the second call, that is, after assigning the address of the derived class to pointer p, displays() function of the derived class is executed.
 

15.6 Write a program to use pointer for both base and derived class and call the member function. Use virtual keyword.

VIRTUAL FUNCTIONS

#include<iostream.h>

#include<conio.h>

class super

{

public:

virtual void display() {cout<<“\n In function display() class super”;}

virtual void show() {cout<<“\n In function show() class super”;}

}

;

class sub : public super

{

public:

void display() {cout<<“\nIn function display() class sub”;}

void show() {cout<<“\n In function show() class sub”;}

};

int main()

{

clrscr();
super S;
sub A;
super *point;
cout<<“\n Pointer point points to class super\n”;
point=&S;
point->display();
point->show();
cout<<“\n\n Now Pointer point points to derived class sub\n”;
point=&A;
point->display();
point->show();
return 0;

}

OUTPUT
Pointer point points to class super
In function display() class super
In function show() class super
Now Pointer point points to derived class sub
In function display() class sub
In function show() class sub

Explanation:
In the above program, the base class super and the derived class sub have member functions a similar name. They are display() and show(). In function main(), the variable S is an object of class super, and the variable A is an object of derivedclass sub. The pointer variable point is a pointer to the base class. The address of object S is assigned to the pointer S. The pointer calls both the member functions. Similarly, the variable A is an object of the derived class sub. The address of A is assigned to the pointer point and again, the pointer calls the member functions.
 
The member functions of the base class are preceded by the keyword virtual. If the virtual keyword is removed, in both the function calls, the member functions of the base class are executed. The member functions for the derived class are not executed, though the pointer has the address of the derived class. If the virtual keyword is not removed, firstly, the member function of the base class is executed, and then member function of derived class.




Test Your Skills Now!
Take a Quiz now
Reviewer Name