object oriented Programming Spring 2017 Assignment a12, due by 11 on Thursday, M
ID: 3833584 • Letter: O
Question
object oriented Programming Spring 2017 Assignment a12, due by 11 on Thursday, May 4 write the following 7C fles (all function definitions must be in the qpp fles): Shapeh declaration of Shape class Shape-cpp-definitioes hape class fanctions ofGrde darek publicly inherited ircle opp -del tions of Orde dass functions Rectangle h declaration Rectangle class, publicly inherited from Shape Rectangle/cp of Rectangle class functions Progi2eppo mai shape dasscontains: coordinate integer y coordina shape type string Functions: default ign unknown shape to type variable and 00 that receives shape type, e, and y values nd values and the Pri fields (any integer value be allowed the shape type location as shape type at (xy print- writes format. Ext orcie at (23,750 Shall Cirde class inherited publicly from pe-contains: radius. a positive double radius and sends "Circle" and 00 to the parent nstructor that elvesx y, and radius values. Send Circle" and the to the parent constructor, and send the radius the double is 0, data field, otherwise the parent class print function to write out Orde at with print the location, and then write out the radius, and the area. The thing like: Show 2 decimal places of accuracy culates and retums th of a circle with the radius from the private datafield. Rectangle class publicly inherited from Shape-contains positive double height-a positive double Functions: default constructor sets width and height to each be 10 Rectangle and 00 the parent eives width, height x and y locations pass Rectangle and setwidth and setHeight functions for the width and height serwidth receives a double the double is 0, store it to width lf the double is 0, height field otherwise lass print function to write out Rectange at print with the location, then write the width the height and the tput should look somethi with a height of 23 and a width of of 39 Show 2 decimal pl of accuracy of rectangle with the height and width from the private data felds. Main Program: Create 2 Shape objects. with parameters and without. print for both Shape objects. of the objects, giving Run print again for that object Create 20nde objects, with parameters and Run print for both Circle objects. of the Circle objects, giving Run the print function again Create 2 Retangle obiects. with parameters, and without Run print for both Rectangle objects. eight functions for Rectangle objects giving new values. Run the print function again create 3 shape dass poi dynamically all object of each class and their addresses to the Shape pointe functin from each of the Shape class pointers block in each file Descriptive nts before each function definit Compress the files and submit thezipped file on Titanium. N printout requiredExplanation / Answer
PROGRAM CODE:
Shape.h
/*
* Shape.h
*
* Created on: 06-May-2017
* Author: kasturi
*/
#ifndef SHAPES_SHAPE_H_
#define SHAPES_SHAPE_H_
using namespace std;
class Shape
{
private:
int x, y;
string type;
public:
Shape();
Shape(int, int, string);
void setLocation(int, int);
void print();
};
#endif /* SHAPES_SHAPE_H_ */
Shape.cpp
/*
* Shape.cpp
*
* Created on: 06-May-2017
* Author: kasturi
*/
#include <iostream>
#include "Shape.h"
using namespace std;
Shape::Shape()
{
x = 0;
y = 0;
type = "unknown shape";
}
Shape::Shape(int x_, int y_, string type_)
{
x = x_;
y = y_;
type = type_;
}
void Shape::setLocation(int x_, int y_)
{
x = x_;
y = y_;
}
void Shape::print()
{
cout<<type<< " at ("<<x<<","<<y<<")";
}
Circle.h
/*
* Circle.h
*
* Created on: 06-May-2017
* Author: kasturi
*/
#ifndef SHAPES_CIRCLE_H_
#define SHAPES_CIRCLE_H_
#include "Shape.h"
class Circle:public Shape
{
private:
double radius;
public:
Circle();
Circle(int , int, double );
void setRadius(double);
void print();
double calcArea();
};
#endif /* SHAPES_CIRCLE_H_ */
Circle.cpp
/*
* Circle.cpp
*
* Created on: 06-May-2017
* Author: kasturi
*/
#include <iostream>
#include "Circle.h"
#include "Shape.h"
using namespace std;
Circle::Circle():Shape(0,0,"Circle")
{
radius = 1.0;
}
Circle::Circle(int x, int y, double r):Shape(x, y, "Circle")
{
setRadius(r);
}
void Circle::setRadius(double r)
{
if(r>0)
radius = r;
else radius = 1.0;
}
void Circle::print()
{
Shape::print();
cout<<" with a radius of "<<radius<<" has an area of "<<calcArea();
}
double Circle::calcArea()
{
return 3.14*radius*radius;
}
Rectangle.h
/*
* Rectangle.h
*
* Created on: 06-May-2017
* Author: kasturi
*/
#ifndef SHAPES_RECTANGLE_H_
#define SHAPES_RECTANGLE_H_
#include "Shape.h"
class Rectangle:public Shape
{
private:
double width;
double height;
public:
Rectangle();
Rectangle(double , double, int, int );
void setWidth(double);
void setHeight(double);
void print();
double calcArea();
};
#endif /* SHAPES_RECTANGLE_H_ */
Rectangle.cpp
/*
* Rectangle.cpp
*
* Created on: 06-May-2017
* Author: kasturi
*/
#include <iostream>
#include "Rectangle.h"
#include "Shape.h"
using namespace std;
Rectangle::Rectangle():Shape(0,0,"Rectangle")
{
width = 1.0;
height = 1.0;
}
Rectangle::Rectangle(double w, double h, int x, int y):Shape(x,y,"Rectangle")
{
setWidth(w);
setHeight(h);
}
void Rectangle::print()
{
Shape::print();
cout<<" with a height of "<<height <<" and a width of "<<width<<" has an area of "<<calcArea();
}
void Rectangle::setHeight(double h)
{
if(h>0)
height = h;
else height = 1.0;
}
void Rectangle::setWidth(double w)
{
if(w>0)
width = w;
else width = 1.0;
}
double Rectangle::calcArea()
{
return width*height;
}
Prog12.cpp
/*
* Prog12.cpp
*
* Created on: 06-May-2017
* Author: kasturi
*/
#include <iostream>
#include "shape.h"
#include "Circle.h"
#include "Rectangle.h"
int main()
{
Shape shape1, shape2(2,3, "Cone");
shape1.print();
cout<<endl;
shape2.print();
cout<<endl;
shape1.setLocation(5, 6);
shape1.print();
cout<<endl;
Circle c1, c2(3, 5, 2);
c1.print();
cout<<endl;
c2.print();
cout<<endl;
c1.setLocation(3,6);
c1.setRadius(3);
c1.print();
cout<<endl;
Rectangle r1,r2(3.2, 5.4, 6, 7);
r1.print();
cout<<endl;
r2.print();
cout<<endl;
r1.setLocation(8,9);
r1.setHeight(2.7);
r1.setWidth(8.6);
r1.print();
return 0;
}
OUTPUT:
unknown shape at (0,0)
Cone at (2,3)
unknown shape at (5,6)
Circle at (0,0) with a radius of 1 has an area of 3.14
Circle at (3,5) with a radius of 2 has an area of 12.56
Circle at (3,6) with a radius of 3 has an area of 28.26
Rectangle at (0,0) with a height of 1 and a width of 1 has an area of 1
Rectangle at (6,7) with a height of 5.4 and a width of 3.2 has an area of 17.28
Rectangle at (8,9) with a height of 2.7 and a width of 8.6 has an area of 23.22
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.