Atul 777
Atul 777
Syntax:
Class student
{
Public:
……
…....
Virtual return type function name (argument)
{
……….
…………
}
……….
};
Program
#include<iostream.h>
#include<conio.h>
Class base
{
Public:
void display()
{
Cout<< “\n display base”;
}
Class derived : public base
{
Public:
Void display()
{
Cout<<”\n display derived”;
}
Void show()
{
Cout<<”\n show derived”;
};
Int main()
{
Base b;
Derived d;
Base *bptr;
Cout<<”\n bptr points to base \n”;
}
bptr = &b;
bptr -> display(); //calls bqase version
bptr -> show(); // calls base version
cout<< “\n\n bptr points to derived\n”;
bptr =&d;
bptr -> display(); // calls base version
bptr -> show(); // calls derived version
return 0;
getch();
}
Abstract class
Abstract classes (classes with at least one virtual function) can be used as a framework
upon which new classes can be built to provide new functionality. A framework is a
combination of class libraries (set of cooperative classes) with predefined flow of control.
Abstract classes with virtual functions can be used as an aid to debugging. Suppose, it is
required to build a project consisting of a number of classes, possibly using a large
number of programmers.
Syntax:
Class student
{
Public:
…….
………
Virtual return type function name(argument)=0;
……….
…………
};
A pure virtual function declared in a base class has no implementation as far as the base
class is concerned.
The following are the properties of pure virtual functions:
1. A pure virtual function has no implementation in the base class hence, a class
with pure virtual function cannot be instantiated.
2. It acts as an empty bucket (virtual function is a partially filled bucket) that the
derived class is supposed to fill.
3. A pure virtual member function can be invoked by its derived class.