.Your solution must include header, implementation file, and test files . In C++
ID: 3677171 • Letter: #
Question
.Your solution must include header, implementation file, and test files .
In C++ write a code to Consider a graphics system that has classes for various figures rectangles, squares, triangles, circles, and so on. For example, a rectangle might have data members for Height, Width and center point, while a square and circle might have only a center point and an edge length or radius.
In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system.
The class Figure is the base class. You should add only Rectangle and Triangle classes derived from Figure.
Each class has two member functions erase
draw
Each of the member functions erase and draw outputs a message telling what function has been called and what the class of the calling object is. For example if you call the function erase from the object of type Rectangle, the output is "this is the function Rectangle::erase()". (These functions do nothing more than output this message.)
The base class has another extra function named center. The member function center will also output a message like this: "the member function center is called to erase and draw". Then it will calls the erase and draw functions to erase and draw the figure. So basically this function just write a line in the output and then it will call erase and draw functions.
All the member functions should take no arguments.
There are three parts to this project:
Write the class definitions using no virtual functions. Compile and test. Make the base class member functions virtual. Compile and test .
Explain the difference in results .
Use the following main function for all testing (Remember to include necessary files before main):
int main() {
Triangle tri;
tri.draw();
cout << " In main, Derived class Triangle object calling"
<< " center(). "; tri.center();
Rectangle rect;
rect.draw();
cout << " In main, Derived class Rectangle object calling"
<< " center(). "; rect.center();
return 0; }
-7 files total triangle.h, triangle.cpp,figure.h,figure.cpp,rectangle.h,rectangle.cpp and int main(already given)
Explanation / Answer
//figure.h
#include <iostream>
using namespace std;
#ifndef PROTECT_FIGURE_H
#define PROTECT_FIGURE_H
class Figure
{
public:
virtual void erase();
virtual void draw();
void center()
{
cout << "member function center called " << endl;
erase();
draw();
}
};
void Figure::erase()
{
cout << "Figure::erase()" << endl;
}
void Figure::draw()
{
cout << "Figure::draw()" << endl;
}
#endif
//rectangle.h
#include <iostream>
#include "figure.h"
using namespace std;
class Rectangle : public Figure
{
public:
void erase();
void draw();
};
void Rectangle::erase()
{
cout << "Rectangle::erase() " << endl;
}
void Rectangle::draw()
{
cout << "Rectangle::draw() " << endl;
}
//triangle.h
#include <iostream>
#include "figure.h"
using namespace std;
class Triangle : public Figure
{
public:
void draw();
void erase();
};
void Triangle::draw()
{
cout << "Triangle::draw() " << endl;
}
void Triangle::erase()
{
cout << "Triangle::erase()" << endl;
}
//ch15p1App.cpp
#include <iostream>
#include "figure.h"
#include "rectangle.h"
#include "triangle.h"
using std::cout;
int main()
{
Triangle tri;
tri.draw();
cout << " In main, Derived class Triangle object calling"
<< " center(). ";
tri.center();
Rectangle rect;
rect.draw();
cout << " In main, Derived class Rectangle object calling"
<< " center(). ";
rect.center();
return 0;
}
A trial run follows:
-------------------------------
With Virtual erase and draw
-------------------------------
Triangle::draw()
In main, Derived class Triangle object calling center().
member function center called
Triangle::erase()
Triangle::draw()
Rectangle::draw()
In main, Derived class Rectangle object calling center().
member function center called
Rectangle::erase()
Rectangle::draw()
-------------------------------
Without Virtual erase and draw
-------------------------------
Triangle::draw()
In main, Derived class Triangle object calling center().
member function center called
Figure::erase()
Figure::draw()
Rectangle::draw()
In main, Derived class Rectangle object calling center().
member function center called
Figure::erase()
Figure::draw()
-------------------------------
Explanation of difference
-------------------------------
The differences are due solely to the presence or absence
of the keyword virtual in the base class figure.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.