课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/8894095
【项目4】按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,并且完成要求的计算任务:
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员 (半径);
(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。
要求编写程序,设计出各类中基本的成员函数(包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等),使之能用于处理以上类对象,最后求出圆格柱体的表面积、体积并输出。
(提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)
参考解答:
(1) 基类Point类及用于测试的main()函数
#include <iostream> using namespace std; class Point { public: Point(double x=0,double y=0); //构造函数 void setPoint(double,double); //设置坐标值 double getX( ) const { return x; //读x坐标 } double getY( ) const { return y; //读y坐标 } friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<” protected: //受保护成员 double x,y; }; //Point的构造函数 Point::Point(double a,double b) { x=a; y=b; } //设置x和y的坐标值 void Point::setPoint(double a,double b) { x=a; y=b; } ostream & operator<<(ostream &output,const Point &p) { output<<"["<<p.x<<","<<p.y<<"]"<<endl; return output; } int main( ) { Point p(3.5,6.4); cout<<"x="<<p.getX( )<<",y="<<p.getY( )<<endl; p.setPoint(8.5,6.8); cout<<"p:"<<p<<endl; return 0; }
(2) 声明派生类Circle及其测试函数
#include <iostream> using namespace std; class Point { public: Point(double x=0,double y=0); //构造函数 void setPoint(double,double); //设置坐标值 double getX( ) const { return x; //读x坐标 } double getY( ) const { return y; //读y坐标 } friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<” protected: //受保护成员 double x,y; }; //Point的构造函数 Point::Point(double a,double b) { x=a; y=b; } //设置x和y的坐标值 void Point::setPoint(double a,double b) { x=a; y=b; } ostream & operator<<(ostream &output,const Point &p) { output<<"["<<p.x<<","<<p.y<<"]"<<endl; return output; } class Circle:public Point //circle是Point类的公用派生类 { public: Circle(double x=0,double y=0,double r=0); //构造函数 void setRadius(double); //设置半径值 double getRadius( ) const; //读取半径值 double area ( ) const; //计算圆面积 friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<” protected: double radius; }; //定义构造函数,对圆心坐标和半径初始化 Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { } //设置半径值 void Circle::setRadius(double r) { radius=r; } //读取半径值 double Circle::getRadius( ) const { return radius; } //计算圆面积 double Circle::area( ) const { return 3.14159*radius*radius; } //重载运算符“<<”,使之按规定的形式输出圆的信息 ostream &operator<<(ostream &output,const Circle &c) { output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius<<", area="<<c.area( )<<endl; return output; } int main( ) { Circle c(3.5,6.4,5.2); cout<<"original circle:\nx="<<c.getX()<<", y="<<c.getY()<<", r="<<c.getRadius( ) <<", area="<<c.area( )<<endl; c.setPoint(5,5); cout<<"new circle:\n"<<c; return 0; }
(3) 声明Circle的派生类Cylinder及测试函数
#include <iostream> using namespace std; class Point { public: Point(double x=0,double y=0); //构造函数 void setPoint(double,double); //设置坐标值 double getX( ) const { return x; //读x坐标 } double getY( ) const { return y; //读y坐标 } friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<” protected: //受保护成员 double x,y; }; //Point的构造函数 Point::Point(double a,double b) { x=a; y=b; } //设置x和y的坐标值 void Point::setPoint(double a,double b) { x=a; y=b; } ostream & operator<<(ostream &output,const Point &p) { output<<"["<<p.x<<","<<p.y<<"]"<<endl; return output; } class Circle:public Point //circle是Point类的公用派生类 { public: Circle(double x=0,double y=0,double r=0); //构造函数 void setRadius(double); //设置半径值 double getRadius( ) const; //读取半径值 double area ( ) const; //计算圆面积 friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<” protected: double radius; }; //定义构造函数,对圆心坐标和半径初始化 Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { } //设置半径值 void Circle::setRadius(double r) { radius=r; } //读取半径值 double Circle::getRadius( ) const { return radius; } //计算圆面积 double Circle::area( ) const { return 3.14159*radius*radius; } //重载运算符“<<”,使之按规定的形式输出圆的信息 ostream &operator<<(ostream &output,const Circle &c) { output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius<<", area="<<c.area( )<<endl; return output; } class Cylinder:public Circle { public: Cylinder (double x=0,double y=0,double r=0,double h=0);//构造函数 void setHeight(double); //设置圆柱高 double getHeight( ) const; //读取圆柱高 double area( ) const; //计算圆表面积 double volume( ) const; //计算圆柱体积 friend ostream& operator<<(ostream&,const Cylinder&);//重载运算符“<<” protected: double height; //圆柱高 }; //定义构造函数 Cylinder::Cylinder(double a,double b,double r,double h) :Circle(a,b,r),height(h) {} //设置圆柱高 void Cylinder::setHeight(double h) { height=h; } //读取圆柱高 double Cylinder::getHeight( ) const { return height; } //计算圆柱表面积 double Cylinder::area( ) const { return 2*Circle::area( )+2*3.14159*radius*height; } //计算圆柱体积 double Cylinder::volume() const { return Circle::area()*height; } //重载运算符“<<” ostream &operator<<(ostream &output,const Cylinder& cy) { output<<"Center=["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height <<"\narea="<<cy.area( )<<", volume="<<cy.volume( )<<endl; return output; } int main( ) { Cylinder cy1(3.5,6.4,5.2,10); cout<<"\noriginal cylinder:\nx="<<cy1.getX( )<<", y="<<cy1.getY( )<<", r=" <<cy1.getRadius( )<<", h="<<cy1.getHeight( )<<"\narea="<<cy1.area() <<",volume="<<cy1.volume()<<endl; cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5,5); cout<<"\nnew cylinder:\n"<<cy1; return 0; }