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

C++ CSCI 241 Assignment 9 100 points Purpose This short assignment covers inheri

ID: 3717621 • Letter: C

Question

C++ CSCI 241 Assignment 9
100 points

Purpose

This short assignment covers inheritance and polymorphism.

Assignment

For this project, you will create a series of classes to represent some simple geometric shapes and a small program to test your classes.

class Shape

Shape is an abstract base class.

Data Members

A Shape has the following private data member:

a color, which is of type string,

Methods

The Shape class should have the following public methods:

a constructor that takes a const string& argument and uses it to initialize the shape's color. Since the Shape class is abstract, this constructor will only be invoked by a derived-class constructor.

a virtual destructor. The method body for this destructor can be empty, since it does not need to delete any dynamic storage. If you don't have a virtual destructor for an abstract base class, the compiler may produce a warning message.

a virtual method called print() that takes no arguments and returns nothing. The method should print the color.

a pure virtual method called get_area() that takes no arguments and returns a double. Since it is pure virtual (or abstract), this method has no definition, only a prototype. It must be defined in any concrete class derived from Shape.

class Circle

Circle is derived from Shape using public inheritance.

Data Members

A Circle has the following private data member:

a radius, which is of type int,

Methods

The Circle class should have the following methods:

a constructor that takes a string to initialize the circle's color and an int to initiallize the circle's radius. The color string should be passed to the Shape constructor.

an overridden version of print() that takes no arguments and returns nothing. The method should call the base class print() method to print the color, then print the word "circle" followed by the circle's radius and area, e.g.:

an overridden version of get_area() that takes no arguments and returns a double. This method should compute and return the circle's area based on its radius.

class Rectangle

Rectangle is derived from Shape using public inheritance.

Data Members

A Rectangle has the following private data member:

a height, which is of type int,

a width, which is of type int,

Methods

The Rectangle class should have the following methods:

a constructor that takes a string to initialize the circle's color and two ints to initialize the rectangle's height and width. The color string should be passed to the Shape constructor.

an overridden version of print() that takes no arguments and returns nothing. The method should call the base class print() method to print the color, then print the word "rectangle" followed by the rectangle's height, width, and area, e.g.:

an overridden version of get_area() that takes no arguments and returns a double. This method should compute and return the rectangle's area based on its height and width.

class Triangle

Triangle is derived from Shape using public inheritance.

Data Members

A Triangle has the following private data member:

a height, which is of type int,

a base, which is of type int,

Methods

The Triangle class should have the following methods:

a constructor that takes a string to initialize the triangle's color and two ints to initialize the triangle's height and base. The color string should be passed to the Shape constructor.

an overridden version of print() that takes no arguments and returns nothing. The method should call the base class print() method to print the color, then print the word "triangle" followed by the triangle's height, base, and area, e.g.:

an overridden version of get_area() that takes no arguments and returns a double. This method should compute and return the rectangle's area based on its height and width.

Main Program

Write a test program that creates either an array or an STL vector of pointers to Shape objects.

Dynamically create some Circles, Rectangles, and Triangles (at least two of each). After creating each object, add it to the array or vector.

Loop through the array or vector of Shape pointers and call the print() method for each of them.

Loop through the array or vector of Shape pointers again and call the print() method for each of the Triangle objects in the array or vector.

Loop through the list of Shape pointers one more time and delete each object.

Output from this program should look something like this:

Other Points

Your code should enforce const correctness. Methods that do not alter the object should be const, for example.

Code for each of your classes should be placed in separate files in the usual fashion for non-template C++ classes. That means a header file and a source code file for each class. Do not submit all of the code in one big file just because you're feeling lazy. :-)

You must have a complete makefile, with a separate rule for compiling a separate object file for the code from each source code file. The linking rule should link all of your object files together to create one executable. The name of your final executable should be assign9.

Everything inheritance- and polymorphism-related that needs to be done in this assignment is also done in the inheritance example available on the course website.

Remember, use of the dynamic_cast operator requires Run Time Type Information (RTTI) to be enabled. It's on by default in g++, but may not be if you're working with a different compiler / IDE.

Don't forget to get rid of all compiler warnings when the -Wall compilation option is used.

As always, programs that do not compile on hopper automatically receive 0 points.

Submit your program using the electronic submission guidelines posted on the course web site and discussed in class.

Explanation / Answer

main.cpp:

#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Triangle.h"
#include <iostream>
#include <vector>

int main(void)
   {
   //Create vector shape with an initial size of 6
   std::vector <Shape *> shape(6);

   //Initialize vector with Shapes
   shape.push_back(new Circle("green", 10));
   shape.push_back(new Rectangle("red", 8, 6));
   shape.push_back(new Triangle("yellow", 8, 4));
   shape.push_back(new Triangle("black", 4, 10));
   shape.push_back(new Circle("orange", 5));
   shape.push_back(new Rectangle("blue", 3, 7));

std::cout << std::endl << "Printing all shapes..."
                         << std::endl << std::endl;

   //Process each element in vector 'shape'
   //'unsigned int' due to comparison warning
   for (unsigned int i = 0; i < shape.size(); ++i)
      {
      Shape *shapePtr = dynamic_cast<Shape *> (shape[i]);
  
      if (shapePtr != 0)
         shapePtr->print();
   }
  
   std::cout << std::endl << "Printing only circles..."
                          << std::endl << std::endl;

   //'unsigned int' due to comparison warning
   for (unsigned int i = 0; i < shape.size(); ++i)
      {
      Circle *circlePtr = dynamic_cast<Circle *> (shape[i]);
  
      if (circlePtr != 0)
         circlePtr->print();
      }
  
   //Loop through list of shape pointers and delete each object
   //Only needed if using ARRAY, not VECOTR.
// for (unsigned int i = 0; i < shape.size(); ++i)
    // delete shape[i];
   
     std::cout << std::endl;

   return 0;
   }


Circle.cpp
#include "Circle.h"
#include <iostream>
#include <cmath>
#include <string>

/****************************************************************
   FUNCTION: Circle()

   USE:   This is the default constructor for the Circle class. It
        takes the color of a circle and the radius.

   ARGUMENTS: 1. string cirColor - This string is sent to the
                 shape base class for initialization.
               
              2. int cirRadius - This is the radius of the
                 circle and is stored in the private variable
                 'radius'.
****************************************************************/
Circle::Circle(std::string cirColor, int cirRadius) : Shape(cirColor)
   {
   radius = cirRadius;
   }

/****************************************************************
   FUNCTION: get_area()

   USE:   This function calculates the area of the circle based on
        the value stored in 'radius'. It returns a double which
        is the calculated area.

   ARGUMENTS: None.
****************************************************************/
double Circle::get_area()
   {
   double area = 0.0;
   area = (M_PI * pow(radius, 2));

   return area;
   }

/****************************************************************
   FUNCTION: print()

   USE:   This function first calls the base class 'Shape' for the
        color to be printed, and then it prints out the circle
        radius and area.

   ARGUMENTS: None.
****************************************************************/
void Circle::print()
   {
   Shape::print();   //Prints color
   std::cout << " circle, radius " << radius
             << ", area " << get_area() << std::endl;
   }


Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H

#include "Shape.h"
#include <string>

class Circle : public Shape
   {
   protected:

      int radius;
  
   public:
    
      Circle(std::string, int);
      virtual double get_area();
      virtual void print();
    
   };

#endif //CIRCLE_H


Rectangle.cpp
#include "Rectangle.h"
#include <iostream>
#include <string>

/****************************************************************
   FUNCTION: Rectangle()

   USE:   This is the default constructor for the Rectangle class.
        It takes the color, height, and width of a rectangle.

   ARGUMENTS: 1. string recColor - This string is sent to the
                 shape base class for initialization.
               
              2. int recHeight - This is the height of the
                 rectangle which will be stored in the private
                 variable 'height'.
               
              3. int recWidth - This is the width of the
                 rectangle which will be stored in the private
                 variable 'width'.
****************************************************************/
Rectangle::Rectangle(std::string recColor, int recHeight,
                     int recWidth) : Shape(recColor)
   {
   height = recHeight;
   width = recWidth;
   }

/****************************************************************
   FUNCTION: get_area()

   USE:   This function calculates the area of the rectangle based
        on the values that are stored in the 'height' and 'width'
        variables. It returns a double which is the calculated
        area.

   ARGUMENTS: None.
****************************************************************/
double Rectangle::get_area()
   {
   double area = 0.0;
   area = height * width;

   return area;
   }

/****************************************************************
   FUNCTION: print()

   USE:   This function first calls the base class 'Shape' for the
        color to be printed, and then it prints out the rectangle
        height, width, and area.

   ARGUMENTS: None.
****************************************************************/
void Rectangle::print()
   {
   Shape::print(); //Prints color
   std::cout << " rectangle, height " << height
             << ", width " << width << ", area "
             << get_area() << std::endl;
   }

Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "Shape.h"
#include <string>

class Rectangle : public Shape
   {
   protected:

      int height;
      int width;
    
   public:

      Rectangle(std::string, int, int);
      virtual double get_area();
      virtual void print();
    
   };

#endif //RECTANGLE_H


Shape.cpp
#include "Shape.h"
#include <iostream>
#include <string>

/****************************************************************
   FUNCTION: Shape()

   USE:   This is the default constructor which initializes the
        shapes color.

   ARGUMENTS: string& shapeColor - Argument used to initialize
              the shapes color.
****************************************************************/
Shape::Shape(const std::string& shapeColor) : color(shapeColor) {}

/****************************************************************
   FUNCTION: print()

   USE:   This function prints the color stored in the 'color'
        string.

   ARGUMENTS: None.
****************************************************************/
void Shape::print()
   {
   std::cout << color;
   }

Shape.h
#ifndef SHAPE_H
#define SHAPE_H

#include <string>

class Shape
   {
   protected:

      std::string color;
    
   public:

      Shape(const std::string&);       //Constructor
      //Virtual destructor not needed if using the vector
      //virtual ~Shape() {}            //Virtual destructor
      virtual double get_area() = 0;   //Pure virtual
      virtual void print();            //Virtual
    
   };

#endif //SHAPE_H


Triangle.cpp
#include "Triangle.h"
#include <iostream>
#include <string>

/****************************************************************
   FUNCTION: Rectangle()

   USE:   This is the default constructor for the Rectangle class.
        It takes the color, height, and width of a rectangle.

   ARGUMENTS: 1. string triColor - This string is sent to the
                 shape base class for initialization.
               
              2. int triHeight - This is the height of the
                 triangle which will be stored in the private
                 variable 'height'.
               
              3. int triBase - This is the base of the
                 triangle which will be stored in the private
                 variable 'base'.
****************************************************************/
Triangle::Triangle(std::string triColor, int triHeight,
                   int triBase) : Shape(triColor)
   {
   height = triHeight;
   base = triBase;
   }

/****************************************************************
   FUNCTION: get_area()

   USE:   This function calculates the area of the triangle based
        on the values that are stored in the 'height' and 'base'
        variables. It returns a double which is the calculated
        area.

   ARGUMENTS: None.
****************************************************************/
double Triangle::get_area()
   {
   double area = 0.0;
   area = (0.5 * base * height);

   return area;
   }

/****************************************************************
   FUNCTION: print()

   USE:   This function first calls the base class 'Shape' for the
        color to be printed, and then it prints out the triangle
        height, base, and area.

   ARGUMENTS: None.
****************************************************************/
void Triangle::print()
   {
   Shape::print(); //Prints color
   std::cout << " triangle, height " << height
             << ", base " << base << ", area "
             << get_area() << std::endl;
   }

   Triangle.h
   /*********************************************************************
   PROGRAMMER: Trevor Bayless

   PURPOSE:    This file contains the declaration of the Triangle
               class which is derived from shape using public
               inheritance.
*********************************************************************/
#ifndef TRIANGLE_H
#define TRIANGLE_H

#include "Shape.h"
#include <string>

class Triangle : public Shape
   {
   protected:

      int height;
      int base;
    
   public:

      Triangle(std::string, int, int);
      virtual double get_area();
      virtual void print();
    
   };

#endif //TRIANGLE_H

sample output

                                                                                                                                                            
Printing all shapes...                                                                                                                                      
                                                                                                                                                            
green circle, radius 10, area 314.159                                                                                                                       
red rectangle, height 8, width 6, area 48                                                                                                                   
yellow triangle, height 8, base 4, area 16                                                                                                                  
black triangle, height 4, base 10, area 20                                                                                                                  
orange circle, radius 5, area 78.5398                                                                                                                       
blue rectangle, height 3, width 7, area 21                                                                                                                  
                                                                                                                                                            
Printing only circles...                                                                                                                                    
                                                                                                                                                            
green circle, radius 10, area 314.159                                                                                                                       
orange circle, radius 5, area 78.5398