C++ Program to implement Virtual Functions – Q24
Q24. Program to implement Virtual Functions:
Draw the various geometrical figures using the concept of virtual functions along with following class hierarchy:
The base class shapes should contain a virtual function draw().
All the derived classes should contain the function to calculate and display the area of the respective figure.
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <graphics.h> #include <stdlib.h> #include <conio.h> #include <math.h> class Cshapes{ protected: int x1, y1; public: Cshapes(){} virtual void Draw() = 0; virtual void Area() = 0; }; class Cline : public Cshapes{ private: int x2, y2; public: Cline(){} Cline(int _x1, int _y1, int _x2, int _y2){ x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; } void Draw(){ line(x1, y1, x2, y2); } void Area(){ cout<<"\n No Area, its a Line."; } }; class Ctrng : public Cshapes{ private: int x2, y2, x3, y3; public: Ctrng(){} Ctrng(int _x1, int _y1, int _x2, int _y2, int _x3, int _y3){ x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; x3 = _x3; y3 = _y3; } void Draw(){ line(x1, y1, x2, y2); line(x2, y2, x3, y3); line(x3, y3, x1, y1); } void Area(){ unsigned int s1, s2, s3; float s, ar; s1 = sqrt( pow((x2-x1), 2) + pow((y2-y1), 2) ); s2 = sqrt( pow((x3-x2), 2) + pow((y3-y2), 2) ); s3 = sqrt( pow((x3-x1), 2) + pow((y3-y1), 2) ); s = (s1+s2+s3)/2; ar = sqrt(s * (s-s1) * (s-s2) * (s-s3)); gotoxy(1, 24); cout<<" Area of Triangle: "<<ar; } }; class Crect : public Cshapes{ private: int x2, y2; public: Crect(){} Crect(int _x1, int _y1, int _x2, int _y2){ x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; } void Draw(){ line(x1, y1, x1, y2); line(x1, y2, x2, y2); line(x2, y2, x2, y1); line(x2, y1, x1, y1); } void Area(){ int ln, bd; unsigned long ar; ln = abs(x2 - x1); bd = abs(y2 - y1); ar = ln * bd; gotoxy(1, 24); cout<<" Area of Rectangle: "<<ar; } }; void main(){ int ch, _x1, _y1, _x2, _y2, _x3, _y3; int gd=DETECT, gm; initgraph(&gd, &gm, "\\tc"); Cline Oline; Ctrng Otrng; Crect Orect; while(1){ clrscr(); cleardevice(); cout<<"\n 1 -> LINE"; cout<<"\n 2 -> TRIANGLE"; cout<<"\n 3 -> RECTANGLE"; cout<<"\n 4 -> EXIT"; cout<<"\n Enter your choice: "; cin>>ch; switch(ch){ case 1: // LINE. cout<<"\n Enter 1st Cordinates: "; cin>>_x1>>_y1; cout<<"\n Enter 2nd Cordinates: "; cin>>_x2>>_y2; Cline Oline(_x1, _y1, _x2, _y2); clrscr(); cleardevice(); Oline.Draw(); Oline.Area(); break; case 2: // TRIANGLE. cout<<"\n Enter 1st Cordinates: "; cin>>_x1>>_y1; cout<<"\n Enter 2nd Cordinates: "; cin>>_x2>>_y2; cout<<"\n Enter 3rd Cordinates: "; cin>>_x3>>_y3; clrscr(); cleardevice(); Ctrng Otrng(_x1, _y1, _x2, _y2, _x3, _y3); Otrng.Draw(); Otrng.Area(); break; case 3: // RECTANGLE. cout<<"\n Enter 1st Cordinates: "; cin>>_x1>>_y1; cout<<"\n Enter 2nd Cordinates: "; cin>>_x2>>_y2; clrscr(); cleardevice(); Crect Orect(_x1, _y1, _x2, _y2); Orect.Draw(); Orect.Area(); break; case 4: // EXIT closegraph(); exit(1); default: cout<<"\n Enter Appropriate Choice."; } // end of switch. getch(); } // end of while. } // end of main.
Output:
DRAW – MAIN MENU
1 -> LINE
2 -> TRIANGLE
3 -> RECTANGLE
4 -> EXIT
Enter your choice: 1
Enter 1st Cordinate: 100 100
Enter 2nd Cordinate: 300 400
No Area, its a Line.
Categories: Cpp
Cpp, Virtual Function
Comments (0)
Trackbacks (0)
Leave a comment
Trackback