n this problem, you will create an inheritance hierarchy for classes pointType,
ID: 3597955 • Letter: N
Question
n this problem, you will create an inheritance hierarchy for classes pointType, circleType, and cylinderType. Use the pointType class as a base class of hierarchy. The following files are provided to the problem.
header files: pointType.h, circleType.h and cylinderType.h
implementation files: pointTypeImp.cpp and circleTypeImp.cpp
main file: testmain.cpp
You need to implement the file cylinderTypeImp.cpp.
Create a Win 32 console application, add the header files, the implementation files including cylinderTypeImp.cpp and the testmain.cpp. Your output should shown as below:
***** Cylinder 1 *****
Base Center: (3.00, 2.50)
Base Radius: 4.00
Base Circumference: 25.13
Base Area: 50.27
Cylinder height: 2.50
Cylinder surface area: 62.83
Cylinder volume: 125.66
***** Cylinder 2 *****
Base Center: (-2.50, 7.00)
Base Radius: 4.00
Base Circumference: 25.13
Base Area: 50.27
Cylinder height: 3.90
Cylinder surface area: 98.02
Cylinder volume: 196.04
Enter x Coordinates of the center: 1
Enter y Coordinate of the center: 2
Enter base radius: 10.0
Enter cylinder height: 20.0
***** Cylinder 3 *****
Base Center: (1.00, 2.00)
Base Radius: 10.00
Base Circumference: 62.83
Base Area: 314.16
Cylinder height: 20.00
Cylinder surface area: 1256.64
Cylinder volume: 6283.20
Supplemental code
The header files, implementation files and the main files are provided below:
pointType.h
#ifndef H_PointType
#define H_PointType
class pointType
{
public:
void setPoint(double x, double y);
void print() const;
double getX() const;
double getY() const;
pointType(double x = 0.0, double y = 0.0);
protected:
double xCoordinate;
double yCoordinate;
};
#endif
pointTypeImp.cpp
//Implementation File pointTypeImp.cpp
#include <iostream>
#include "pointType.h"
using namespace std;
void pointType::setPoint(double x, double y)
{
xCoordinate = x;
yCoordinate = y;
}
void pointType::print() const
{
cout << "(" << xCoordinate << ", " << yCoordinate << ")" << endl;
}
double pointType::getX() const
{
return xCoordinate;
}
double pointType::getY() const
{
return yCoordinate;
}
pointType::pointType(double x, double y)
{
xCoordinate = x;
yCoordinate = y;
}
circleType.h
//Class circleType
#ifndef H_CircleType
#define H_CircleType
#include "pointType.h"
class circleType: public pointType
{
public:
void print() const;
void setRadius(double r);
double getRadius() const;
double getCircumference() const;
double getArea() const;
circleType(double x = 0.0, double y = 0.0, double r = 0.0);
protected:
double radius;
};
#endif
circleTypeImp.cpp
//Implementation file circleTypeImp.cpp
#include <iostream>
#include "circleType.h"
using namespace std;
void circleType::print() const
{
cout << "Center: ";
pointType::print();
cout << endl;
cout << "Radius: " << radius << endl;
cout << "Circumference: " << getCircumference() << endl;
cout << "Area: " << getArea() << endl;
}
void circleType::setRadius(double r)
{
radius = r;
}
double circleType::getRadius() const
{
return radius;
}
double circleType::getCircumference() const
{
return (2 * 3.1416 * radius);
}
double circleType::getArea() const
{
return (3.1416 * radius * radius);
}
circleType::circleType(double x, double y, double r)
:pointType(x,y)
{
radius = r;
}
cylinderType.h
//Class cylinderType
#ifndef H_CylinderType
#define H_CylinderType
#include "circleType.h"
class cylinderType: public circleType
{
public:
void print() const;
void setHeight(double h);
void setBaseCenter(double x, double y);
void setCenterRadiusHeight(double x, double y,
double r, double h);
double getHeight() const;
double getVolume() const;
double getSurfaceArea() const;
cylinderType(double x = 0.0, double y = 0.0,
double r = 0.0, double h = 0.0);
protected:
double height;
};
#endif
#include "cylinderType.h"
using namespace std;
int main()
{
cylinderType cylinder1(3, 2.5, 4, 2.5);
cylinderType cylinder2;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "***** Cylinder 1 *****" << endl;
cylinder1.print();
cout << endl;
cylinder2.setPoint(-2.5, 7);
cylinder2.setRadius(4);
cylinder2.setHeight(3.9);
cout << "***** Cylinder 2 *****" << endl;
cylinder2.print();
cout << endl;
double x, y;
double r;
double h;
cylinderType cylinder3;
cout << "Enter x Coordinates of the center: ";
cin >> x;
cout << endl;
cout << "Enter y Coordinate of the center: ";
cin >> y;
cout << endl;
cout << "Enter base radius: ";
cin >> r;
cout << endl;
cout << "Enter cylinder height: ";
cin >> h;
cout << endl;
cylinder3.setCenterRadiusHeight(x, y, r, h);
cout << "***** Cylinder 3 *****" << endl;
cylinder3.print();
cout << endl;
return 0;
}
Explanation / Answer
//save below code as pointType.h
#ifndef POINTTYPE_H
#define POINTTYPE_H
class pointType {
private:
double x;
double y;
public:
pointType();
void setX(double _x);
void setY(double _Y);
double getX();
double getY();
void printCoordinate();
};
#endif // !POINTTYPE_H
/*----------------------------------------------------------------------------------------------------------------------*/
/*Save below code as pointTypeImp.cpp */
#include "pointType.h"
#include <iostream>
pointType::pointType() : x(5), y(5) {}
void pointType::setX(double _x) {
x = _x;
}
void pointType::setY(double _y) {
y = _y;
}
double pointType::getX() {
return x;
}
double pointType::getY() {
return y;
}
void pointType::printCoordinate() {
std::cout << "Co-ordinate : (" << x << " , " << y << ")" << " ";
}
/*------------------------------------------------------------------------------------------------------*/
/* Save below code as circleType.h */
#ifndef CIRCLETYPE_H
#define CIRCLETYPE_H
#include "pointType.h"
class circleType : public pointType {
private:
double radius;
public:
circleType();
void setRadius(double _radius);
double getRadius();
void printArea();
void printCircumference();
};
#endif // !CIRCLETYPE_H
/*---------------------------------------------------------------------------------------------------------*/
/* Save below code as circleTypeImp.cpp */
#include "circleType.h"
#include <iostream>
circleType::circleType() : radius(10) {}
void circleType::setRadius(double _radius) {
radius = _radius;
}
double circleType::getRadius() {
return radius;
}
void circleType::printArea() {
std::cout << "Area of circle : " << 3.1416 * radius * radius << " ";
}
void circleType::printCircumference() {
std::cout << "Circumference of circle : " << 2 * 3.1416 * radius << " ";
}
/*--------------------------------------------------------------------------------------------------*/
/* Below is the main function to test our class save it with any name but make sure it ended with .cpp extension */
#include "circleType.h"
#include <iostream>
using namespace std;
int main() {
circleType circle1;
cout << "Default circle data : ";
cout << "Center ";
circle1.printCoordinate();
cout << "Radius : " << circle1.getRadius() << endl;
circle1.printCircumference();
circle1.printArea();
//setting some data
cout << endl;
cout << endl;
circle1.setX(100);
circle1.setY(100);
circle1.setRadius(200);
cout << "After setting some data : ";
cout << "Center ";
circle1.printCoordinate();
cout << "Radius : " << circle1.getRadius() << endl;
circle1.printCircumference();
circle1.printArea();
cout << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.