标签:
【项目4 - 类族的设计】(提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)
#include <iostream> using namespace std; class Point { protected: double x,y; public: Point(double x0,double y0):x(x0),y(y0){} double get_x() { return x; } double get_y() { return y; } void PrintPoint(); }; void Point::PrintPoint() { cout<<"Point: ("<<x<<","<<y<<")"<<endl; //输出点 } class Circle: public Point { protected: double r; public: Circle(double a,double b,double c):Point(a,b),r(c){} double set_r(); double Circle_area(); }; double Circle::set_r() { cout<<"请输入圆的半径:"; cin>>r; } double Circle::Circle_area() { return 3.14*r*r; } class Cylinder:public Circle { protected: double h; public: Cylinder(double a,double b,double c):Circle(a,b,c){} double set_h(); double Cylinder_area(); double Cylinder_volume(); }; double Cylinder::set_h() { cout<<"请输入圆柱体的高:"; cin>>h; } double Cylinder::Cylinder_area() { return (2*3.14*r*r+2*3.14*r*h); } double Cylinder::Cylinder_volume() { return 3.14*r*r*h; } int main() { Cylinder cd(2.0,5.0,3.0); cd.set_r(); cout<<"圆的面积为:"<<cd.Circle_area()<<endl; cd.set_h(); cout<<"圆柱体的表面积为:"<<cd.Cylinder_area()<<endl; cout<<"圆柱体的体积为:"<<cd.Cylinder_volume(); }运行结果:
标签:
原文地址:http://blog.csdn.net/zs9528/article/details/46053951