Sunday 25 September 2011



Hierarchal inheritance with virtual function and RTTI
Aim:
To write a C++ program to draw rectangle, square and circle using multiple inheritance with virtual function.
Algorithm:
Step 1: Create a class Point.
1.1 Declare the data members x and y.
1.2 Declare the member functions as
Point(int tempx,int tempy)
int getx()
int gety()
1.3 getx() function is used to return the value of x .
1.4 gety() function is used to return the value of y.
Step 2: Create a base class shape.
2.1 Declare the necessary data members.
2.2 Declare the member function as
virtual void draw()
Step 3: Create a derived class square. (Base class – shape)
3.1 Create an abstract class.
3.2 Get the sides of square.
3.3 Draw the square using draw() function.
Step 4: Create a derived class rectangle. (Base class – shape)
3.1 Create an abstract class.
3.2 Get the length and breadth of rectangle.
3.3 Draw the rectangle using draw() function.
Step 5: Create a derived class circle. (Base class – shape)
3.1 Create an abstract class.
3.2 Get the radius of circle.
3.3 Draw the circle using draw() function.
Step 6: In the main,
6.1 Create the objects for point class.
6.2 Create a base pointer object for shape class.
6.3 Call the draw() function to draw the square, rectangle and circle.
Program:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class Point
{
public:
int x;
int y;
Point(){}
Point(int tempX, int tempY)
{
x=tempX;
y=tempY;
}
int GetX()
{
return x;
}
int GetY()
{
return y;
}
friend ostream & operator<<(ostream & tempout, Point & tempPoint)
{
tempout<<"("<<tempPoint.GetX()<<","<<tempPoint.GetY()<<")";
return tempout;
}
};

class Shape
{
Point Position;
public:
Shape(){}
virtual void draw()
{
cout<<"shape is drawn";
}
};

class Square : public Shape
{
Point LeftBottom;
int Length;
public:
Square(){}
Square(Point tLeftBottom, int tLength)
{
LeftBottom=tLeftBottom;
Length=tLength;
}
void draw()
{
cout<<"Square is drwan at"<<LeftBottom<<"and with length as"<<Length<<"\n";
setcolor(14);
rectangle(LeftBottom.GetX(),LeftBottom.GetY(),LeftBottom.GetX()+Length,LeftBottom.GetY()+Length);
}
};
class Rectangles : public Shape
{
Point LeftBottom, LeftTop, RightBottom, RightTop;
public:
Rectangles(){}
Rectangles(Point tLeftBottom, Point tLeftTop, Point tRightBottom, Point tRightTop)
{
LeftBottom=tLeftBottom;
LeftTop=tLeftTop;
RightBottom=tRightBottom;
RightTop=tRightTop;
}
void draw()
{
cout<<"Rectangle is drawn at ("<<LeftBottom<<","<<RightBottom<<")"<<"and"<<")"<<LeftTop<<","<<RightTop<<")\n";
setcolor(4);
rectangle(LeftBottom.GetX(),LeftBottom.GetY(),RightTop.GetX(),RightTop.GetY());
}
};
class Circle : public Shape
{
Point Center;
int Radius;
public:
Circle(){}
Circle(Point tCenter, int tRadius)
{
Center=tCenter;
Radius=tRadius;
}
void draw()
{
cout<<"Circle is drawn at"<<" "<<Center<<" "<<"and the radius is"<<Radius<<"\n";
setcolor(5);
circle(Center.GetX(),Center.GetY(),Radius);
}
};
int main()
{
clrscr();
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "D:/Tc/BGI");

Point p1(100,200);
Point p2(50,50);
Point p3(100,300);

Square sq(p1,50);
Rectangles rect(p1,p2,p1,p2);
Circle c(p3,50);

Shape*s;
s=&sq; s->draw();getch();
s=&rect;s->draw();getch();
s=&c; s->draw();getch();

return 0;
}

Output:
Square is drawn at (100,200) and with length as 50
Rectangle is drawn  at  ((100,200), (100,200) ) and ( (50,50), (50,50) )
Circle is drawn at (100,300) and the radius is 50

0 comments:

Post a Comment