Write the following 7 C++ files (all function definitions must be in the cpp fil
ID: 3830173 • Letter: W
Question
Write the following 7 C++ files (all function definitions must be in the cpp files): Shape h declaration of Shape class Shape.cpp definitions of Shape class functions Circle h declaration of Circle class, publicly inherited from Shape Circle.cpp definitions of Circle class functions Rectangle. h declaration of Rectangle class, publicly inherited from Shape Rectangle.cpp definitions of Rectangle class functions Prog12 cpp main program to use the classes Shape class contains: Private Data: x coordinate integer y coordinate integer shape type string Functions: default constructor-assign "unknown shape" to type variable and 0,0 as the location. constructor that receives shape type, x, and y values and sets the private data fields set for location receives both x and y values and sets the private data fields (any integer value will be allowed) print writes out the shape type and location as shape type "at" (x,y) format. Ex: 'Circle at (23,75) or "Square at 12,460' Circle class nherited publicly from Shape contains Private Data: radius a positive double Functions: default constructor stores 1.0 into radius and sends "Circle" and 0,0 to the parent constructor constructor that receives x, y, and radius values. Send "Circle" and the x and y values to the parent constructor, and send the radius parameter value to the setRadius function. setRadius receives a double. If the double is 0, store it to radius data field, otherwise store 1.0 print use the parent class print function to write out "Circle at with the location, and then write out the radius, and the area. The output should look something like: "Circle at (23,750 with a radius of 2.0 has an area of 12.57". Show 2 decimal places of accuracy on the area value. calcArea calculates and returns the area of a circle with the radius from the private data fieldExplanation / Answer
//Shape.h
#pragma once
#include<string>
#include<iostream>
using namespace std;
class Shape
{
string type;
int x, y;
public:
Shape();
Shape(string t,int x,int y);
void set_location(int a, int b);
void print();
virtual ~Shape();
};
--------------------------------------
//Shape.cpp
#include"Shape.h"
Shape::Shape()
{
type = "";
x = 0;
y = 0;
}
Shape::Shape(string t, int a, int b)
{
type = t;
x = a;
y = b;
}
void Shape::set_location(int a, int b)
{
x = a;
y = b;
}
Shape::~Shape()
{
}
void Shape::print()
{
cout << type << " at " << "(" << x << " ," << y << ")"<<endl;
}
-------------------------------------------------
//Circle.h
#include "Shape.h"
class Circle : public Shape
{
double radius;
public:
Circle();
Circle(int a, int b,double radius);
void print();
void setRadius(double r);
double calArea();
};
-----------------------------------------------------
//Circle.cpp
#include"Circle.h"
#include"Shape.h"
Circle::Circle() : Shape("Circle",0,0)
{
radius = 0;
}
Circle::Circle(int a, int b, double r) : Shape("Circle",a,b)
{
radius = r;
}
void Circle::setRadius(double r)
{
radius = r;
}
double Circle::calArea()
{
return 3.14159*radius;
}
void Circle::print()
{
Shape::print();
cout << "With radius " << radius << " has an area " << calArea() << endl;
}
-------------------------------------------
//Rectangle.h
#include"Shape.h"
using namespace std;
class Rectangle: public Shape
{
double height, width;
public:
Rectangle();
Rectangle(int a, int b, double h, double w );
void setWidth(double w);
void setHeight(double h);
void print();
double calArea();
};
-----------------------------------------------
//Rectangle.cpp
#include"Rectangle.h"
#include"Shape.h"
Rectangle::Rectangle() : Shape("Rectangle", 0, 0)
{
height = 0;
width = 0;
}
Rectangle::Rectangle(int a, int b, double h, double w) : Shape("Rectangle",a,b)
{
height = h;
width = w;
}
void Rectangle::print()
{
Shape::print();
cout << "With width " << width << " and height " << height << " has an area " << calArea() << endl;
}
double Rectangle::calArea()
{
return height*width;
}
void Rectangle::setHeight(double h)
{
height = h;
}
void Rectangle::setWidth(double w)
{
width = w;
}
--------------------------------
//main.cpp
#include"Shape.h"
#include "Circle.h"
#include "Rectangle.h"
int main()
{
Shape s1, s2("Rectangle",3, 4);
s1.print();
s2.print();
//now set location
s1.set_location(6, 7);
s1.print();
s2.set_location(10, 4);
s2.print();
//create circle object
Circle c1, c2(5,6,4.5);
c1.print();
c2.print();
c1.setRadius(3.5);
c2.setRadius(5.6);
c1.print();
c2.print();
//create rectangle object
Rectangle r1, r2(3, 4, 2, 7);
r1.print();
r2.print();
r1.setHeight(4.3);
r1.setWidth(5.2);
r2.setHeight(8.5);
r2.setWidth(2.6);
r1.print();
r2.print();
}
---------------------------------------------------
//output
at (0 ,0)
Rectangle at (3 ,4)
at (6 ,7)
Rectangle at (10 ,4)
Circle at (0 ,0)
With radius 0 has an area 0
Circle at (5 ,6)
With radius 4.5 has an area 14.1372
Circle at (0 ,0)
With radius 3.5 has an area 10.9956
Circle at (5 ,6)
With radius 5.6 has an area 17.5929
Rectangle at (0 ,0)
With width 0 and height 0 has an area 0
Rectangle at (3 ,4)
With width 7 and height 2 has an area 14
Rectangle at (0 ,0)
With width 5.2 and height 4.3 has an area 22.36
Rectangle at (3 ,4)
With width 2.6 and height 8.5 has an area 22.1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.