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
return 0;
}
In Derived

 class A{
 void f1();
 virtual void f2();
 virtual void f3();
 virtual void f4();
 };
 class B:public A
 {
 void f2();
 void f4(int x);
 void f1();
 }
Virtual Function
&f2() &f3() &f4()
_vtable
&f2() &f3() &f4()
_vtable
 A *p;
 A x1;
 p=&x1;
 p->f1();
 p->f2();
 p->f3();
 p->f4();
 p->f4(10);
 !!Error
//EB
//EB
//LB
//LB
//LB
p
x1
_vptr

 class A{
 void f1();
 virtual void f2();
 virtual void f3();
 virtual void f4();
 };
 class B:public A
 {
 void f2();
 void f4(int x);
 void f1();
 }
Virtual Function
&f2() &f3() &f4()
_vtable
&f2() &f3() &f4()
_vtable
 A *p;
 B x1;
 p=&x1;
 p->f1();
 p->f2();
 p->f3();
 p->f4();
 p->f4(10);
//EB
//EB
//LB
//LB
//LB
p
x1
_vptr

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;
}
};
int main()
{
Derived d1; Base *b = &d1; b->fun();
return 0;
}
"Derived::fun(), x =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;
}
};
int main()
{
Derived d1; Base *bp = &d1; bp->fun();
return 0;
}
"Derived::fun(), x =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().

 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

 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!!!
Abstract class in C++

 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

Virtual function in C++ Pure Virtual Function

  • 1.
    CE142 Runtime Polymorphism& Virtual Function
  • 2.
     Virtual Function #include<iostream> using namespacestd; 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 return 0; } In Derived
  • 3.
      class A{ void f1();  virtual void f2();  virtual void f3();  virtual void f4();  };  class B:public A  {  void f2();  void f4(int x);  void f1();  } Virtual Function &f2() &f3() &f4() _vtable &f2() &f3() &f4() _vtable  A *p;  A x1;  p=&x1;  p->f1();  p->f2();  p->f3();  p->f4();  p->f4(10);  !!Error //EB //EB //LB //LB //LB p x1 _vptr
  • 4.
      class A{ void f1();  virtual void f2();  virtual void f3();  virtual void f4();  };  class B:public A  {  void f2();  void f4(int x);  void f1();  } Virtual Function &f2() &f3() &f4() _vtable &f2() &f3() &f4() _vtable  A *p;  B x1;  p=&x1;  p->f1();  p->f2();  p->f3();  p->f4();  p->f4(10); //EB //EB //LB //LB //LB p x1 _vptr
  • 5.
     Can static functionsbe virtual in C++? #include<iostream> using namespace std; class Test { public: // Error: Virtual member functions cannot be static virtual static void fun() { } };
  • 6.
     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; } }; int main() { Derived d1; Base *b = &d1; b->fun(); return 0; } "Derived::fun(), x =0
  • 7.
     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; } }; int main() { Derived d1; Base *bp = &d1; bp->fun(); return 0; } "Derived::fun(), x =0
  • 8.
     Can virtual functionsbe private in C++? Never. Why?
  • 9.
     Abstract Classes inC++  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().
  • 10.
      A classwhich 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
  • 11.
      How tocreate 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!!! Abstract class in C++
  • 12.
      Error: Undefinedreference to vtable for Sha  How to make any function as pure virtual function
  • 13.
     Abstract class inC++  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.
  • 14.
  • 15.
  • 16.
     1. Abstract classmay also contain non virtual functions.
  • 17.
     2. Instances ofthe abstract class can not be created.
  • 18.
     3. We canhave pointers and references of abstract class type.
  • 19.
     4. If wedo not override the pure virtual function in derived class, then derived class also becomes abstract class.
  • 20.
     5. An abstractclass can have constructors.
  • 21.
  • 22.
  • 23.