技术学习C++C++问题 C: C++类与对象——长方形类
Ramsayi题目描述
定义长方形类 rectangle,数据成员包括长 length 和宽 width,均为 double 类型,成员函数包括:
(1)构造函数 2 个,一个无参的构造函数,长和宽的默认值为 0,带两个参数的构造函数;
(2)设置长方形尺寸函数 assign,即任意输入 2 个实数,大的数赋值给长,小的数赋值给宽
(3)计算长方形周长函数 double circumference();
(4)计算长方形面积的函数 double area();
(5)输出长方形的长和宽
要求在 main 函数中创建 3 个长方形的对象,第 1,2 个长方形分别由无参构造函数和有参构造函数初始化实现,第 3 个长方形对象通过输入数据,调用设置长方形尺寸函数,给长宽赋值,然后分别计算 3 个长方形的周长和面积,并输出结果
输入
输入仅一行数据,输入 2 个实数,数据之间用空格分开
输出
3 个长方形输出 3 行数据,每行数据先输出长方形的长、宽,再输出它对应的周长和面积,小数点后保留 2 位数字,一行中 4 个数据之间用逗号分隔
样例输入
样例输出
| 12
 3
 
 | 0.00,0.00,0.00,0.002.00,1.00,6.00,2.00
 3.80,2.50,12.60,9.50
 
 | 
提示
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 
 | #include<iostream>   using namespace std;
 class rectangle{
 double length,width;
 double x,y;
 public:
 rectangle(){
 length=0;
 width=0;
 }
 rectangle(double a,double b){
 if(a>b) length=a,width=b;
 else length=b,width=a;
 }
 void assign(){
 cin>>x>>y;
 if(x>y) length=x,width=y;
 else length=y,width=x;
 }
 double circumference(){
 return (length+width)*2;
 }
 double area(){
 return length*width;
 }
 void lenwid(){
 cout.precision(2);
 cout<<fixed<<length<<','<<width<<',';
 }
 };
 int main(){
 rectangle m,n(2,1),p;
 p.assign();
 m.lenwid();
 cout<<m.circumference()<<','<<m.area()<<endl;
 n.lenwid();
 cout<<n.circumference()<<','<<n.area()<<endl;
 p.lenwid();
 cout<<p.circumference()<<','<<p.area()<<endl;
 return 0;
 }
 
 |