Defined the class circleType to implement the basic properties of a circle. (Add
ID: 3832225 • Letter: D
Question
Defined the class circleType to implement the basic properties of a circle. (Add the function print to the is class to output the radius, area, and circumference of a circle.) Every cylinder has a base and height, where the base is a circle. Design a class cylinderType that can capture the properties of a cylinder and perform the usual operations on the cylinder. Derive this class from the class circleType designed in Chapter 10. Some of the operations that can be performed on a cylinder are as follows: calculate and print the volume, calculate and print the surface area, set the height, set the radius of the base, and set the center of the base. Also, write a program to test various operations on a cylinder.
Create a class pointType to define a point as having two float variables, x and y. Implement, at minimum, the following functions for your point class.
· default constructor
· constructor with two parameters
· setPoint
· setX
· setY
· getPoint
· getX
· getY
Use your pointType class to create a point at the center of a circleType, as described above. circleType will have two variables, one a pointType and the other a float, radius. (This is an example of composition or aggregation. A circle “has a” center.)
Use your circleType class as the base class for the derived class cylinderType, as described above. Keep in mind that, when, for example, setting the center of the base of a cylinder you are setting a point. (This is a slight stretch of the definition of inheritance since, technically we do not say that a cylinder “is a” circle, but write your cylinderType class as a derived class anyway.)
You should end up with four .cpp files and three .h files in your project. You should have the following header files: pointType.h, circleType.h, and cylinderType.h . You should have the following .cpp files: pointType.cpp, circleType.cpp, and cylinderType.cpp as well as the .cpp file for your main program.
Explanation / Answer
cylinderType.cpp
#include "cylinder.h"
const double PI = 3.14159;
// Constructors
Cylinder::Cylinder()
{
radius = 0;
height = 0;
}
Cylinder::Cylinder(double r, double h)
{
radius = r;
height = h;
}
// Accessors
double Cylinder::getRadius()
{
return radius;
}
double Cylinder::getHeight()
{
return height;
}
// Setters
void Cylinder::setRadius(double r)
{
radius = r;
}
void Cylinder::setHeight(double h)
{
height = h;
}
// Calculations
double Cylinder::area()
{
return volume() * 2 * PI * radius * radius;
}
double Cylinder::volume()
{
return PI * radius * radius * height;
}
Cylinder.h
using namespace std;
class Cylinder
{
public:
// Constructors
Cylinder();
Cylinder(double r, double h);
// Accessors
double getRadius();
double getHeight();
void setRadius(double r);
void setHeight(double h);
double area();
double volume();
private:
double radius;
double height;
};
Circle.cpp
#include <iostream>
#include <cstdlib>
#include "circle.h"
using namespace std;
Circle::Circle()
{
radius = 1.0;
}
Circle::Circle(double radi)
{
radius = radi;
}
void Circle::setRadius(double r)
{
radius = r;
}
double Circle::getRadius()
{
return radius;
}
double Circle::getArea()
{
return pi * radius * radius;
}
double Circle::getDiameter()
{
return radius * 2;
}
double Circle::getCircumference()
{
return 2 * pi * radius;
}
Circle.h
#include <iostream>
#include <cstdlib>
using namespace std;
class Circle
{
public:
Circle();
Circle(double radi);
void setRadius(double r);
double getRadius();
double getArea();
double getDiameter();
double getCircumference();
private:
double radius;
static const double pi = 3.14159;
};
#endif
pointType.cpp
#include "Point.h"
#include <iostream>
#include <cmath>
using namespace std;
// Constructor (default values can only be specified in the declaration)
Point::Point(float x, float y) : x(x), y(y) { } // Use member initializer list
// Public getter for private data member x
int Point::getX() const {
return x;
}
// Public setter for private data member x
void Point::setX(float x) {
this->x = x;
}
// Public getter for private data member y
int Point::getY() const {
return y;
}
// Public setter for private data member y
void Point::setY(float y) {
this->y = y;
}
// Public member function to set both x and y
void Point::setXY(float x, float y) {
this->x = x;
this->y = y;
}
// Public member function to return the magitude
double Point::getMagnitude() const {
return sqrt(x*x + y*y); // sqrt in <cmath>
}
// Public member function to return the argument
double Point::getArgument() const {
return atan2(y, x); // atan2 in <cmath>
}
// Public member function to print description about this point
void Point::print() const {
cout << "(" << x << "," << y << ")" << endl;
}
circle include pointType
{
circle::circle(float radius,flout area){10.0,10.0}
{
float point=radius+area/2;
return point;
}
};
Point.h
#ifndef POINT_H
#define POINT_H
// Point class declaration
class Point {
private:
// private data members (variables)
int x;
int y;
public:
// Declare member function prototypes
Point(int x = 0, int y = 0); // Constructor with default values
int getX() const;
void setX(int x);
int getY() const;
void setY(int y);
void setXY(int x, int y);
double getMagnitude() const;
double getArgument() const;
void print() const;
};
#endif
Main.cpp
#include <iostream>
#include <cylinder.h>
#include <circle.h>
using namespace std;
int main()
{
Cylinder CylinderOne(10.0, 10.0);
cout << "Cylinder 1 Radius: " << CylinderOne.getRadius() << endl;
cout << "Cylinder 1 Height: " << CylinderOne.getHeight() << endl;
cout << "Cylinder 1 volume: " << CylinderOne.volume() << endl;
cout << "Cylinder 1 area: " << CylinderOne.area() << endl;
cin.get();
for(;;)
{
try
{
double radius;
cout<<"Enter the radius: ";
cin>>radius;
Circle cir(radius);
cout<<"Radius: "<<cir.getRadius()<<endl;
cout<<"Area: "<<cir.getArea()<<endl;
cout<<"Diameter: "<<cir.getDiameter()<<endl;
cout<<"Circumference: "<<cir.getCircumference()<<endl;
break;
}
catch(double e)
{
cout<<"The radius cannot be 0.0"<<endl<<endl;
}
}
Point p1(3, 4);
p1.print();
cout << "x = " << p1.getX() << endl;
cout << "y = " << p1.getY() << endl;
cout << fixed << setprecision(2);
cout << "mag = " << p1.getMagnitude() << endl;
cout << "arg = " << p1.getArgument() << endl;
p1.setX(6);
p1.setY(8);
p1.print();
p1.setXY(1, 2);
p1.print();
// Construct an instance of Point using default constructor
Point p2;
p2.print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.