rewrite the C++ lab with the code below (an area calculator for different shapes
ID: 666371 • Letter: R
Question
rewrite the C++ lab with the code below (an area calculator for different shapes) and privide a method for the user to add shapes and shape area formulae to calculate the area of user-appointed shapes. (Down below I provided the code for lab8)
Here is my code for lab8:
Shape.h:
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include <iostream>
#include <cstdlib>
using namespace std;
class Shape
{
protected:
float base;
float height;
float width;
float radius;
float area;
public:
void getlength();
void printarea(int);
};
class Triangle : public Shape
{
public:
void getlength()
{
cout << "Input Your Triangle's base length ";
cin >> base;
cout << "Input Your Triangle's height length ";
cin >> height;
}
void printarea()
{
area = 0.5 * base * height;
cout << "Your triangle's area is "<< area <<" ";
}
};
class Rectangle : public Shape
{
public:
void getlength()
{
cout << "Input Your Rectangle's width length ";
cin >> width;
cout << "Input Your Rectangle's height length ";
cin >> height;
}
void printarea()
{
area = width * height;
cout << "Your triangle's area is "<< area <<" ";
}
};
class Circle : public Shape
{
public:
void getlength()
{
cout << "Input Your Rectangle's width length ";
cin >> radius;
}
void printarea()
{
area = 3.14 *radius*radius;
cout << "Your triangle's area is "<< area <<" ";
}
};
#endif // SHAPE_H_INCLUDED
lab8.cpp:
#include "Shape.h"
int main()
{
Triangle tri;
Rectangle rec;
Circle cir;
int choice;
cout<<" Area of Different Shapes:";
cout<<" 1. Area of a Triangle";
cout<<" 2. Area of a Rectangle";
cout<<" 3. Area of a Circle";
cout<<" 4. Exit";
cout<<" Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
tri.getlength();
tri.printarea();
break;
case 2:
rec.getlength();
rec.printarea();
break;
case 3:
cir.getlength();
cir.printarea();
break;
case 4:
system("PAUSE");
return 0;
break;
default:
cout<<"Enter your choice of shape (1-4) ";
break;
}
}
Explanation / Answer
Shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include <bits/stdc++.h>
using namespace std;
class Shape{
protected:
float base;
float height;
float width;
float radius;
float area;
public:
virtual void getlength();
virtual void printarea();
};
class Triangle : public Shape{
public:
void getlength() override{
cout << "Input Your Triangle's base length ";
cin >> base;
cout << "Input Your Triangle's height length ";
cin >> height;
}
void printarea() override{
area = 0.5 * base * height;
cout << "Your triangle's area is "<< area <<" ";
}
};
class Rectangle : public Shape{
public:
void getlength() override{
cout << "Input Your Rectangle's width length ";
cin >> width;
cout << "Input Your Rectangle's height length ";
cin >> height;
}
void printarea() override{
area = width * height;
cout << "Your Rectangle's area is "<< area <<" ";
}
};
class Circle : public Shape{
public:
void getlength() override{
cout << "Input Your Cirlce's radius length ";
cin >> radius;
}
void printarea() override{
area = 3.14 *radius*radius;
cout << "Your Circle's area is "<< area <<" ";
}
};
Shape.cpp
int main(){
vector<Shape*> vect;
vector<int> myvec;
int choice;
while (true){
cout<<" Area of Different Shapes:";
cout<<" 1. Area of a Triangle";
cout<<" 2. Area of a Rectangle";
cout<<" 3. Area of a Circle";
cout<<" 4. Exit";
cout<<" Enter your choice: ";
cin>>choice;
if (choice == 1){
Triangle *tri = new Triangle();
vect.push_back(tri);
myvec.push_back(1);
}
else if (choice == 2){
Rectangle *rect = new Rectangle();
vect.push_back(rect);
myvec.push_back(2);
}
else if (choice == 3){
Circle *cir = new Circle();
vect.push_back(cir);
myvec.push_back(3);
}
else if (choice == 4)
break;
else{
cout << "Enter the number between (1 and 4) both included";
}
}
for (int i = 0; i < vect.size(); i++){
vect[i]->getlength();
vect[i]->getarea();
cout << endl << endl << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.