In C++ show and explain how to do this. Consider the following class definition:
ID: 3744271 • Letter: I
Question
In C++ show and explain how to do this.
Consider the following class definition:
class circle class cylinder: public circle
{ {
public: public:
void print() const; void print() const;
void setRadius(double); void setHeight(double);
void setCenter(double, double); double getHeight();
void getCenter(double&, double&); double volume();
double getRadius(); double area();
double area(); circle(); cylinder();
circle(double, double, double); cylinder(double, double,
double, double);
private: private:
double xCoordinate; double height;
double yCoordinate; }
double radius;
}
Suppose that you have the declaration:
cylinder newCylinder;
Write the definitions of the member functions of the classes circle and cylinder. Identify the member functions of the class cylinder that overrides the member functions of the class circle.
Explanation / Answer
If you have any doubts, please give me comment...
circle.h
#include<iostream>
#include<cmath>
using namespace std;
class circle{
public:
void print();
void setRadius(double);
void setCenter(double, double);
void getCenter(double &, double &);
double getRadius();
double area();
circle();
circle(double, double, double);
private:
double xCoordinate;
double yCoordinate;
double radius;
};
circle.cpp
void circle::print(){
cout<<"xCoordinate: "<<xCoordinate<<", yCoordinate: "<<yCoordinate<<", Radius: "<<radius<<endl<<"Area: "<<area()<<endl;
}
void circle::setRadius(double r){
radius = r;
}
void circle::setCenter(double x, double y){
xCoordinate = x;
yCoordinate = y;
}
void circle::getCenter(double &x, double &y){
x = xCoordinate;
y = yCoordinate;
}
double circle::getRadius(){
return radius;
}
double circle::area(){
return M_PI*radius*radius;
}
circle::circle(){
xCoordinate = 0;
yCoordinate = 0;
radius = 0;
}
circle::circle(double x, double y, double r){
xCoordinate = x;
yCoordinate = y;
radius = r;
}
cylinder.h
#include "circle.h"
class cylinder: public circle{
public:
void print();
void setHeight(double);
double getHeight();
double volume();
double area();
cylinder();
cylinder(double, double, double, double);
private:
double height;
};
cylinder.cpp
void cylinder::print(){
double x, y;
getCenter(x, y);
cout<<"xCoordinate: "<<x<<", yCoordinate: "<<y<<", getRadius(): "<<getRadius()<<", Height: "<<height<<endl<<"Area: "<<area()<<", Volume: "<<volume()<<endl;
}
void cylinder::setHeight(double h){
height = h;
}
double cylinder::getHeight(){
return height;
}
double cylinder::volume(){
return M_PI * getRadius() * getRadius() * height;
}
double cylinder::area(){
return (2*M_PI*getRadius()*height) + (2*M_PI*getRadius()*getRadius());
}
cylinder::cylinder():circle(){
height = 0;
}
cylinder::cylinder(double x, double y, double r, double h):circle(x, y, r){
height = h;
}