#include<iostream>
using namespace std;
class Rectangle{
private:
double height;
double width;
public:
Rectangle(double newheight=1,double newwidth=1){
height=newheight;
width=newwidth;
cout<<"Constructor is called"<<endl;
}
void set(double newheight,double newwidth){
height=newheight;
width=newwidth;
}
double getHeight(){
return height;
}
double getWidth(){
return width;
}
double getArea(){
return height*width;
}
double getPerimeter(){
return 2*(height+width);
}
};
int main(){
Rectangle r1;
Rectangle r2(3,4);
cout<<r1.getHeight()<<" "<<r1.getWidth()<<endl;
r1.set(40,4);
cout<<r1.getHeight()<<" "<<r1.getWidth()<<endl;
cout<<r2.getArea()<<" "<<r2.getPerimeter()<<endl;
return 0;
}
此题void set()函数没有使用&,但是为什么可以改变r1的值