注册 登录
编程论坛 C++教室

算长方形的面积和周长,错哪里了?请大神之处并修改。

_Strike 发布于 2016-04-19 17:32, 4507 次点击
#include <iostream>
using namespace std;
class rectangle{
public:
    rectangle(int len=10,int wid=10);
    rectangle(const rectangle &p);
    ~rectangle();
    void disp()
    {cout<<length<<"  "<<width<<endl;}
    void showarea();
    void setlengthandwidth();
private:
    int length,width;
};
rectangle::rectangle(int len, int wid)
{ length=len;
  width=wid;
  cout<<"Using  normal constructor\n";
}
rectangle::rectangle(const rectangle &p)
{ length=2*p.length;
  width=2*p.width;
  cout<<"Using copy constructor\n";
}
rectangle::~rectangle()
{cout<<"destructing..."<<endl;
}
rectangle::showarea()
{cout<<"该矩形的面积为:"<< length*width<<endl;}
rectangle::setlengthandwidth()
{cout<<"该矩形的周长为:"<<(length+width)*2<<endl;}
int main()
{rectangle rec1(5,5);
cout<<"rectangle1 output1:"<<endl;
rec1.showarea();
rec1.setlengthandwidth();
return 0;
}
2 回复
#2
我不信2016-04-19 20:37
程序代码:
#include <iostream>

 using namespace std;

 class rectangle{

 public:
     rectangle(int len=10,int wid=10);
     rectangle(const rectangle &p);
     ~rectangle();
     void disp()
     {cout<<length<<"  "<<width<<endl;}
     void showarea();
     void setlengthandwidth();

 private:
     int length,width;

 };

 rectangle::rectangle(int len, int wid)

 { length=len;
   width=wid;
   cout<<"Using  normal constructor\n";

 }

 rectangle::rectangle(const rectangle &p)

 { length=2*p.length;
   width=2*p.width;
   cout<<"Using copy constructor\n";

 }

 rectangle::~rectangle()

 {cout<<"destructing..."<<endl;

 }

 void rectangle::showarea()

 {cout<<"该矩形的面积为:"<< length*width<<endl;}

 void rectangle::setlengthandwidth()

 {cout<<"该矩形的周长为:"<<(length+width)*2<<endl;}

 int main()

 {rectangle rec1(5,5);

 cout<<"rectangle1 output1:"<<endl;

 rec1.showarea();

 rec1.setlengthandwidth();

 return 0;

 }
外面定义周长和面积函数时候少了函数类型
#3
_Strike2016-04-19 22:20
好的 谢谢啦
1