标签:
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接
【项目4 - 类族的设计】按下面的提示,由基类的设计和測试開始,逐渐地完毕各个类的设计,求出圆格柱体的表面积、体积并输出而且完毕要求的计算任务:
(1)先建立一个Point(点)类,包括数据成员x,y(坐标点),实现须要的成员函数,并设计main函数完毕測试;
(2)以Point为基类,派生出一个Circle(圆)类,添加数据成员r(半径),以及求面积的成员函数area,实现其它须要的成员函数,设计main函数完毕測试;
(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再添加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现须要的成员函数,并设计main函数完毕測试。
要求编敲代码,设计出各类中“须要的成员函数”,包括构造函数、析构函数、改动数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等。
(提示:此任务能够分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这样的方法适用于做不论什么的项目)
參考解答:
(1)先建立一个Point(点)类,包括数据成员x,y(坐标点),实现须要的成员函数,并设计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; }
#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; }
#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; }
=================== 迂者 贺利坚 CSDN博客专栏================= |== IT学子成长指导专栏 专栏文章的分类文件夹(不定期更新) ==| |== C++ 课堂在线专栏 贺利坚课程教学链接(分课程年级) ==| |== 我写的书——《逆袭大学——传给IT学子的正能量》 ==| ===== 为IT菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 ===== |
标签:
原文地址:http://www.cnblogs.com/gcczhongduan/p/4471151.html