You are on page 1of 23

CE142 Runtime Polymorphism & Virtual

Function
Virtual Function
#include<iostream>
using namespace std; 
class Base
{
public:
virtual void show() { cout<<" In Base \n"; }
};

class Derived: public Base


{
public:
void show() { cout<<"In Derived \n"; }
};
int main(void)
{
Base *bp = new Derived;
bp->show(); // RUN-TIME POLYMORPHISM
In Derived
return 0;
}
Virtual Function

 class A{ &f2() &f3() &f4()
 void f1(); _vtable  A *p;
 virtual void f2();  A x1;
 virtual void f3(); x1  p=&x1;
 virtual void f4(); p
_vptr  p->f1(); //EB
 };
 p->f2(); //LB
 class B:public A
 p->f3(); //LB
 { &f2() &f3() &f4()
 p->f4(); //LB
 void f2();
_vtable  p->f4(10); //EB
 void f4(int x);
 void f1();  !!Error
 }
Virtual Function

 class A{ &f2() &f3() &f4()
 void f1(); _vtable  A *p;
 virtual void f2();  B x1;
 virtual void f3(); x1  p=&x1;
 virtual void f4(); p
_vptr  p->f1(); //EB
 };
 p->f2(); //LB
 class B:public A
 p->f3(); //LB
 { &f2() &f3() &f4()
 p->f4(); //LB
 void f2();
_vtable  p->f4(10); //EB
 void f4(int x);
 void f1();
 }
Can static functions be virtual in C++?

#include<iostream>

using namespace std;

class Test
{
public:
// Error: Virtual member functions cannot be static
virtual static void fun() { }
};
Virtual Function & Default
Arguments

#include <iostream>
using namespace std;
class Base
{
public:
virtual void fun ( int x = 0 )
{
cout << "Base::fun(), x = " << x << endl;
}
};
class Derived : public Base
{
public:
void fun ( int x )
{
cout << "Derived::fun(), x = " << x << endl;
"Derived::fun(), x =0
}
};

int main()
{
Derived d1; Base *b = &d1; b->fun();
return 0;
}
Virtual Function & Default
Arguments
#include <iostream>


using namespace std;
class Base
{
public:
virtual void fun ( int x = 0 )
{
cout << "Base::fun(), x = " << x << endl;
}
};
class Derived : public Base
{
public:
void fun ( int x=10 ) //Note The Changes
{
cout << "Derived::fun(), x = " << x << endl;
} "Derived::fun(), x =0
};

int main()
{
Derived d1; Base *bp = &d1; bp->fun();
return 0;
}
Can virtual functions be private in C++?

Never.

Why?
Abstract Classes in C++


 No Implementation of function in base
class is referred as Abstract class
Example: class Shape having member
function draw().
It can only be have implementation at
derived classes. i.e. draw() in rectangle,
draw() in square.
Example: class Animal having member
function move().
Abstract class

 A class which does not have an implementations of
the function.
 How to create abstract class in java.
 By putting abstract keyword before class definition
 How to create abstract class in C#
 By putting abstract keyword before class definition
 How to create abstract class in C++
 By putting abstract keyword before class definition
 There is no abstract keyword in C++!!!!!
Abstract class in C++

 How to create abstract class in C++
 A class in C++ is abstract if it has at least one pure
virtual function.
 What is pure virtual function?
 It is a kind of virtual function which has no
implementations!!!

 Error: Undefined reference to vtable for Sha


 How to make any function as pure virtual
function
Abstract class in C++

 How to create abstract class in C++
 A class in C++ is abstract if it has at least one pure
virtual function.
 What is pure virtual function?
 It is a kind of virtual function which has no
implementations!!!
 How to make any virtual function as pure
virtual function?
 By putting “=0” expression at function declaration.

Characteristics of Abstract
class in C++

1. Abstract class may also contain non virtual
functions.


2. Instances of the abstract class can not be created.


3. We can have pointers and references of abstract
class type.


4. If we do not override the pure virtual function in
derived class, then derived class also becomes
abstract class.


5. An abstract class can have constructors.


Operator in C++

Example


Example

You might also like