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

C omplete the provided partial C++ program that utilizes classes and inheritance

ID: 3666886 • Letter: C

Question

Complete the provided partial C++ program that utilizes classes and inheritance to model geometric shapes. Class Shape is an abstract base class used to model properties common to all shapes including its Color. Shape includes a function (area) that must exhibit polymorphic behavior in any derived classes. Class Triangle is a derived class that is used to model properties of all triangles. The Triangle class inherits from the base class Shape. Class Rectangle is a derived class that is used to model properties of all rectangles. The Rectangle class inherits from the base class Shape.

Filename

Description

main.cpp

Included

Driver program to test Shape, Triangle, and Rectangle classes

shape.h

Write and submit this file

Specification file for Shape class

shape.cpp

Included

Implementation file for Shape class

triangle.h

Included

Specification file for Triangle class

triangle.cpp

Write and submit this file

Implementation file for Triangle class

rectangle.h

Included

Specification file for Rectangle class

rectangle.cpp

Write and submit this file

Implementation file for Rectangle class

color.h

Included

Specification file for Color class

color.cpp

Write and submit this file

Implementation file for Color class

p02input1.txt

Included

Tests class Shape member functions

p02input2.txt

Included

Tests class Triangle member functions

p02input3.txt

Included

Tests class Rectangle member functions

color.h file below

-------------------------------------------------------------------------------------------------------------------------

/
// color.h
//
// Color class models the color of an item <red,green,blue>
//
// Use the contents of this file to implement color.cpp
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//

#include <iostream>
using namespace std;

#ifndef COLOR_H
#define COLOR_H

class Color
{
private:
    int     red;            // Amount of red 0-255
    int     green;          // Amount of green 0-255
    int     blue;           // Amount of blue 0-255

public:
    Color();                // Initializes red, green, and blue to zero
    void setRed(int rr);    // Sets red to rr
    void setGreen(int gg); // Sets green to gg
    void setBlue(int bb);   // Sets blue to bb

    int getRed() const;     // Returns red
    int getGreen() const;   // Returns green
    int getBlue() const;    // Returns blue

    void print() const      // Writes <red,green,blue> to std output
    {
   cout << "<" << getRed() << "," << getGreen() << "," << getBlue() << ">";
    }
};

#endif


-------------------------------------------------------------------------------------------------------------

rectangle.h file below

-----------------------------------------------------------------------------------------------------------

//
// rectangle.h
//
// Rectangle class models Rectangle with side lengths l, w
//
// Use the contents of this file to implement rectangle.cpp
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//

#include "shape.h"

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle : public Shape
{
private:
    double l, w;                                  // Side lengths

public:
    Rectangle();                                  // Initializes l, w to 0.0 and red, green, blue to 0

    Rectangle(double ll, double ww,               // Initializes l, w to ll, ww and
              int rr, int gg, int bb);            // red, green, blue to rr, gg, bb respectively
                                               

    double area() const;                          // Returns area value computed from l, w

    double perimeter() const;                     // Returns perimeter value computed from l, w
};

#endif

------------------------------------------------------------------------------------------------------------------------

triangle.h file below

------------------------------------------------------------------------------------------------------------------------

//
// triangle.h
//
// Triangle class models triangle with side lengths a, b, c
//
// Use the contents of this file to implement triangle.cpp
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//

#include "shape.h"

#ifndef TRIANGLE_H
#define TRIANGLE_H

class Triangle : public Shape
{
private:
    double a, b, c;            // Side lengths

public:
    Triangle();                                   // Initializes a, b, c to 0.0 and red, green, blue to 0

    Triangle(double aaa, double bbb, double ccc, // Initializes a, b, c to aaa, bbb, ccc and
             int rr, int gg, int bb);             // red, green, blue to rr, gg, bb respectively
                                               

    double area() const;                          // Returns area value computed from a, b, c

    double perimeter() const;                     // Returns perimeter value computed from a, b, c
};

#endif


----------------------------------------------------------------------------------------------------------------------------

shape.cpp file below

---------------------------------------------------------------------------------------------------------------------------

//
// shape.cpp
//
// Shape class models a generic geometric shape with color
// defined by a private Color object attribute c
//
// Use the contents of this file to reverse engineer the contents of shape.h
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//
//

#include "shape.h"

Shape::Shape()
// Initializes c to <0,0,0>
{
    c = Color();              
}

Shape::Shape(int r, int g, int b)
// Initializes c to <r,g,b>
{
    c.setRed(r);
    c.setGreen(g);
    c.setBlue(b);
}

Color Shape::getColor() const
// Returns color of object
{
    return c;
}

double Shape::area() const
// Returns default area of 0.0 -- polymorphic function
{
    return 0.0;
}

double Shape::perimeter() const
// Returns default perimeter of -1.0 -- polymorphic function
{
    return -1.0;
}


------------------------------------------------------------------------------------------------------

main.cpp file below

------------------------------------------------------------------------------------------------------


// main.cpp
//
// Driver program which is used to test each
// class member function.
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <typeinfo>
#include "color.h"
#include "shape.h"
#include "triangle.h"
#include "rectangle.h"

using namespace std;                     // Global using declaration

// Prototypes for support functions -- definitions appear after main()
void Bar(int n);                         // Prototype for Bar function
void Print(Shape* someShape);           // Prototype for Print function
//void PrintColor(Color someColor);        // Prototype for PrintColor function

// Start of main() function

int main (int argc, char* const argv[]) // Command-line arguments (more on this later)
{
ifstream inputs;                         // Input file stream variable for test file
char op, ch;                           // Hold operation and optional char input from test file
Shape* ptr = NULL;                     // Pointer to abstract shape object
string operand1,operand2,operand3,operand4;   // Holds operand values input from test file
string comment;                        // Holds comment input from test file
Color someColor;                       // Holds color input from file
char t;                                // Holds s for Shape, t/T for Triangle, r/R for Rectangle
double a, b, c;                        // Holds lengths a, b, c input from file
int red, green, blue;                  // Holds red, green, blue input from file
Color* color;


// Output usage message if test input file name is not provided
if (argc != 2)
{
    cout << "Usage: project02 <inputfile> ";
   return 1;
}

// Attempt to open test input file -- terminate if file does not open
inputs.open(argv[1]);
if (!inputs)
{
    cout << "Error - unable to open input file" << endl;
   return 1;
}

Bar(60);                                           // Output long bar

// Process comment line from input file
getline(inputs, comment);                          // Input file header comment
cout << endl << comment << endl << endl;           // Output file header comment

// Configure floating point decimal output
cout << fixed << showpoint << setprecision(2);
  
  
// Below is the primary loop that processes each operation appearing within the test file.
// Starts with an initial priming read of first operation

inputs >> op;                                         // Attempt to input first test operation from file
  
while (inputs)                                     // While Not-EOF
{
    switch (op)                                      // Process operation input from test file
    {
     case '#':   // Test file comment
            getline(inputs, comment);      // Input and echo the comment appearing in the test file
            cout << '#' << comment << endl;
            break;
  
     case '?':   // Print Operator
            cout << "TypeID -- ";
            Print(ptr);
            break;  

     case '~':   // Print Bar
            Bar(40);                         // Output short bar
            break;              

     case '+':   // Use symbol input from file to invoke appropriate constructor
                 inputs >> t;               // Input object type symbol

                 try
                 {
                   cout << "Constructor -- ";
                   switch (t)
                   {
                     case 's': // Shape default constructor
                                cout << "Shape() ";
                                ptr = new Shape();
                                break;

                     case 'S': // Shape parameterized constructor
                                inputs >> red >> green >> blue;
                                cout << "Shape(" << red << "," << green << "," << blue << ") ";
                                ptr = new Shape(red,green,blue);
                                break;

                     case 't': // Triangle default constructor
                                cout << "Triangle() ";
                                ptr = new Triangle;
                                break;

                     case 'T': // Triangle parameterized constructor
                                inputs >> a >> b >> c >> red >> green >> blue;
                                cout << "Triangle(" << a << ", " << b << ", "
                                     << c << ", " << red << ", " << green << ", " << blue << ") ";
                                ptr = new Triangle(a, b, c, red, green, blue);
                                break;

                     case 'r': // Rectangle default constructor
                                cout << "Rectangle() ";
                                ptr = new Rectangle;
                                break;

                     case 'R': // Rectangle parameterized constructor
                                inputs >> a >> b >> red >> green >> blue;
                                cout << "Rectangle(" << a << ", " << b << ", "
                                    << red << ", " << green << ", " << blue << ") ";
                                ptr = new Rectangle(a, b, red, green, blue);
                                break;

                     default:   cout << "Error: unrecognized object" << endl;
                    } // End switch (op)


                    cout << "Completed";
                  } // End try
                  catch ( ... )                        // Catch any exception thrown above
                  {
                    cout << "Failed";
                  }
          
                  cout << endl;                
                  break;

      case 'c':   // getColor()
                  cout << "getColor() -- ";
                  try
                  {
                    ptr->getColor().print();
                    cout << " Completed";
                  }
                  catch ( ... )
                  {
                    cout << "Failed";
                  }
                  cout << endl;
                  break;

      case 'a':   // area(...)
                  //inputs >> operand1;
                  cout << "area() -- ";
                  try
                  {
                    cout << ptr->area();
                  }
                  catch ( ... )
                  {
                    cout << "Failed";
                  }
                  cout << endl;
                  break;  

      case 'p':   // perimeter(...)
                  //inputs >> operand1;
                  cout << "perimeter() -- ";
                  try
                  {
                    cout << ptr->perimeter();
                  }
                  catch ( ... )
                  {
                    cout << "Failed";
                  }
                  cout << endl;
                  break;
                      
      case '-':   // Destructor
                  try
                  {
                    cout << "Destructor -- ";
                    delete ptr;                        // Deallocate currency
                    cout << "Completed";
                       ptr = NULL;                     // Make sure that ptr is not a dangling pointer
                  }
                  catch ( ... )
                  {
                    cout << "Failed";
                  }
                  cout << endl << endl;
                  break;      

      default:    // Error
                  cout << "Error - unrecognized operation '" << op << "'" << endl;
                  cout << "Terminating now..." << endl;
                  return 1;
                  break;
    }
  
    inputs >> op;                                     // Attempt to input next command
}

cout << endl;
Bar(60);                                           // Output long bar

return 0;
}

/************** Implementation of Print() and Bar() functions ********************/

// DO NOT MODIFY THIS CODE

void Bar(int n)
// Bar() -- prints horizontal bar
{
   for(int k = 0; k < n; k++)
        cout << '#';
    cout << endl;
} // End Bar()


void Print(Shape* someShape)
// Writes shape object description to stdout
{
   string s = typeid(*someShape).name();
   cout << s << endl;         
}

/************** End of main.cpp ***************/


-------------------------------------------------------------------------------------------

The first input file is for Test Shape Class below

-------------------------------------------------------------------

# p02input1.txt - Test Shape class

# Test default constructor
+ s
?
-

# Test parameterized constructor
+ S 1 2 3
?
-

# Test area() and perimeter() with default constructor
+ s
?
a
p
-

# Test area() and perimeter() with parameterized constructor
+ S 1 2 3
?
a
p
-

# Test getColor() with default constructor
+ s
?
c
-

# Test getColor() parameterized constructor
+ S 1 2 3
?
c
-


-----------------------------------------------------------------------------------

The second input file is Test Rectangle Class file below

-------------------------------------------------------------------------------

# p02input1.txt - Test Rectangle class

# Test default constructor
+ r
?
-

# Test parameterized constructor
+ R 3 5 0 128 256
?
-

# Test getColor() with default constructor
+ r
?
c
-

# Test getColor() with parameterized constructor
+ R 3 5 0 128 256
?
c
-

# Test area() and perimeter() with default constructor
+ r
?
a
p
-

# Test area() and perimeter() with parameterized constructor
+ R 3 5 0 128 256
?
a
p
-

-----------------------------------------------------------------------------

The third input file is Test Rectangle Class file below

-------------------------------------------------------------------------------

# p02input1.txt - Test Triangle class

# Test default constructor
+ t
?
-

# Test parameterized constructor
+ T 3 4 5 128 256 0
?
-

# Test area() and perimeter() with default constructor
+ t
?
a
p
-

# Test area() and perimeter() with parameterized constructor
+ T 3 4 5 128 256 0
?
a
p
-

# Test getColor() with default constructor
+ t
?
c
-

# Test getColor() parameterized constructor
+ T 3 4 5 128 256 0
?
c
-


--------------------------------------------------------------------------------------

I need the Implementation files which are triangle.cpp, rectangle.cpp, color.cpp, and a header file which is shape.h

I need 4 files.

Filename

Description

main.cpp

Included

Driver program to test Shape, Triangle, and Rectangle classes

shape.h

Write and submit this file

Specification file for Shape class

shape.cpp

Included

Implementation file for Shape class

triangle.h

Included

Specification file for Triangle class

triangle.cpp

Write and submit this file

Implementation file for Triangle class

rectangle.h

Included

Specification file for Rectangle class

rectangle.cpp

Write and submit this file

Implementation file for Rectangle class

color.h

Included

Specification file for Color class

color.cpp

Write and submit this file

Implementation file for Color class

p02input1.txt

Included

Tests class Shape member functions

p02input2.txt

Included

Tests class Triangle member functions

p02input3.txt

Included

Tests class Rectangle member functions

Explanation / Answer

//Triangle.cpp
//header file
#include<math.h>
#include "Triangle.h"
#include "Shape.h"
// Initializes a, b, c to 0.0 and red, green, blue to 0
Triangle::Triangle(): Shape(0,0,0)
{

}
// Initializes a, b, c to aaa, bbb, ccc and
// red, green, blue to rr, gg, bb respectively                                               
Triangle::Triangle(double aaa, double bbb, double ccc, int rr, int gg, int bb)
   : Shape(rr,gg,bb)
{
   a=aaa;
   b=bbb;
   c=ccc;
}
//Returns the area of a triangle
double Triangle::area() const                       
{
   double s=(a+b+c)/2.0;

   double Area=s*(s-a)*(s-b)*(s-c);

   return sqrt(Area);

}
//Returns the perimeter of triangle
double Triangle::perimeter() const
{
   return (a+b+c);
}

------------------------------------------------------------------------------------------------------------------------------------------------

//Rectangle.cpp
//include Rectangle header file
#include "Rectangle.h"
// Initializes l, w to 0.0 and red, green, blue to 0
Rectangle::Rectangle()
   :Shape(0,0,0)                               
{  
   l=0;
   w-0;
}

//Set values for red, green, blue to rr, gg, bb respectively                                               
Rectangle::Rectangle(double ll, double ww,int rr, int gg, int bb)
   :Shape(rr,gg,bb)     
{
   l=ll;
   w=ww;
}

// Returns area value computed from l, w
double Rectangle::area() const
{
   return (l*w);
}
//Return perimeter of rectangle
double Rectangle::perimeter() const
{
   return 2*(l+w);
}

------------------------------------------------------------------------------------------------------------------------------------------------


//Color.cpp
//include header files
#include<iostream>
#include "Color.h"
using namespace std;
// Initializes red, green, and blue to zero
Color::Color()
{
   red=0;
   green=0;
   blue=0;
}
void Color::setRed(int rr)    // Sets red to rr
{
   red=rr;
}
void Color::setGreen(int gg) // Sets green to gg
{
   green=gg;
}
void Color::setBlue(int bb)   // Sets blue to bb
{
   blue=bb;
}

int Color::getRed() const     // Returns red
{
   return red;
}
int Color::getGreen() const   // Returns green
{
   return green;
}
int Color::getBlue() const    // Returns blue
{
   return blue;
}

void Color::print() const      // Writes <red,green,blue> to std output
{
   cout << "<" << getRed() << "," << getGreen() << "," << getBlue() << ">"<<endl;
}

------------------------------------------------------------------------------------------------------------------------------------------------

//Shape.h

#ifndef SHAPE_H
#define SHAPE_H
#include "Color.h"
class Shape
{

private:
   Color c;
public:

   // Default Constructor
   Shape();
   Shape(int r, int g, int b);
   Color getColor() const;
   double area() const;
   double perimeter() const;

};

#endif SHAPE_H

------------------------------------------------------------------------------------------------------

//Shape.cpp


#include "Shape.h"
#include "Color.h"
// Initializes c to <0,0,0>
Shape::Shape()
{
    c = Color();              
}

Shape::Shape(int r, int g, int b)
// Initializes c to <r,g,b>
{
    c.setRed(r);
    c.setGreen(g);
    c.setBlue(b);
}

Color Shape::getColor() const
// Returns color of object
{
    return c;
}

double Shape::area() const
// Returns default area of 0.0 -- polymorphic function
{
    return 0.0;
}

double Shape::perimeter() const
// Returns default perimeter of -1.0 -- polymorphic function
{
    return -1.0;
}

------------------------------------------------------------------------------------------------------------------------------------------------