Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to give new definitions to these constructors and member functions. Figur

ID: 3633292 • Letter: I

Question

I need to give new definitions to these constructors and member functions. Figure::center, Figure::draw, Figure::erase, Triangle::draw, Triangle::erase, Rectangle::draw, Rectangle::erase, so that the draw functions actually draw figures on the screen using a '*'. Erase functions should just clear the screen.

//Here is the main.cpp

#include <cstdlib>
#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;
}

//Here is the figure.h

#ifndef FIGURE_H
#define FIGURE_H
#include <iostream>
using namespace std;
class Figure
{
public:

virtual void erase();
virtual void draw();

void center()
{
cout << "The center function was called" << endl;
erase();
draw();
}
};

void Figure::erase()
{
cout << "The figure was erased" << endl;
}

void Figure::draw()
{
cout << "A figure was drawn" << endl;
}
#endif

//Here is the triangle.h

#ifndef TRIANGLE_H
#define TRIANGLE_H
#include <iostream>
#include "figure.h"
using namespace std;

class Triangle : public Figure
{
public:

void erase();
void draw();
};

void Triangle::erase()
{
cout << "A triangle was erased" << endl;
}
void Triangle::draw()
{
cout << "A triangle was drawn" << endl;
}
#endif

//Here is the rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
#include "figure.h"

using namespace std;



class Rectangle : public Figure
{
public:

void erase();
void draw();

};

void Rectangle::erase()
{
cout << "A rectangle was erased" << endl;
}

void Rectangle::draw()
{
cout << "A rectangle was drawn" << endl;
}
#endif

Explanation / Answer

class Figure
{
public:
    Figure();
    virtual void erase();
    virtual void draw();
    virtual void center();
private:
    bool cent;
};

Figure::Figure() : cent(false)
{

}

void Figure::erase()
{
    cout << "Figure class calling erase(). ";
}

void Figure::draw()
{
    cout << "Figure class calling draw(). ";
}

void Figure::center()
{
    cout << "Figure class calling center(). ";
    erase();
    cent = true;
    draw();
}

Rectangle class

class Rectangle : public Figure
{  
public:
    Rectangle();
    void erase();
    void draw();
    void center();
private:
    void centerOutput();
    bool cent;
};

Rectangle::Rectangle() : Figure()
{
   
}

void Rectangle::erase()
{
    cout << "Rectangle class calling erase(). ";
}

void Rectangle::draw()
{
    for(int i = 0; i < 5; i++)
    {
        if(cent)
            centerOutput();
       
        cout << "*";
        for(int j = 0; j < 20; j++)
        {
            cout << "*";
        }
        cout << "* ";
    }
}

void Rectangle::center()
{
    cout << "Rectangle calling center(). ";
    erase();
    cent = true;
    draw();
}

void Rectangle::centerOutput()
{
    const int l = 20;
    const int line = 80;
    int position = (int)((line - l) / 2);
    for(int i = 0; i < position; i++)
    {
        cout << " ";
    }
}


or

#include <iostream>
using namespace std;

class CPolygon {
protected:
    int width, height;
public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area ()
      { return (0); }
};

class CRectangle: public CPolygon {
public:
    int area ()
      { return (width * height); }
};

class CTriangle: public CPolygon {
public:
    int area ()
      { return (width * height / 2); }
};

int main () {
CRectangle rect;
CTriangle trgl;
CPolygon poly;
CPolygon * ppoly1 = &rect;
CPolygon * ppoly2 = &trgl;
CPolygon * ppoly3 = &poly;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly3->set_values (4,5);
cout << ppoly1->area() << endl;
cout << ppoly2->area() << endl;
cout << ppoly3->area() << endl;
return 0;
}

or

#include <iostream>
using namespace std;

class CPolygon {
protected:
    int width, height;
public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area (void) =0;
};

class CRectangle: public CPolygon {
public:
    int area (void)
      { return (width * height); }
};

class CTriangle: public CPolygon {
public:
    int area (void)
      { return (width * height / 2); }
};

int main () {
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = &rect;
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << endl;
cout << ppoly2->area() << endl;
return 0;
}


// pure virtual members can be called
// from the abstract base class
#include <iostream>
using namespace std;

class CPolygon {
protected:
    int width, height;
public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area (void) =0;
    void printarea (void)
      { cout << this->area() << endl; }
};

class CRectangle: public CPolygon {
public:
    int area (void)
      { return (width * height); }
};

class CTriangle: public CPolygon {
public:
    int area (void)
      { return (width * height / 2); }
};

int main () {
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = &rect;
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
return 0;
}


// dynamic allocation and polymorphism
#include <iostream>
using namespace std;

class CPolygon {
protected:
    int width, height;
public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area (void) =0;
    void printarea (void)
      { cout << this->area() << endl; }
};

class CRectangle: public CPolygon {
public:
    int area (void)
      { return (width * height); }
};

class CTriangle: public CPolygon {
public:
    int area (void)
      { return (width * height / 2); }
};

int main () {
CPolygon * ppoly1 = new CRectangle;
CPolygon * ppoly2 = new CTriangle;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}