(1)定义一个Rectangle类,它包括两个数据成员长len和宽width,以及求面积的成员函数Area,另外定义Set函数对私有数据成员进行设置。在主函数定义一个对象,输出其面积。(可自行增加需要

#include<iostream>
using namespace std;
class Point
{
  public:
    Point()
{
len=0;
width=0;
}
Point(float len,float width):len(len),width(width)
{

}
void printInfor()
{
cout<<"len:"<<len<<endl<<"width:"<<width<<endl;
}
void setLen(float len)
{
  this->len=len;
}
void setWidth(float width)
{
this->width=width;
}
float getLen()
{
return len;
}
float getWidth()
{
return width;
}
  private:
  float len;
  float width;
};
class Rectangle 
{
private:
float len;
float width;
public:
Rectangle()
{
len=0;
width=0;
}
Rectangle(float len,float width):p(len,width)
{
}
void printInfor()
{
p.printInfor();

void Area(float len,float width)
{
float area;
area=len*width;
cout<<"Area:"<<area;
}
~Rectangle()
{
}
void setP(float len,float width)//将值传到point类里 
{
p.setLen(len);
p.setWidth(width);
}
void setNewL(float Newlen)
{
len=Newlen;
p.setLen(len); 
}
void setNewW(float Newwidth)
{
width=Newwidth;
p.setWidth(width); 
}
Point getP()
{
return p;
}
float getNewL()
{
return len;
}
float getNewW()
{
return width;
}
private:
Point p;
};
int main()
{
float len,width;
float a,b;
cout<<"输入长宽:"; 
cin>>a>>b;
Rectangle r1;
r1.printInfor() ;
r1.setP(a,b);//传值 
r1.printInfor() ;
r1.Area(a,b);//求面积 
cout<<endl;
cout<<"输入长宽:"; 
cin>>a>>b;
r1.setNewL(a);
r1.setNewW(b);   
r1.printInfor();
r1.Area(a,b); 
return 0;    

}



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值