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

Code in C++ - Read all instructions: Sample output: As shown in the attachment,

ID: 3735568 • Letter: C

Question

Code in C++ - Read all instructions:

Sample output:

As shown in the attachment, please implement a base class 'Shape' and two derived classes 'TwoDimensionalShape and 'ThreeDimensionalShape'. The class "TwoDimensionalShape' should have two derived classes 'Circle' and Triangle' and the class ThreeDimensionalShape' should have two derived classes 'Sphere' and Tetrahedron. You can assume that only regular tetrahedrons will be used. You can also assume that the triangles are equilateral. Each of the two-dimensional shapes (Circle and Triangle) should contain a member function getArea to calculate the area of the two-dimensional shape. Each three-dimensional shape (Sphere and Tetrahedron) should have member functions getArea and getVolume to calculate the surface area and volume of three-dimensional shapes respectively. Write a program to test this implementation. If the shape is two dimensional, its area should be displayed. If the shape is a three-dimensional shape, display its surface area and volume. The instances of the objects should be able to access functions to set and get the center of the shapes. The functions for the 2D shapes should be able to handle 2D coordinates (x.y) while the functions for the 3D shapes should be able to handle 3D coordinates (x.y,.z). Please also implement a print function to print the appropriate information to the screen Please implement this in a way that efficiently uses INHERITANCE. For this and all future assignments that use classes, please use separate files for the classes (header and.cpp files) and main function Please make sure to zip up the whole project directory

Explanation / Answer

//main.cpp
#include <iostream>
#include "Circle.h"
#include "Triangle.h"
#include "Sphere.h"
#include "Tetrahedrons.h"

using namespace std;

int main() {
   //Initialized variables.
   int choice, x, y, z;
   double radius, length, width, sideLength;
   Circle ci;
   Triangle tr;
   Sphere sp;
   Tetrahedrons te;
   //Always run the program until user selected 0.
   while (true) {
       cout << "Please choose a shape or 0 to exit:" << endl;
       cout << "1. Circle" << endl;
       cout << "2. Triangle" << endl;
       cout << "3. Sphere" << endl;
       cout << "4. Regular Tetrahedron " << endl;
       cout << "0. Exit" << endl;
       cout << "Choice: ";
       cin >> choice;
       //Selected different class to calculate the result according to users' selection.
       switch (choice) {
       case 1: {
           cout << endl;
           cout << "You have chosen the circle." << endl;
           cout << endl;
           cout << "Please enter the center of the circle (x-coordinate and then y-coordinate):" << endl;
           cin >> x;               //Input x and y coordinate.
           cin >> y;
           cout << endl;
           cout << "Please enter the radius of the circle." << endl;
           cin >> radius;           //Input radius of circle.
           cout << endl;
           ci = Circle(x, y, radius);
           ci.print();
           break;
           }
       case 2: {
           cout << endl;
           cout << "You have chosen the triangle." << endl;
           cout << endl;
           cout << "Please enter the center of the triangle (x-coordinate and then y-coordinate):" << endl;
           cin >> x;               //Input x and y coordinate.
           cin >> y;
           cout << endl;
           cout << "Please enter the length and width of the triangle." << endl;
           cin >> length;           //Input length and width of triangle.
           cin >> width;
           cout << endl;
           tr = Triangle(x, y, length, width);
           tr.print();
           break;
           }
       case 3: {
           cout << endl;
           cout << "You have chosen the sphere." << endl;
           cout << endl;
           cout << "Please enter the radius of the sphere." << endl;
           cin >> radius;       //Input radius of sphere.
           cout << endl;
           cout << "Please enter the center of the sphere (x-coordinate, y-coordinate, then z-coordinate):" << endl;
           cin >> x;           //Input x, y and z coordinate.
           cin >> y;
           cin >> z;
           cout << endl;
           sp = Sphere(x, y, z, radius);
           sp.print();
           break;
           }
       case 4: {
           cout << endl;
           cout << "You have chosen the regular tetrahedron." << endl;
           cout << endl;
           cout << "Please enter the edge length of the regular tetrahedron." << endl;
           cin >> sideLength;   //Input length of side of tetrahedron.
           cout << endl;
           cout << "Please enter the center of the regular tetrahedron (x-coordinate, y-coordinate, then z-coordinate):" << endl;
           cin >> x;           //Input x, y and z coordinate.
           cin >> y;
           cin >> z;
           cout << endl;
           te = Tetrahedrons(x, y, z, sideLength);
           te.print();
           break;
           }
       case 0: {
           return 0;   //Stop and exit the program.
           break;
           }
       }
   }
}
----------------------------------------------------------
//Circle.cpp
#include <iostream>
#include "Circle.h"
using namespace std;

/*Constructor.*/
Circle::Circle(int xValue, int yValue, double radiusValue)
   :TwoDimensionalShape(xValue, yValue) {
   setRadius(radiusValue);
}

/*Set the private variable radius of circle.*/
void Circle::setRadius(double radiusValue) {
   radius = radiusValue;
}

/*Get the private variable radius.*/
double Circle::getRadius() {
   return radius;
}

/*Calculated the area of circle.*/
double Circle::getArea() {
   return 3.1415 * pow(getRadius(), 2.0);
}

/*Print out the result.*/
void Circle::print(){
   cout << "The circle with radius " << getRadius() << "that is located at(" << Shape::getX() << ", " << Shape::getY() << ") has: " << endl;
   cout << "An area of " << getArea() << endl;
   cout << endl;
}
------------------------------------------------------
//Circle.h
#pragma once
#include "TwoDimensionalShape.h"

class Circle : public TwoDimensionalShape {
public:
   Circle(int = 0, int = 0, double = 0);
   void setRadius(double);
   double getRadius();
   double getArea();
   void print();
private:
   double radius;
};
--------------------------------------------------
//Shape.cpp
#include <iostream>
#include "Shape.h"
using namespace std;

/*Constructor.*/
Shape::Shape(int xValue, int yValue) {
   setX(xValue);
   setY(yValue);
}

/*Set the private variable X*/
void Shape::setX(int xValue) {
   x = xValue;
}

/*Set the private variable Y*/
void Shape::setY(int yValue) {
   y = yValue;
}

/*Get the private variable X*/
int Shape::getX() {
   return x;
}

/*Get the private variable Y*/
int Shape::getY() {
   return y;
}
--------------------------------------
//Shape.h
#pragma once
class Shape {
public:
   Shape(int = 0, int = 0);
   void setX(int xValue);
   void setY(int yValue);
   int getX();
   int getY();
private:
   int x;
   int y;
};
-----------------------------------
//Sphere.cpp
#include <iostream>
#include "Sphere.h"
using namespace std;

/*Constructor.*/
Sphere::Sphere(int xValue, int yValue, int zValue, double radiusValue)
   :ThreeDimensionalShape(xValue, yValue, zValue) {
   setRadius(radiusValue);
}

/*Set the private variable radius of sphere.*/
void Sphere::setRadius(double radiusValue) {
   radius = radiusValue;
}

/*Get the private variable radius.*/
double Sphere::getRadius() {
   return radius;
}

/*Calculated the surface area of sphere.*/
double Sphere::getArea() {
   return 4 * 3.1415 * getRadius() * getRadius();
}

/*Calculated the volume of sphere.*/
double Sphere::getVolume() {
   return 4 * 3.1415 * getRadius() * getRadius() * getRadius() / 3;
}

/*Print out the result.*/
void Sphere::print() {
   cout << "The shpere with radius " << getRadius() << " that is located at(" << Shape::getX() << ", " << Shape::getY() << ", " << ThreeDimensionalShape::getZ() << ") has: " << endl;
   cout << "Surface area of " << getArea() << endl;
   cout << "Volume of " << getVolume() << endl;
   cout << endl;
}
-------------------------------------------------
//Sphere.h
#pragma once
#include "ThreeDimensionalShape.h"

class Sphere : public ThreeDimensionalShape {
public:
   Sphere(int = 0, int = 0, int = 0, double = 0);
   void setRadius(double);
   double getRadius();
   double getArea();
   double getVolume();
   void print();
private:
   double radius;
};
--------------------------------------------
//Tetrahedrons.cpp
#include <iostream>
#include "Tetrahedrons.h"
using namespace std;

/*Constructor.*/
Tetrahedrons::Tetrahedrons(int xValue, int yValue, int zValue, double sideLengthValue)
   :ThreeDimensionalShape(xValue, yValue, zValue) {
   setSideLength(sideLengthValue);
}

/*Set the private variable side length of tetrahedron.*/
void Tetrahedrons::setSideLength(double sideLengthValue) {
   sideLength = sideLengthValue;
}

/*Get the private variable side length of tetrahedron.*/
double Tetrahedrons::getSideLength() {
   return sideLength;
}

/*Calculated the surface area of tetrahedron.*/
double Tetrahedrons::getArea() {
   return 1.7321 * getSideLength() * getSideLength();
}

/*Calculated the volume of tetrahedron.*/
double Tetrahedrons::getVolume() {
   return 1.4142 * getSideLength() * getSideLength() * getSideLength() / 12;
}

/*Print out the result.*/
void Tetrahedrons::print() {
   cout << "The Tetrahedrons with sideLength " << getSideLength() << " that is located at(" << Shape::getX() << ", " << Shape::getY() << ", " << ThreeDimensionalShape::getZ() << ") has: " << endl;
   cout << "Surface area of " << getArea() << endl;
   cout << "Volume of " << getVolume() << endl;
   cout << endl;
}
-------------------------------------------------
//Tetrahedrons.h
#pragma once
#include "ThreeDimensionalShape.h"

class Tetrahedrons : public ThreeDimensionalShape {
public:
   Tetrahedrons(int = 0, int = 0, int = 0, double = 0);
   void setSideLength(double sideLength);
   double getSideLength();
   double getArea();
   double getVolume();
   void print();
private:
   double sideLength;
};
-------------------------------------------------------
//ThereDimensionalShape.cpp
#include <iostream>
#include "ThreeDimensionalShape.h"
using namespace std;

/*Extended from class Shape.*/
ThreeDimensionalShape::ThreeDimensionalShape(int xValue, int yValue, int zValue)
   :Shape(xValue, yValue) {
   setZ(zValue);
}

/*Set z coordinate with value z.*/
void ThreeDimensionalShape::setZ(int zValue) {
   z = zValue;
}

/*Get the z coordinate.*/
int ThreeDimensionalShape::getZ() {
   return z;
}
---------------------------------------------
//ThreeDimensionalShape.h
#pragma once
#include "Shape.h"

class ThreeDimensionalShape : public Shape {
public:
   ThreeDimensionalShape(int = 0, int = 0, int = 0);
   void setZ(int zValue);
   int getZ();
private:
   int z;
};
-----------------------------------------------
//Triangle.cpp
#include <iostream>
#include "Triangle.h"

using namespace std;

/*Constructor.*/
Triangle::Triangle(int xValue, int yValue, double lengthValue, double widthValue)
   : TwoDimensionalShape(xValue, yValue){
   setLength(lengthValue);
   setWidth(widthValue);
}

/*Set the private variable length of triangle.*/
void Triangle::setLength(double lengthValue) {
   length = lengthValue;
}

/*Set the private variable width of triangle.*/
void Triangle::setWidth(double widthValue) {
   width = widthValue;
}

/*Get the private variable length of triangle.*/
double Triangle::getLength() {
   return length;
}

/*Get the private variable width of triangle.*/
double Triangle::getWidth() {
   return width;
}

/*Get the area of triangle.*/
double Triangle::getArea() {
   return getLength() * getWidth();
}

/*Print out the result.*/
void Triangle::print() {
   cout << "The triangle with length " << getLength() << "and width " << getWidth() << " that is located at(" << Shape::getX() << ", " << Shape::getY() << ") has: " << endl;
   cout << "An area of " << getArea() << endl;
   cout << endl;
}
----------------------------------------
//Triangle.h
#pragma once
#include "TwoDimensionalShape.h"

class Triangle : public TwoDimensionalShape {
public:
   Triangle(int = 0, int = 0, double = 0, double = 0);
   void setLength(double);
   void setWidth(double);
   double getLength();
   double getWidth();
   double getArea();
   void print();
private:
   double length;
   double width;
};
------------------------------------------------------
//TwoDimensionalShape.cpp
#include <iostream>
#include "TwoDimensionalShape.h"
using namespace std;

/*Extended from class Shape.*/
TwoDimensionalShape::TwoDimensionalShape(int xValue, int yValue)
   :Shape(xValue, yValue) {
}
----------------------------------------------------
//TwoDimensionalShape.h
#pragma once
#include "Shape.h"

class TwoDimensionalShape : public Shape {
public:
   TwoDimensionalShape(int = 0, int = 0);
private:
};