Write a program for a graphics system that has classes for rectangles, squares,
ID: 646105 • Letter: W
Question
Write a program for a graphics system that has classes for rectangles, squares, circles and triangles. Each of these must be derived from a base class, Figure. You must include member functions of your choice that output a message telling what function has been called and what the class of the calling object is. Create two instances within your program:
Do the class definitions using no virtual functions.
Make the base class member functions virtual.
Answers in c++only.
please provide necessary comments.
Explanation / Answer
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;
}
----------------------------------------------------------------------------------------------------------------------------------------------------
//circle.h
#include <iostream>
#include "figure.h"
using namespace std;
class Circle : public Figure
{
public:
void draw();
void erase();
};
void Circle::draw()
{
cout << " Circle::draw() " << endl;
}
void Circle::erase()
{
cout << " Circle::erase()" << endl;
}
-------------------------------------------------------------------------------------------------------------------------------------------------
//All.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();
Circle cir;
cir.draw();
cout << " In main, Derived class Circle object calling"
<< " center(). ";
cir.center();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.