YTUOJ矩形类定义(复制类对象)

#include <iostream>

using namespace std;class Rectangle{private: double x1,x2,y1,y2;public:Rectangle(double a=0,double b=0,double c=0,double d=0);Rectangle (Rectangle&p); void input(); void output();

};Rectangle::Rectangle(double a,double b,double c,double d){ x1=a; y1=b; x2=c; y2=d;}void Rectangle::input(){ double a,b,c,d; cin>>a>>b>>c>>d; x1=a; y1=b; x2=c; y2=d;}void Rectangle::output(){ cout<<(x1-x2)*(y1-y2); cout<<endl;}Rectangle::Rectangle(Rectangle &p){ x1=p.x1; x2=p.x2; y1=p.y1; y2=p.y2;}

int main()

{

Rectangle p1;

p1.input();

p1.output();

Rectangle p2(p1);

p2.output();

Rectangle p3(1,1,6,3);

p3.output();

return 0;

}

#include <iostream>using namespace std;class Rectangle{private:double x1,x2,y1,y2;public:Rectangle(double a=0,double b=0,double c=0,double d=0);Rectangle (Rectangle&p);void input();void output();};Rectangle::Rectangle(double a,double b,double c,double d){x1=a;y1=b;x2=c;y2=d;}void Rectangle::input(){double a,b,c,d;cin>>a>>b>>c>>d;x1=a;y1=b;x2=c;y2=d;}void Rectangle::output(){cout<<(x1-x2)*(y1-y2);cout<<endl;}Rectangle::Rectangle(Rectangle &p){x1=p.x1;x2=p.x2;y1=p.y1;y2=p.y2;}int main(){Rectangle p1;p1.input();p1.output();Rectangle p2(p1);p2.output();Rectangle p3(1,1,6,3);p3.output();return 0;}学习总结:

Rectangle(double a=0,double b=0,double c=0,double d=0);//在声明构造函数时指定默认参数

Rectangle::Rectangle(double a,double b,double c,double d){ x1=a; y1=b; x2=c; y2=d;}//在定义构造函数时可以不指定默认参数

Rectangle p1;//没有给实参,此时才不出错

void Rectangle::input(){ double a,b,c,d; cin>>a>>b>>c>>d; x1=a; y1=b; x2=c; y2=d;}//input的格式,将a,b,c,d传值给x1,y1,x2,y2;而不能直接cin>>x1>>y1>>x2>>y2;

构造函数是赋初值,而input函数可以改变构造函数的初值

,最糟糕的行为是抱怨,最易见效 的努力是从自己做起。

YTUOJ矩形类定义(复制类对象)

相关文章:

你感兴趣的文章:

标签云: