C++ Program For Virtual Function
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Program for Virtual Function
#include<iostream>
#include<string.h>
#define clrscr() system("cls")
using namespace std;
class Base // Abstract class
{
public:
void virtual display()=0;
};
class Derived:public Base
{
public:
void display()
{
cout<<"\n display method of derived class";
}
};
class Derived1:public Base
{
public:
void display()
{
cout<<"\n display method of derived 1 class";
}
};
int main()
{
clrscr();
Base *p;
Derived D;
p=&D;
p->display();
// Derived D,*p; // Both object and pointer of derived class;
// p=&D;
// p->display();
// Base B,*p; // Both object and pointer of base class;
// p=&B;
// p->display();
return 0;
}Program 2
// Program for Virtual Function
#include<iostream>
#include<string.h>
#define clrscr() system("cls")
using namespace std;
class Button //Abstract Class
{
public:
void virtual click()=0;
void virtual dbclick()=0;
};
class Color:public Button //Programmer 1
{
public:
void click()
{
cout<<"\nNow color is red........ click";
}
void dbclick()
{
cout<<"\nNow color is blue.......double click";
}
};
class Image:public Button //Programmer 2
{
public:
void click()
{
cout<<"\nThis is My Click Image.....";
}
void dbclick()
{
cout<<"\nThis is My duble Click Image.....";
}
};
int main()
{
clrscr();
Button *p; // Reference Object
Color C;
p=&C;
p->click();
p->dbclick();
}
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

