| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4253 人关注过本帖
标题:算长方形的面积和周长,错哪里了?请大神之处并修改。
只看楼主 加入收藏
_Strike
Rank: 2
等 级:论坛游民
帖 子:133
专家分:72
注 册:2016-3-22
结帖率:90.63%
收藏
已结贴  问题点数:15 回复次数:2 
算长方形的面积和周长,错哪里了?请大神之处并修改。
#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;
}
搜索更多相关主题的帖子: private include public normal 长方形 
2016-04-19 17:32
我不信
Rank: 2
等 级:论坛游民
威 望:1
帖 子:33
专家分:24
注 册:2016-3-3
收藏
得分:15 
程序代码:
#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;

 } 
外面定义周长和面积函数时候少了函数类型
2016-04-19 20:37
_Strike
Rank: 2
等 级:论坛游民
帖 子:133
专家分:72
注 册:2016-3-22
收藏
得分:0 
好的 谢谢啦
2016-04-19 22:20
快速回复:算长方形的面积和周长,错哪里了?请大神之处并修改。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015489 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved