源程序:
#include<iostream>
#include<Cmath>
//#define P 3.14 宏定义是c语言里的····
const double P = 3.14;//c++应该用const
using namespace std;
class Point //定义坐标点类
{
protected:
double x, y; //点的横坐标和纵坐标
public:
Point(){x=0;y=0;}
Point(double x0, double y0);
~Point();
double getx();
double gety();
};
class Circle: public Point//定义圆类
{
protected:
double r;//定义圆的半径
public:
Circle(){x=0; y=0; r=0;}
Circle(double x0, double y0, double r0);
~Circle();
double getr();
double Area();
double Grith();
};
class Cylinder:public Circle//定义圆柱类
{
protected:
double h;//定义圆柱类的高
public:
Cylinder(){x=0; y=0; r=0; h=0;}
Cylinder(double x0, double y0, double r0, double h0);
~Cylinder();
double geth();
double Volume();
double WArea();
friend istream& operator >> (istream& input, Cylinder &c1);
friend ostream& operator << (ostream& output, Cylinder &c1);
};
Point::Point(double x0, double y0):x(x0), y(y0){}
Point::~Point(){}
double Point::getx()
{
return x;
}
double Point::gety()
{
return y;
}
Circle::Circle(double x0, double y0, double r0):Point(x0, y0),r(r0){}
Circle::~Circle(){}
double Circle::getr()
{
return r;
}
double Circle::Area()
{
return P * getr() * getr();
}
double Circle::Grith()
{
return P * getr() * 2;
}
Cylinder::Cylinder(double x0, double y0, double r0, double h0):Circle(x0, y0, r0), h(h0){}
Cylinder::~Cylinder(){}
double Cylinder::geth()
{
return h;
}
double Cylinder::Volume()
{
return Area() * h;
}
double Cylinder::WArea()
{
return (2 * Area() + Grith() * h);
}
istream& operator >> (istream& input, Cylinder &c1)
{
char f;
while(1)
{
cout << "请输入一个圆柱底面○的圆心坐标(x和y之间用','隔开):";
input >> c1.x >> f >> c1.y;
if(f != ',')
{
cout << "输入格式有误。。。" << endl;
}
else
break;
}
cout << "请输入一个圆柱底面○的半径:";
input >> c1.r;
cout << "请输入一个圆柱的高:";
input >> c1.h;
return input;
}
ostream& operator << (ostream& output, Cylinder &c1)
{
output << "圆柱底面○的圆心为:" << "(" << c1.getx() << "," << c1.gety() << ")" << endl;
output << "圆柱底面○的半径为:" << c1.getr() << endl;
output << "圆柱底面○的面积为:" << c1.Area() << endl;
output << "圆柱底面○的周长为:" << c1.Grith() << endl;
output << "圆柱的高为:" << c1.geth() <<endl;
output << "圆柱的表面积为:" << c1.WArea() << endl;
output << "圆柱的体积为:" <<c1.Volume() << endl;
return output;
}
int main()
{
Cylinder c;
cin >> c;
cout << c;
system("pause");
return 0;
}
截图: