Implement a simple graphics system consisting of classes to represent different
ID: 661647 • Letter: I
Question
Implement a simple graphics system consisting of classes to represent different geometric shapes such as rectangles, squares, and circles. Specifically, a rectangle has the following data members: height, width, and center point. A square has center point and edge width. Finally, a circle has center and radius. These three classes (e.g., rectangle) are all derived from a common ancestor class Figure.
Implement the Rectangle and Triangle classes as derived classes of the base class Figure.
Each of these three classes (i.e., Figure, Rectangle, and Triangle) has two member functions, namely, erase() and draw(), which if properly implemented erase or draw a figure, respectively. You don't need to implement the actual drawing or erasing. It will suffice to just print out a message to indicate which version of the erase() or draw() function being called at run time. For example, you can print a message like "Calling Figure::erase()".
The base class Figure has three member functions: (1) draw(), which doesn't do anything other than print a message to identify itself when it is being called; (2) erase(), which again doesn't do anything other than print a message to identify itself when being called; and (3) center(), which simply calls the erase() and draw() functions and prints a message indicating that it is being called.
part I Implement the above three classes using separate compilation without including any virtual functions. Compile and then test your classes using the following test drive:
Part II Make the draw() and erase() functions in the base class Figure virtual. Compile the project and test using the above test drive again.
Part III Observe and describe the differences in the outputs generated by Part I and Part II. Next explain why virtual functions can lead to such differences.
Explanation / Answer
Answer:
I.
class Figure {
Figure f=new Figure();
public:
draw( )
{
cout << "Calling Figure::draw() :" <<endl;
}
erase()
{
cout << "Calling Figure::erase() :" <<endl;
return 0;
}
center(){
f.draw();
f.erase();
}
}
class Rectangle: public Figure{
Rectangle r=new Rectangle();
public:
draw( )
{
cout << "Calling Rectangle::draw() :" <<endl;
}
erase()
{
cout << "Calling Rectangle::erase() :" <<endl;
return 0;
}
center(){
r.draw();
r.erase();
}
}
class Triangle: public Figure{
Triangle t=new Traingle();
public:
draw( )
{
cout << "Calling Triangle::draw() :" <<endl;
}
erase()
{
cout << "Calling Triangle::erase() :" <<endl;
return 0;
}
center(){
t.draw();
t.erase();
}
}
#include < iostream >
#include "figure.h"
#include "rectangle.h"
#include "triangle.h"
using std::cout;
int main()
{
Triangle tri;
tri.draw();
cout << " Derived class Triangle object calling" << " center(). ";
tri.center();
Rectangle rect;
rect.draw();
cout << " Derived class Rectangle object calling" << " center(). ";
rect.center();
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------
II
class Figure {
Figure f=new Figure();
public:
draw( )
{
cout << "Calling Figure::draw() :" <<endl;
}
erase()
{
cout << "Calling Figure::erase() :" <<endl;
return 0;
}
center(){
f.draw();
f.erase();
}
}
class Rectangle: public Figure{
Rectangle rect=new Rectangle();
public:
virtual draw( ) /* Virtual function */
{
cout << "Calling Rectangle::draw() :" <<endl;
}
virtual erase() /* Virtual function */
{
cout << "Calling Rectangle::erase() :" <<endl;
return 0;
}
center(){
rect.draw();
rect.erase();
}
}
class Triangle: public Figure{
Triangle tri=new Traingle();
public:
virtual draw( ) /* Virtual function */
{
cout << "Calling Triangle::draw() :" <<endl;
}
virtual erase() /* Virtual function */
{
cout << "Calling Triangle::erase() :" <<endl;
return 0;
}
center(){
tri.draw();
tri.erase();
}
}
#include < iostream >
#include "figure.h"
#include "rectangle.h"
#include "triangle.h"
using std::cout;
int main()
{
Figure *f;
Triangle tri;
f=&tri; /* calls draw() of class derived tri */
f->draw();
cout << " Derived class Triangle object calling" << " center(). ";
tri.center();
Rectangle rect;
f=▭ /* calls draw() of class derived rect */
f->draw();
cout << " Derived class Rectangle object calling" << " center(). ";
rect.center();
return 0;
}
----------------------------------------------------------------------------------------------------------------------
III
Without "virtual" you get "early binding". Which implementation of the method is used gets decided at compile time based on the type of the pointer that you call through.
With "virtual" you get "late binding". Which implementation of the method is used gets decided at run time based on the type of the pointed-to object
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.