第十周任务三(多重派生的构造函数定义)

/*(程序头部注释开始)   
程序的版权和版本声明部分   
Copyright (c) 2011, 烟台大学计算机学院学生    
All rights reserved.   
文件名称:  多重派生的构造函数定义                              
作    者:   计114-4 冯珍珍       
完成日期:    2012  年  4 月  23  日   
版 本 号:       ly--w     
对任务及求解方法的描述部分     
问题描述: *(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员 (半径);
(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。
要求编写程序,设计出各类中基本的成员函数(包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等),使之能用于处理以上类对象,最后求出圆格柱体的表面积、体积并输出。
(提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)
*/    


#include<iostream> 
using namespace std;
#include<Cmath>    
#define PI 3.1415926//定义符号常量  
class Point //定义坐标点类  
{  
private:  
    double x;//横坐标
	double y;//纵坐标 
public:
    Point(double x0,double y0) {x=x0; y=y0;}   
    ~Point ()
	{cout<<"Destructor called"<<endl;}
	double get_x(){return x;} //公用基类的私有数据成员在派生类中不能访问,需要获取私有数据成员的公共接口 
    double get_y(){return y;}  
    friend ostream &operator << (ostream &, Point &);//声明运算符"<<"的重载    
}; 
  
//利用坐标点类定义圆类, 其基类的数据成员表示圆的中心  
class Circle: public Point   
{  
private:  
    double r;  
public:  
    Circle(double xx,double yy,double dd): Point(xx,yy)
	{
		r=dd;
	}
    ~Circle()
	{
	}
    friend ostream &operator << (ostream &, Circle &);    
    double get_r(){return r;}  
};  
class Cylinder: public Circle     
{  
private:  
    double h;  
public:  
    Cylinder(double xx,double yy,double dd,double hh): Circle (xx,yy,dd)
	{
		h=hh;
	}
	~Cylinder()  {}
    friend ostream &operator << (ostream &,Cylinder &);    
    double get_h(){return h;}  
    double superficial_area();//表面积  
    double volume();//圆柱体的体积  
}; 
 
ostream &operator << (ostream & output, Point & c)  
{  
    output<<"点的横坐标为:"<<c.x<<endl;
	output<<"点的纵坐标为:"<<c.y<<endl;    
    return output;    
}    
ostream &operator << (ostream & output, Circle & c)  
{  
    output<<"圆的半径为:"<<c.get_r()<<endl;
	output<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;    
    return output;    
}  
ostream &operator << (ostream & output,Cylinder & c)  
{  
    output<<"圆柱的高为:"<<c.get_h()<<endl;
	output<<"圆柱底面圆的半径为:"<<c.get_r()<<endl;
	output<<"圆柱底面圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;   
    return output;     
} 

//圆柱体的表面积  
double  Cylinder::superficial_area()
{     
     double s=2*PI*get_r()*get_r()+2*PI*get_r()*get_h();
     return s;
}

//圆柱体的体积   
double  Cylinder::volume()   
{    
    double v=PI*get_r()*get_r()*get_h();  
    return v;  
}  
int main()  
{  
    Point p(1,1);  
    cout<<p;  
    Circle ci(1,2,6);  
    cout<<ci;  
    Cylinder cy(1,2,3,4);  
    cout<<cy;  
    cout<<"圆柱的体积为:"<<cy.volume ()<<endl;  
    cout<<"圆柱的表面积为:"<<cy.superficial_area ()<<endl; 
   system("pause");  

    return 0;  
} 



以下为运算结果:
点的横坐标为:1
点的纵坐标为:1
圆的半径为:6
圆的圆心为(1,2)
圆柱的高为:4
圆柱底面圆的半径为:3
圆柱底面圆的圆心为(1,2)
圆柱的体积为:113.097
圆柱的表面积为:131.947
请按任意键继续. . .

小结:1.在类中队派生类构造函数声明是,不包括基类构造函数名及其参数表列 
      2.多成派生的构造函数在定义时,只需写出其上一层派生类的构造函数即可
	  





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值