C++基础实验(考虑Point(点)、Circle(圆)、Rectangle(矩形)、Cylinder(圆柱体)、Cube(长方体)等的共性,使用类的继承实现以上类并在构造、析构函数中增加打印信息)

#include<iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/*1.编写坐标系下的不同图形的信息类: 
Point(点)、Circle(圆)、Rectangle(矩形)、Cylinder(圆柱体)、Cube(长方体)等。
考虑他们的共性,使用类的继承实现以上类。
2.在题1构造、析构函数中增加打印信息,观察各类对象子类父类的构造析构调用顺序。*/  
using namespace std;
#include<Cmath>  
 
#define pi 3.1415926  
class Point 
{  
public:  
    double x,y;  
    Point(){
		x=0;y=0;
		cout<<"Point无参构造函数执行完毕(No Params function performs finished)"<<endl;
		}  
    Point(double x0,double y0) {
		x=x0; y=y0;
		cout<<"Point有参构造函数执行完毕(Params function performs finished)"<<endl;
		}   
    ~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){
		cout<<"Circle有参构造函数执行完毕(Params function performs finished)"<<endl;
	}
    ~Circle()  
    {
		cout<<"Circle析构函数执行完毕(Destructor function performs finished)"<<endl; 
    }  
    friend ostream &operator << (ostream & output, Circle & c);    
    double get_d(){return d;}  
};  

class Rectangle:public Point
{
public:
	Rectangle(double x,double y): Point(x,y){
		cout<<"Rectangle有参构造函数执行完毕(Params function performs finished)"<<endl;
	}
	~Rectangle()  
	{
		cout<<"Rectangle析构函数执行完毕(Destructor function performs finished)"<<endl; 
	}  
	friend ostream &operator << (ostream & output, Rectangle & r);     
};

class Cylinder: public Circle     
{  
private:  
    double h;  
public:  
    Cylinder(double xx,double yy,double dd,double hh): Circle (xx,yy,dd),h(hh){
		cout<<"Cylinder有参构造函数执行完毕(Params function performs finished)"<<endl;
	}  
    ~Cylinder()  
    {
		cout<<"Cylinder析构函数执行完毕(Destructor function performs finished)"<<endl; 
    }  
    friend ostream &operator << (ostream & output,Cylinder & c);    
    double get_h(){return h;}  
 
};  

class Cube: public Rectangle
{
private:
	double h;
public:
	Cube(double x,double y,double hh): Rectangle (x,y),h(hh){
		cout<<"Cube有参构造函数执行完毕(Params function performs finished)"<<endl;
	}  
	~Cube()  
	{
		cout<<"Cube析构函数执行完毕(Destructor function performs finished)"<<endl; 
	}  
	friend ostream &operator << (ostream & output,Cube & c);    
	double get_h(){return h;}  
};

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, Rectangle & r){
	output<<"矩形长为:"<<r.get_x()<<"矩形宽为:"<<r.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;     
}  
ostream &operator << (ostream & output,Cube & c)
{
	output<<"长方体的长为:"<<c.get_x()<<"长方体的宽为:"<<c.get_y()<<"长方体的高为:"<<c.get_h()<<endl;   
	return output; 
}
void f()
{ 
	Point p(1,1);  
    cout<<p;  
    Circle ci(1,2,6);  
    cout<<ci;  
    Rectangle r(3,4);
    cout<<r;
    Cylinder cy(1,2,3,4);  
    cout<<cy;  
    Cube cb(3,4,5);
    cout<<cb;
}
 
int main()  
{  
	f();
	system("pause");
    return 0;  
	
}  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值