第十周实验指导--任务3--先建立一个Point(点)类,再派生出一个Circle(圆)类,再派生出一个Cylinder(圆柱体)类

时间:2022-09-07 19:16:16

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

* 作    者:         雷恒鑫                       
* 完成日期:     2012    年 04      月  21    日
* 版 本 号:       V1.0   
* 对任务及求解方法的描述部分
* 输入描述: 
* 问题描述: 
* 程序输出: 

* 程序头部的注释结束

*/

#include<iostream.h>
#include<Cmath>

#define pi 3.1415926
class Point //定义坐标点类
{
public:
	double x,y;   //点的横坐标和纵坐标
	Point(){x=0;y=0;}
	Point(double x0,double y0) {x=x0; y=y0;} 
	~Point ()
	{
		cout<<"析构函数执行完毕(Destructor function performs finished)"<<endl;
	}
	double get_x(){return x;}
	double get_y(){return y;}
	friend ostream &operator << (ostream & output, Point & c);  
}; 

class Circle: public Point   //利用坐标点类定义圆类, 其基类的数据成员表示圆的中心
{
private:
	double d;
public:
	Circle(double xx,double yy,double dd): Point(xx,yy) ,d(dd){}//构造函数
	~Circle()
	{
	}
	friend ostream &operator << (ostream & output, Circle & c);  
	double get_d(){return d;}
};
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 & output,Cylinder & c);  
	double get_h(){return h;}
	double superficial_area();     //表面积
	double volume();    //体积
};
ostream &operator << (ostream & output, Point & c)
{
	output<<"点的横坐标为:"<<c.x<<"     "<<"点的纵坐标为:"<<c.y<<endl;  
    return output;  
}  
ostream &operator << (ostream & output, Circle & c)
{
	output<<"圆的半径为:"<<c.get_d()<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;  
    return output;  
}
ostream &operator << (ostream & output,Cylinder & c)
{
	output<<"圆的高为:"<<c.get_h()<<"圆的半径为:"<<c.get_d()<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl; 
	return output;   
}
double  Cylinder::superficial_area()     //表面积
{
	double s;
	s=2*pi*get_d()*get_d()+2*pi*get_d()*get_h();
	return s;
}
double  Cylinder::volume()   //体积
{
	double v;
	v=pi*get_d()*get_d()*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;
	return 0;
	
	
	
}

运行结果:

第十周实验指导--任务3--先建立一个Point(点)类,再派生出一个Circle(圆)类,再派生出一个Cylinder(圆柱体)类






下面的程序能够看出析构函数的执行过程:

#include<iostream>  
using namespace std;
#include<Cmath>  

#define pi 3.1415926  
class Point //定义坐标点类  
{  
public:  
    double x,y;   //点的横坐标和纵坐标  
    Point(){x=0;y=0;}  
    Point(double x0,double y0) {x=x0; y=y0;}   
    ~Point ()  
    {  
        cout<<"Point析构函数执行完毕(Destructor function performs finished)"<<endl;  
    }  
    double get_x(){return x;}  
    double get_y(){return y;}  
    friend ostream &operator << (ostream & output, Point & c);    
};   

class Circle: public Point   //利用坐标点类定义圆类, 其基类的数据成员表示圆的中心  
{  
private:  
    double d;  
public:  
    Circle(double xx,double yy,double dd): Point(xx,yy) ,d(dd){}//构造函数  
    ~Circle()  
    {
		cout<<"Circle析构函数执行完毕(Destructor function performs finished)"<<endl; 
    }  
    friend ostream &operator << (ostream & output, Circle & c);    
    double get_d(){return d;}  
};  
class Cylinder: public Circle     
{  
private:  
    double h;  
public:  
    Cylinder(double xx,double yy,double dd,double hh): Circle (xx,yy,dd),h(hh){} //构造函数  
    ~Cylinder()  
    {
		cout<<"Cylinder析构函数执行完毕(Destructor function performs finished)"<<endl; 
    }  
    friend ostream &operator << (ostream & output,Cylinder & c);    
    double get_h(){return h;}  
    double superficial_area();     //表面积  
    double volume();    //体积  
};  
ostream &operator << (ostream & output, Point & c)  
{  
    output<<"点的横坐标为:"<<c.x<<"     "<<"点的纵坐标为:"<<c.y<<endl;    
    return output;    
}    
ostream &operator << (ostream & output, Circle & c)  
{  
    output<<"圆的半径为:"<<c.get_d()<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;    
    return output;    
}  
ostream &operator << (ostream & output,Cylinder & c)  
{  
    output<<"圆的高为:"<<c.get_h()<<"圆的半径为:"<<c.get_d()<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;   
    return output;     
}  
double  Cylinder::superficial_area()     //表面积  
{  
    double s;  
    s=2*pi*get_d()*get_d()+2*pi*get_d()*get_h();  
    return s;  
}  
double  Cylinder::volume()   //体积  
{  
    double v;  
    v=pi*get_d()*get_d()*get_h();  
    return v;  
} 
void f()
{ 
	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;  
}

int main()  
{  
	f();
	system("pause");
    return 0;  
	
}  

运行结果:

第十周实验指导--任务3--先建立一个Point(点)类,再派生出一个Circle(圆)类,再派生出一个Cylinder(圆柱体)类