You are on page 1of 10

CSC241: OBJECT ORIENTED

PROGRAMMING
VIRTUAL FUNCTIONS
class Shape {. // Abstract class
protected:
float dimension;
public:
4
void getDimension() {
cin >> dimension;}
virtual float calculateArea() = 0; }; // pure virtual Function
class Square : public Shape {
public:
float calculateArea() {
return dimension * dimension; }};
class Circle : public Shape {
public:
float calculateArea() {
return 3.14 * dimension * dimension; }};

int main() {
Square square;
Circle circle;
cout << "Enter the length of the square: ";
square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;
cout << "\nEnter radius of the circle: ";
circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;
return 0;}
class Add{
public:
virtual void print ()
{ int a=20, b=30;
5 " base class Action is:"<<a+b <<endl; }
cout<<
void show ()
{ cout<< "show base class" <<endl; }
};
class Sub: public Add {
public:
void print ()
{ int x=20,y=10;
cout<< " derived class Action:"<<x-y <<endl; }
void show ()
{ cout<< "show derived class" <<endl; } };
int main() {
Add *aptr;
Sub s;
aptr = &s;
//virtual function, binded at runtime (Runtime polymorphism)
aptr->print();

// Non-virtual function, binded at compile time


aptr->show();
class GFG_Base {
public:
virtual void display(){
6 cout << "Called virtual Base Class function" << "\n\n"; }
void print()
{
cout << "Called GFG_Base print function"
<< "\n\n";
}};
class GFG_Child : public GFG_Base {
public:
void display(){
cout << "Called GFG_Child Display Function"
<< "\n\n";
}
void print(){
cout << "Called GFG_Child print Function"
<< "\n\n";
}};
int main()
{
GFG_Base* base;
GFG_Child child;
base = &child;
base->GFG_Base::display();
base->print();
class base {
public:
7virtual void print() {
cout << "print base class" << endl; }
void show() { cout << "show base class" << endl; }};
class derived : public base {
public:
void print() { cout << "print derived class" << endl; }
void show() { cout << "show derived class" << endl; }};
int main()
{
base* bptr;
derived d;
bptr = &d;

bptr->print();

bptr->show();

return 0;
}
9 VIRTUAL FUNCTION

• A virtual function is declared by keyword virtual.


• The return type of virtual function may be int, float, void.
• A virtual function is a member function in the base class.
• It can redefine it in a derived class.
• It is part of run time polymorphism.
• A virtual function is not static.
• When declare a virtual function, the compiler determines which function to invoke at runtime.
• When refer to a derived class object using a pointer or a reference to the base class,
can call a virtual function for that object and execute the derived class’s version of
the function.
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 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
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 virtual destructor but it cannot have a virtual constructor.

You might also like