#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;
}
C++基础实验(考虑Point(点)、Circle(圆)、Rectangle(矩形)、Cylinder(圆柱体)、Cube(长方体)等的共性,使用类的继承实现以上类并在构造、析构函数中增加打印信息)
最新推荐文章于 2024-08-21 18:16:36 发布