s2
时间:2010-09-13 来源:luozhiyong131
/*
面向对像,计算矩形周长,面积;
*/ #include <iostream.h> class Rectangle { private:
int width, height;
public:
void setWH(int w, int h){
width = w;
height = h;
} int getWidth(){
return width;
} int getHeight(){
return height;
} int getGrith(){
return (width + height) * 2;
} int getArea(){
return width * height;
}
}; void main()
{
class Rectangle r1,r2;
r1.setWH(10, 8);
cout << "矩形1:宽=" << r1.getWidth() << " 高=" << r1.getHeight() << endl;
cout << "周长=" << r1.getGrith() << endl;
cout << "面积=" << r1.getArea() << endl; r2.setWH(20, 18);
cout << "矩形2:宽=" << r2.getWidth() << " 高=" << r2.getHeight() << endl;
cout << "周长=" << r2.getGrith() << endl;
cout << "面积=" << r2.getArea() << endl;
}
面向对像,计算矩形周长,面积;
*/ #include <iostream.h> class Rectangle { private:
int width, height;
public:
void setWH(int w, int h){
width = w;
height = h;
} int getWidth(){
return width;
} int getHeight(){
return height;
} int getGrith(){
return (width + height) * 2;
} int getArea(){
return width * height;
}
}; void main()
{
class Rectangle r1,r2;
r1.setWH(10, 8);
cout << "矩形1:宽=" << r1.getWidth() << " 高=" << r1.getHeight() << endl;
cout << "周长=" << r1.getGrith() << endl;
cout << "面积=" << r1.getArea() << endl; r2.setWH(20, 18);
cout << "矩形2:宽=" << r2.getWidth() << " 高=" << r2.getHeight() << endl;
cout << "周长=" << r2.getGrith() << endl;
cout << "面积=" << r2.getArea() << endl;
}
相关阅读 更多 +