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

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

}
void printInfor()
{
cout<<"len:"< }
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:"< }
~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< cout<<"输入长宽:"; 
cin>>a>>b;
r1.setNewL(a);
r1.setNewW(b);   
r1.printInfor();
r1.Area(a,b); 
return 0;    

}


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

 

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