Â
In this program, we are going to Learn Runtime Polymorphism using Virtual Function in C++ Programming Language.Â
Â
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
class base
{
public:
virtual void print()
{
cout << "Base Class Print Function " << endl;
}
void show()
{
cout << "Base Class Show Function" << endl;
}
};
class derived : public base
{
public:
void print()
{
cout << "Derived Class Print Function " << endl;
}
void show()
{
cout << "Derived Class Show Function" << endl;
}
};
int main()
{
base *base_ptr;
derived der;
base_ptr = &der;
// virtual function, binded at runtime
base_ptr->print();
// Non-virtual function, binded at compile time
base_ptr->show();
return 0;
}
//.......Coded by RISHAB NAIR
The Code given below can be used in Turbo C++ Compilers: -
#include <iostream.h>
#include <conio.h>
class base
{
public:
virtual void print()
{
cout << "Base Class Print Function " << endl;
}
void show()
{
cout << "Base Class Show Function" << endl;
}
};
class derived : public base
{
public:
void print()
{
cout << "Derived Class Print Function " << endl;
}
void show()
{
cout << "Derived Class Show Function" << endl;
}
};
void main()
{
clrscr();
base *base_ptr;
derived der;
base_ptr = &der;
// virtual function, binded at runtime
base_ptr->print();
// Non-virtual function, binded at compile time
base_ptr->show();
getch();
}
//.......Coded by RISHAB NAIR
#ENJOY CODING
Â
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP