1. Program to demonstrate inheritance and polymorphism using virtual functions.
#include <iostream.h>
#include <graphics.h>
#include<conio.h>
class Shape {
public:
virtual void draw() = 0;
};
class Line : public Shape {
public:
void draw() {
cout << "Drawing a Line" << endl;
// Draw a line
line(100, 100, 100, 200);
}
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing a circle" << endl;
// Draw a circle
circle(200, 200, 50);
}
};
class Rectangle : public Shape {
public:
void draw() {
cout << "Drawing a rectangle" << endl;
// Draw a rectangle
rectangle(100, 250, 300, 350);
}
};
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
Shape *shapePtr;
Line line;
Circle circle;
Rectangle rectangle;
shapePtr = &line;
shapePtr->draw();
getch();
shapePtr = &circle;
shapePtr->draw();
getch();
shapePtr = &rectangle;
shapePtr->draw();
getch();
closegraph();
return 0;
}