+ -
当前位置:首页 → 问答吧 → C语言的一个编程题,求救

C语言的一个编程题,求救

时间:2011-12-08

来源:互联网

声明一个rectangle类,包括数据成员l(长)和w(宽),求周长的函数成员getcir()类和求面积的函数成员getArea()。要求实现根据用户键盘输入的l和w值实例化一个rectangle类的对象,求出其周长和面积的输出。

作者: yo_yo2005   发布时间: 2011-12-08

这不会很难吧:

C/C++ code

class rectangle{

public :
 rectangle()
 rectangle(unsigned l, unsigned w);
 ~rectangle();

 float getcir();

 float getArea();

private:

 float length;
 float width;

};


 rectangle :: rectangle()
{
 length = 0;
 width = 0;
}

rectangle :: rectangle(float l, float w)
{
 length = l;
 width = w;
}

 float rectangle :: getcir()
{
  return 2*(width + length);
}

 float rectangle :: getArea()
{
  return width * length;
}

作者: Qoo_wzp   发布时间: 2011-12-08

补充一下:
C/C++ code

 rectangle(unsigned l, unsigned w);


改成
C/C++ code

 rectangle(float l, float w);


作者: Qoo_wzp   发布时间: 2011-12-08

C/C++ code

#include<iostream>
using namespace std;

class rectangle
{
private:
    double l;
    double w;

public:

    rectangle()
    {
        l = 0;
        w = 0;
    }

    rectangle(double dl, double dw)
    {
        l = dl;
        w = dw;
    }

    double getcir();
    
    double getArea();
};

double rectangle::getcir()
{
    return 2*(l+w);
}

double rectangle::getArea()
{
    return l*w;
}

int main(void)
{
    double l, w;

    cout << "请输入长和宽: ";
    cin >> l >> w;

    rectangle rect(l, w);

    cout << "周长: " << rect.getcir() << endl;

    cout << "面积: " << rect.getArea() << endl;

    return 0;
}

作者: wangliangffaflgh   发布时间: 2011-12-08

我是在DEV C++上 实现的 希望帮到您了
#include<iostream>
using namespace std;
class rectangle
{
public :
 rectangle(){};
 rectangle( float l, float w){length = l;width = w;};
 ~rectangle(){};
 float getcir();
 float getArea();
private:
static float length,width;
};
float rectangle :: length = 0;
float rectangle :: width = 0;
 float rectangle :: getcir()
{
  float g1;
  g1=2*(width + length);
  cout<<"周长="<<g1;
  return g1;
}
 float rectangle :: getArea()
{
  float g2;
  g2=width * length;
  cout<<"面积="<<g2;
  return g2;
}
int main()

  float l,w;
  cout<<"请输入长和宽:"<<endl;
  cin>>l>>w;
  rectangle c(l,w);
  c.getcir();
  c.getArea();
  cout<<endl;
  system("pause");
  return 0;  
}

作者: wowo55mimi   发布时间: 2011-12-08

热门下载

更多