Question

In: Computer Science

In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...

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

Solutions

Expert Solution

#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


Related Solutions

Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Derive a class BulkDiscount from DiscountPolicy. It should have a constructor that has two parameters minimum and percent. It should define the method computeDiscount so that if the quantity purchased of an item is more then minimum, the discount is percent percent.
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
Part I: The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class.
  Part I:   The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class. * Update the function display() to be a virtual function     * Add function, double weeklyEarning() to be a pure virtual function. This will make Employee an abstract class, so your main will not be able to create any Employee objects. Part II: The Derived Classes Add an weeklyEarning() function to each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT