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

//8 Create a C++ console application with a class hierarchy of at least 4 classe

ID: 3918077 • Letter: #

Question

//8

Create a C++ console application with a class hierarchy of at least 4 classes with 3 levels of inheritance (generalization). You may use a single or multiple files (i.e. with .cpp and .h). Create base and derived classes. Consider how specific or general your base class can be considered. For example, here are some examples such as  Shape/Circle/Rectangle class hierarchy, and ahone/CellPhone/LandLinePhone/SmartPhone class hierarchy with UML as such:

CellPhone “is-a” Phone

LandLinePhone “is-a” Phone                                                         

SmartPhone “is-a” CellPhone

Program Requirements:

Add at least 3 member variables in the base class (super class) and at least 2 member variables to each of the other 3 subclasses (derived classes).

Add the get/set methods (accessors/mutators) for each of the member variables to all 4 classes.

Create all-argument constructors in the base class and constructors in each of the other 3 classes.

Add at least one overriding function to each subclass, such as a showValues() function.

Add at least one additional member method to each subclass.

Required Output:

Instantiate at least one object of each class in main(), and show the execution of all member functions.

Generate output samples demonstrating all of your member functions, adequately testing them and showing their functionality (i.e. inputting values, outputting values (displaying them), performing calculations, etc.). Post the output samples in a Word document, along with all of your source code, in a single zip file to Blackboard. Include a basic UML diagram in the Word document to represent your class hierarchy.

Explanation / Answer

//main.cpp
#include <iostream>
#include "shape.h"
#include "rectangle.h"
#include "square.h"
#include "circle.h"

int main() {
Rectangle rectangle(4, 2);
Square square(4);
Circle circle(6);

Shape* shapes[] = {&rectangle, &square, &circle};

Shape* rectangle1 = new Rectangle(4, 2);
delete rectangle1;

for (int i = 0; i < 3; ++i) {
    shapes[i]->print();
    std::cout << ' ';
}

return 0;
}
-----------------------------------------------------------------------------------------------------
//Circle.cpp
#include <math.h>
#include <iostream>
#include "circle.h"

using std::cout;

Circle::Circle(double radius): radius(radius) {}

double Circle::perimeter() const {
return 2 * M_PI * radius;
}

double Circle::area() const {
return M_PI * radius * radius;
}

void Circle::print() const {
cout << "Radius: " << radius
       << " Perimeter: " << perimeter()
       << " Area: " << area() << ' ';
}
-------------------------------------------------------------------------------------------------
//Circle.h
#ifndef SHAPES_CIRCLE_H
#define SHAPES_CIRCLE_H

#include "shape.h"

class Circle : public Shape {
double radius;

public:

Circle(double radius);

double perimeter() const;
double area() const;
void print() const;
};

#endif
--------------------------------------------------------------------------------------------
//rectangle.cpp
#include <iostream>
#include "rectangle.h"

using std::cout;

Rectangle::Rectangle(double width, double height)
: width(width), height(height) {}

double Rectangle::perimeter() const {
return 2 * (width + height);
}

double Rectangle::area() const {
return width * height;
}

void Rectangle::print() const {
cout << "Width: " << width
       << " Height: " << height
       << " Perimeter: " << perimeter()
       << " Area: " << area() << ' ';
}
---------------------------------------------------------------------------------------
//rectangle.h
#ifndef SHAPES_RECTANGLE_H
#define SHAPES_RECTANGLE_H

#include "shape.h"

class Rectangle : public Shape {
protected:
double width;
double height;

public:

Rectangle(double width, double height);

double perimeter() const;
double area() const;
void print() const;
};


#endif
-------------------------------------------------------------------------------
//square.cpp
#include <iostream>
#include "square.h"

using std::cout;

Square::Square(double width): Rectangle(width, width) {}

void Square::print() const {
cout << "Width: " << width
       << " Perimeter: " << perimeter()
       << " Area: " << area() << ' ';
}
------------------------------------------------------------------------------------
//square.h
#ifndef SHAPES_SQUARE_H
#define SHAPES_SQUARE_H

#include "rectangle.h"

class Square : public Rectangle {
public:

Square(double width);

void print() const;
};

#endif
-----------------------------------------------------------------------------------
//shape.h
#ifndef SHAPES_SHAPE_H
#define SHAPES_SHAPE_H

class Shape {
public:

virtual double perimeter() const = 0;
virtual double area() const = 0;
virtual void print() const = 0;

virtual ~Shape() {};
};

#endif