QuestionQuestion

• Objective
o Create a class hierachy by defining an abstract Shape class and concrete classes pertaining to various shapes: Circle, Sphere, Cylinder, Square, Cube, Triangle and Tetrahedron.
• Related SLO
o Develop properly structured multifile programs with automatic compilation.
o Use classes, polymorphism and inheritance in C++.
• Instructions
o Create a makefile named makefile that will automatically compile, link, and create an executable for this assignment.
- IMPORTANT: Be sure to use g++ as the compiler NOT gcc as we are now programming in C++.
- Example makefile using g++ here
- Do NOT submit a makefile with any other name.
- There are no extra .h or .cpp needed for this assignment.
o Create a program named LastnameFirstname24.cpp, that does the following:
- From the abstract Shape class, derive concrete classes Circle, Square, and Triangle. From class Circle, derive concrete classes Sphere and Cylinder. From class Square, derive concrete class Cube. Lastly, from class Triangle, derive class Tetrahedron.
- Define the following functions in Shape.
- virtual char* name() const - returns the name of the class
- virtual void printSize() const - display the area, or surface area & volume of the object
- virtual void inputData() - ask the user for the radius, side, and/or height of the object and set the appropriate data member(s)
- virtual double area() const - returns the area or surface area of the object
- virtual double volume() const - returns the volume of the object
- Copy and paste this starter file: assignment24.cpp
- It contains the abstract Shape class definition, a class definition for Circle, main() driver program, and example output.
- You will need to examine the starter file and create the subsequent classes based on the class definitions as well as the code in the main() to properly get the correct output.
- Do NOT change the Shape class definition.
- You should change the main() function when you first start testing your program. For example, you should create the class Circle first, and test all the functions to make sure that it is working correctly. After you've finished the program, you should put in the original main() function. In other words, turn in your program with the same main() function as the main() function that is already in the assignment24.cpp file.
- Geometric formulas for reference:
- PI = 3.14
- circle's area = πr2
- sphere's surface area = 4πr2
- sphere's volume = (4πr3) / 3
- cylinder's surface area = 2πrh+2πr2
- cylinder's volume = πr2h
- circle's circumference = 2πr
- square's area = s2
- cube's surface area = 6s2
- cube's volume = s3
- equilateral triangle's area = s2 * sqrt(3)/4
- regular tetrahedron's surface area = s2 * sqrt(3)
- regular tetrahedron's volume = s3 * sqrt(2)/12
o ** NOTE: The main error students make with this assignment is to repeat data unnecessarily. The point of inheritance is to be a lazy programmer and not reinvent the wheel. For example, if the base class Circle has a radius, the derived class Sphere does NOT need a radius, because it already has a radius, which is inherited from the base class Circle. Please keep this concept in mind when writing your code. **
o Be sure to have a program description at the top and in-line comments.
- Be clear with your comments, ensure you comment functions with descriptions as well as in-line comments where appropriate.
• Example Output
• Select an object from the menu (enter 7 to quit).
       0. Circle
       1. Sphere
       2. Cylinder
       3. Square
       4. Cube
       5. Triangle
       6. Tetrahedron
0
Enter the Circle's radius: 10
The Circle's area = 314

Select an object from the menu (enter 7 to quit).
       0. Circle
       1. Sphere
       2. Cylinder
       3. Square
       4. Cube
       5. Triangle
       6. Tetrahedron
1
Enter the Sphere's radius: 20
The Sphere's surface area = 5024
The Sphere's volume = 33493.3

Select an object from the menu (enter 7 to quit).
       0. Circle
       1. Sphere
       2. Cylinder
       3. Square
       4. Cube
       5. Triangle
       6. Tetrahedron
2
Enter the Cylinder's radius: 30
Enter the Cylinder's height: 40
The Cylinder's surface area = 13188
The Cylinder's volume = 113040

Select an object from the menu (enter 7 to quit).
       0. Circle
       1. Sphere
       2. Cylinder
       3. Square
       4. Cube
       5. Triangle
       6. Tetrahedron
3
Enter the Square's side: 50
The Square's area = 2500

Select an object from the menu (enter 7 to quit).
       0. Circle
       1. Sphere
       2. Cylinder
       3. Square
       4. Cube
       5. Triangle
       6. Tetrahedron
4
Enter the Cube's side: 60
The Cube's surface area = 21600
The Cube's volume = 216000

Select an object from the menu (enter 7 to quit).
       0. Circle
       1. Sphere
       2. Cylinder
       3. Square
       4. Cube
       5. Triangle
       6. Tetrahedron
5
Enter the Triangle's side: 70
The Triangle's area = 2121.76

Select an object from the menu (enter 7 to quit).
       0. Circle
       1. Sphere
       2. Cylinder
       3. Square
       4. Cube
       5. Triangle
       6. Tetrahedron
6
Enter the Tetrahedron's side: 80
The Tetrahedron's surface area = 11085.1
The Tetrahedron's volume = 60339.8

Select an object from the menu (enter 7 to quit).
       0. Circle
       1. Sphere
       2. Cylinder
       3. Square
       4. Cube
       5. Triangle
       6. Tetrahedron

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <iostream>
#include <cmath>

using namespace std;

class Shape {
public:
virtual void printSize() const = 0;
virtual const char* name() const = 0;
virtual void inputData() = 0;
virtual double area() const = 0;
virtual double volume() const{
    return 0.0;
}   
};

   #define PI 3.14

class Circle : public Shape {
public:
//syntax "datatype variable = value" will give a default value for the variable
// this constructor will be called with no arguments or one argument
// for example: shapeArray[0] = new Circle();
// for example: Circle c = new Circle(10);
Circle(double r = 0.0){
    if(r<0) r=-r;
    radius = r;
}
const char* name() const{
    return "Circle";
}
void printSize() const{
    cout<<"The "<<name()<<"'s area = "<<area()<<endl;
}
void inputData(){
    cout<<"Enter the "<<name()<<"'s radius: ";
    cin>>radius;
}
double area() const{
    return PI*radius*radius;
}      
protected:         
double radius;         
};...

By purchasing this solution you'll be able to access the following files:
Solution.cpp.

$13.50
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats