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

This is my week 3 assigment that I did: / This is my main.cpp #include <iostream

ID: 3839471 • Letter: T

Question

This is my week 3 assigment that I did:

/ This is my main.cpp
#include <iostream>
#include "areaSquare.h"
#include "areaCircle.h"

using namespace std;

/*int main()
{

cout << "For the Circle, tell me how long is the radius?: " <<endl;
cin >> radius;

cout << "Now, Input length of side: " <<endl;
cin >> sidelen;


     cout << "The are of the Circle is: " << areaCircle <<endl;
     cout << "The are of the Square is: " << areaSquare <<endl;

    return 0;
}

// This is my areaSquare.h
/* #ifndef AREASQUARE_H_INCLUDED
#define AREASQUARE_H_INCLUDED

// This is the content of the .h file, which is where the declarations go
   double sidelen;
   double areaSquare;


#endif // AREASQUARE_H_INCLUDED */

// This is my areaCircle.h
#ifndef AREACIRCLE_H_INCLUDED
#define AREACIRCLE_H_INCLUDED

#define Pi 3.14159265358979323846

// This is the content of the .h file, which is where the declarations go
   double radius;
   double areaCircle;

#endif // AREACIRCLE_H_INCLUDED

// This is my areaSquare.cpp
#include "areaSquare.h"


{
   areaSquare = sidelen * sidelen;
}


// This is my areaCircle.cpp
#include "areaCircle.h"

{
    areaCircle(Pi * radius * radius);
}

For this assignment, you will write a new version of the area calculation program from Unit 3 that makes use of inheritance in C++. Add a new Shape base class to the area calculation program that includes data members common to all shapes (such as a shape ID, a shape type, and a unit of measure). You will use the Code::Blocks software to write this application.

Write a new version of the area calculation program from Unit 3 that makes use of inheritance in C++.

Add a new Shape base class to the area calculation program that includes data members common to all shapes (such as a shape ID, a shape type, and a unit of measure).

The Shape base class should also include a virtual getArea() member function.

Revise the Circle and Square classes so that they inherit from the Shape base class.

Add a third shape to the program that also inherits from the Shape class.

The finished program should ask the user to enter the relevant information for each shape and then print the area and other information for each shape.

You will use the Code::Blocks software to write this application. Access this software via the Toolwire virtual desktop activity link in this unit. Be sure to organize the code correctly into header (.h) and implementation (.cpp) files. Your code should include meaningful comments and be correctly formatted.

Explanation / Answer

#include <iostream>
using namespace std;
//Symbolic constant for PI
#define PI 3.14159265358979323846
//Base class Shape definition
class Shape
{
public:
//Common data member for all the classes
int shapeID;
string shapeType;
//Constructor
Shape(int ID, string Type)
{
shapeID = ID;
shapeType = Type;
}//End of constructor
//Pure Virtual function
virtual void getArea() = 0;
};//End of class

//Class Circle derived from Shape class
class Circle : Shape
{
//Own data member
double radius;
public:
//Constructor for derived class which calls base class constructor
Circle(int ID, string Type, double ra):Shape(ID, Type)
{
radius = ra;
}//End of constructor
//Overrides getArea() function
void getArea()
{
cout<<" Shape ID = "<<shapeID<<" Shape = "<<shapeType;
cout<<" Areas = "<<PI * radius * radius;
}//End of function
};//End of class

//Class Square derived from Shape class
class Square : Shape
{
//Own data member
double sidelen;
public:
//Constructor for derived class which calls base class constructor
Square(int ID, string Type, double si):Shape(ID, Type)
{
sidelen = si;
}//End of constructor
//Overrides getArea() function
void getArea()
{
cout<<" Shape ID = "<<shapeID<<" Shape = "<<shapeType;
cout<<" Areas = "<<sidelen * sidelen;
}//End of function
};//End of class

//Class Rectangle derived from Shape class
class Rectangle : Shape
{
//Own data member
double length, breadth;
public:
//Constructor for derived class which calls base class constructor
Rectangle(int ID, string Type, double le, double br):Shape(ID, Type)
{
length = le;
breadth = br;
}//End of constructor
//Overrides getArea() function
void getArea()
{
cout<<" Shape ID = "<<shapeID<<" Shape = "<<shapeType;
cout<<" Areas = "<<length * breadth;
}//End of function
};//End of class

//Main function
int main()
{
int id;
string type;
double measure1, measure2;
//Accepts id and shape type
cout<<" Enter Shape ID: ";
cin>>id;
cout<<" Enter Shape Type: ";
cin>>type;
//Checks if type is circle
if(type == "Circle" || type == "circle")
{
//Accepts radius
cout<<" Enter Radius: ";
cin>>measure1;
//Creates an object for Circle class
Circle c(id, type, measure1);
//Calls getArea() method
c.getArea();
}
//Checks type is square
else if(type == "Square" || type == "square")
{
//Accepts side length
cout<<" Enter Side Length: ";
cin>>measure1;
//Creates an object for Square class
Square s(id, type, measure1);
//Calls getArea() method
s.getArea();
}
//Checks type is Rectangle
else if(type == "Rectangle" || type == "rectangle")
{
//Accepts length and breadth
cout<<" Enter Length: ";
cin>>measure1;
cout<<" Enter Breadth: ";
cin>>measure2;
//Creates an object for rectangle class
Rectangle r(id, type, measure1, measure2);
//Calls getArea() method
r.getArea();
}
//Other wise error
else
cout<<" Wrong Type";
}//End of main

Sample Run1:

Enter Shape ID: 11

Enter Shape Type: Rectangle

Enter Length: 12

Enter Breadth: 10

Shape ID = 11
Shape = Rectangle
Areas = 120

Sample Run2:

Enter Shape ID: 12

Enter Shape Type: circle

Enter Radius: 2

Shape ID = 12
Shape = circle
Areas = 12.5664

Sample Run3:

Enter Shape ID: 123

Enter Shape Type: Square

Enter Side Length: 11

Shape ID = 123
Shape = Square
Areas = 121

Sample Run4:

Enter Shape ID: 124

Enter Shape Type: cone

Wrong Type

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote