In: Computer Science
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square
text file:
circle 3.5
square 3
rectangle 38 36
circle 23
rectangle 2 13
square 12
square 24
square 1
square 8
square 27
rectangle 22 13
rectangle 22 18
rectangle 14 27
circle 11
circle 18
square 5
example output:
Name Area
***********************
circle 1 38.47 square 1 9.00 rectangle 1 1368.00 circle 2 1661.06 rectangle 2 26.00 square 2 144.00 square 3 576.00 square 4 1.00 square 5 64.00 square 6 729.00 rectangle 3 286.00 rectangle 4 396.00 rectangle 5 378.00 circle 3 379.94 circle 4 1017.36 square 7 25.00 *********************** Total Area 7098.82
#include<iostream> //for cin and cout
#include<fstream> //for ifstream
#include<iomanip> //for setw and setprecision
using namespace std;
//shape class definition
class Shape{
public:
virtual void setArea(double) = 0;
virtual double getArea() = 0;
virtual void printArea() = 0;
};
//Circle class definition
class Circle : public Shape{
private:
double radius,area;
public:
Circle(double r){
radius=r;
}
void setArea(double a){
area = a;
}
double getArea(){
return area;
}
void calculateArea(){
area = 3.14*radius*radius;
}
void printArea(){
cout<<area;
}
};
//Square class definition
class Square : public Shape{
private:
double length,area;
public:
Square(double l){
length = l;
}
void setArea(double a){
area = a;
}
double getArea(){
return area;
}
void calculateArea(){
area = length*length;
}
void printArea(){
cout<<area;
}
};
//Rectangle class definition
class Rectangle : public Shape{
private:
double length,breadth,area;
public:
Rectangle(double l,double b){
length = l;
breadth = b;
}
void setArea(double a){
area = a;
}
double getArea(){
return area;
}
void calculateArea(){
area = length*breadth;
}
void printArea(){
cout<<area;
}
};
//main function implementation
int main()
{
ifstream in;
in.open("info.txt"); //opens file info.txt
if(!in){ //if file doesn't exist then prints the message and
terminates the program
cout<<"Error: file doesn't exits";
return -1;
}
string shape; //to store the shape read form file
int c=0,r=0,s=0; //to keep track of count of each shape read
double total=0; //to store the total area of shapes
cout<<fixed;
cout<<setprecision(2); //sets decimal places to 2
cout<<setw(10)<<"Name"<<"
Area"<<endl;
cout<<"***************************"<<endl;
//reads unitl end of file gets reached
while(in>>shape){
if(shape=="circle"){ //if shape is circle then
c+=1;
double radius;
in>>radius;
Circle circle(radius);
cout<<setw(10)<<shape<<" "<<c<<"
";
circle.calculateArea();
circle.printArea();
total += circle.getArea();
}
else if(shape=="square"){ //else if shape is square
s+=1;
double length;
in>>length;
Square square(length);
cout<<setw(10)<<shape<<" "<<s<<"
";
square.calculateArea();
square.printArea();
total+=square.getArea();
}
else if(shape=="rectangle"){ //else if shape is rectangle
r+=1;
double length,breadth;
in>>length>>breadth;
Rectangle rectangle(length,breadth);
cout<<setw(10)<<shape<<" "<<r<<"
";
rectangle.calculateArea();
rectangle.printArea();
total += rectangle.getArea();
}
cout<<endl;
}
in.close(); //closes the in stream
cout<<"***************************"<<endl;
cout<<setw(12)<<"Total Area"<<"
"<<total<<endl; //prints the total area
return 0;
}
//info.txt screenshot
//output screenshot
//any query, post in the comments section