VIRTUAL FUNCTIONS

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>

//base class – abstract class

class shape
{
protected:
int x,y;
public:
//get x,y coordinates
void getxy()
{
}
//virtual function
virtual void draw()=0;
};

//child class
class Line:public shape
{
private:
int x1,y1;
public:
void get()
{
//read x1,y1
}
void draw()
{
line(x,y,x1,y1);
}
};

//child class
class Circle:public shape
{

//Declare a private data member radius
public:
//Define a function to read the radius






void draw()
{
circle(x,y,r);
}
};


class Rect:public shape
{

//Declare a private member for width
public:
//Define a function to read width
void draw()
{
rectangle(x-w,y-w,x+w,y+w);
}
};

void main()
{
int gd=DETECT ,gm,ch;
initgraph(&gd,&gm,"s:\\appl\\tcplus\\include");
//create pointer of the base class
shape *ptr;
do
{
cout<<"Shapes Menu"<<endl;
cout<<"1.Line"<<endl;
cout<<"2.Circle"<<endl;
cout<<"3.Rectangle"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
clrscr();
//create an object of class line
//call the appropriate methods to read the input values
//initializing ptr to line object
ptr=&l;
clrscr();
setcolor(4);
//calling draw function of line class
ptr->draw();
getch();
break;
case 2:
//for circle class
break;
case 3:
//for rectangle class
break;
case 4:
exit(0);
}
cout<<"press any key to continue";
getch();
clrscr();
}
while(1);
}

0 comments: