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

Implement a graphics System that has classes for various figures: rectangles, sq

ID: 3557404 • Letter: I

Question

Implement a graphics System that has classes for various figures: rectangles, squares, triangles and circles. A rectangle has data members height, width, color , and center point. A square has center point and an edge and color . A circle has data members center, color and radius. The several specific shape classes are derived from a common class, Figure.

Implement classes Rectangle, Square, Triangle and Circle classes and derive them from class Figure.

In addition to constructor, destructor and set/get functions, each class has two virtual functions erase and draw to draw or erase the shape.

To draw or erase a figure you must use OpenGL graphics library.

TO RECEIVE FULL CREDIT PLEASE SHOW SCREENSHOTS
Please zip the folder of the whole assignment.

Explanation / Answer

#include <fstream.h>

#include <string.h>

#include <math.h>

//Header file for class Figure

const Size = 9;

enum FigKind {Circle, Rectangle, Square, Triangle};

struct RectangleType

{

float Width, Length;

};

struct TriangleType

{

float Base, Height;

};

class Figure

{

struct FigureType

{

    char Name[Size];

    float Area;

    float Perimeter;

    FigKind Shape;

    union

    {

      float Radius;         //circle

      RectangleType Rec;    //rectangle

      float Side;           //square

      TriangleType Tri;     //triangle

    };

};

struct FigureType OneFig;

public:

void Create();

void Init(FigKind ShapeVal, //input - shape

            float Val1,        //input - first dimension

             float Val2);       //input - second dimension

void ComputePerim();

void ComputeArea();

char *GetName();

float GetArea();

float GetPerim();

float GetFirstVar();

float GetSecondVar();

void Display();

};

________________________________________________________________

      Figure 4.2 shows the implementation file for our class Figure. Method Create sets the Name field of a new figure to the blank string using the string.h function strset. Method Init stores ShapeVal into the union tag member, defines the Name field, and stores either Val1 or both Val1 and Val2 into the appropriate union data member.

Figure 4.3 Implementation File for Class Figure

_________________________________________________________________

#include "figure.h"

//Abstract data type Figure with methods for initializing and

//displaying the characteristics of Figure instances.

void Figure::Create()

//Creates figure with blank string as value of Name field.

{

strset(OneFig.Name, ' ');

}

void Figure::Init(FigKind ShapeVal, float Val1, float Val2)

//

//Pre : Figure has been created.

//Post: Object is defined with union tag member FigKind set

//      to ShapeVal. If object has one data member, Val1 is

//      stored in it and Val2 is ignored; otherwise Val1 and

//      Val2 are stored.

{

  OneFig.Shape = ShapeVal;

switch (OneFig.Shape)

{

    case Circle:

      strcpy(OneFig.Name, "Circle");

      OneFig.Radius = Val1;

      break;

    case Rectangle:

      strcpy(OneFig.Name, "Rectangle");

      OneFig.Rec.Width = Val1;

      OneFig.Rec.Length = Val2;

      break;

    case Square:

      strcpy(OneFig.Name, "Square");

      OneFig.Side = Val1;

      break;

    case Triangle:

      strcpy(OneFig.Name, "Triangle");

      OneFig.Tri.Base = Val1;

      OneFig.Tri.Height = Val2;

      break;

  }

}

void Figure::ComputePerim()

//Defines the value of object data member Perimeter.

//Pre : Union tag member is defined and characteristics of

//    object are defined.

//Post: Assigns value to Perimeter data member.

{

switch(OneFig.Shape)

{

    case Circle:

      OneFig.Perimeter = 2 * M_PI * OneFig.Radius;

      break;

    case Rectangle:

      OneFig.Perimeter =

           2 * (OneFig.Rec.Width + OneFig.Rec.Length);

      break;

    case Square:

      OneFig.Perimeter = 4 * OneFig.Side;

      break;

    case Triangle:

      OneFig.Perimeter =

           //pow() to raise to a power

           OneFig.Tri.Base + OneFig.Tri.Height +

           sqrt(pow(OneFig.Tri.Base, 2.0) +

            pow(OneFig.Tri.Height, 2.0));

      break;

}

}

void Figure::ComputeArea()

//Defines the value of object data member Area.

//Pre : Union tag member is defined and characteristics of

//    object are defined.

//Post: Assigns value to Area data member.

{

switch(OneFig.Shape)

{

    case Circle:

      OneFig.Area = M_PI * pow(OneFig.Radius, 2.0);

      break;

    case Rectangle:

      OneFig.Area = OneFig.Rec.Width * OneFig.Rec.Length;

      break;

    case Square:

      OneFig.Area = pow(OneFig.Side, 2.0);

      break;

    case Triangle:

      OneFig.Area = 0.5 * OneFig.Tri.Base * OneFig.Tri.Height;

      break;

}

}

char *Figure::GetName()

//Returns value of date member Name.

{

char* Name = new char[Size + 1];

strcpy(Name, OneFig.Name);

return(Name);

}

float Figure::GetArea()

//Returns value of data member Area.

{

return(OneFig.Area);

}

float Figure::GetPerim()

//Returns value of data member Perimeter.

{

return(OneFig.Perimeter);

}

float Figure::GetFirstVar()

//Retrieves the value of first object dimension.

{

float X;

switch(OneFig.Shape)

{

    case Circle:

      X = OneFig.Radius;

      break;

    case Rectangle:

      X = OneFig.Rec.Length;

      break;

    case Square:

      X = OneFig.Side;

      break;

    case Triangle:

      X = OneFig.Tri.Base;

      break;

}

return(X);

}

float Figure::GetSecondVar()

//Returns value of second dimension, if defined;

//otherwise returns 0.

{

float X;

switch(OneFig.Shape)

{

    case Circle:

      X = 0;

      break;

    case Rectangle:

      X = OneFig.Rec.Length;

      break;

    case Square:

      X = 0;

      break;

    case Triangle:

      X = OneFig.Tri.Base;

      break;

}

return(X);

}

void Figure::Display()

//Displays object data members.

//Pre : All required data members are defined.

//Post: Each data member has been displayed.

{

int Equal;

//check for blank string as Name value

Equal = strncmp(OneFig.Name, " ", 1);

if (!Equal)

   cout << "Figure kind is not defined ";

else

{

    cout << "Figure kind is " << OneFig.Name << " ";

    cout << "Area is " << OneFig.Area << " ";

    cout << "Perimeter is " << OneFig.Perimeter << " ";

    switch(OneFig.Shape)

    {

      case Circle:

      cout << "Radius is " << OneFig.Radius <<" ";

      break;

      case Rectangle:

      cout << "Width is " << OneFig.Rec.Width << " ";

      cout << "Length is " << OneFig.Rec.Length << " ";

      break;

      case Square:

      cout << "Side is " << OneFig.Side << " ";

      break;

      case Triangle:

      cout << "Base is " << OneFig.Tri.Base << " ";

      cout << "Height is " << OneFig.Tri.Height << " ";

    }

}

}

_________________________________________________________________

      The accessor methods GetName, GetArea, and so on, are all pretty straightforward. Accessor function GetSecondVar returns the value stored in the second variant field. As shown, its value is not defined when the figure is a circle or square. Method Display displays the values of all data members of an instance of class Figure. A switch statement is used to display the union members of OneFig.

Using Figure Class Library

      Figure 4.4 provides a simple client program that uses our Figure class library. The program creates class instances for a rectangle and a square, stores data in both records, computes the area and perimeter for each figure, and displays the figure's characteristics.

Figure 4.4 Client Program for Figure class with Sample Run

________________________________________________________________

#include "figure.h"

//Program to test Figure class

void main()

{

Figure MySquare, MyRectangle;

MyRectangle.Create();

MyRectangle.Init(Rectangle, 5.0, 10.0);

MyRectangle.ComputePerim();

MyRectangle.ComputeArea();

MyRectangle.Display();

cout << " ";

MySquare.Create();

MySquare.Init(Square, 7.0, 0.0);

MySquare.ComputePerim();

MySquare.ComputeArea();

MySquare.Display();

cout << " ";

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote