| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 504 人关注过本帖
标题:[求助]参数传递问题
只看楼主 加入收藏
norlan
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-5-3
收藏
 问题点数:0 回复次数:5 
[求助]参数传递问题

不知道这个标题是否对~~
下面是个求矩形周长及面积的程序
原来是这样的:
#include<iostream.h>
class Rectangle
{
public:
void SetRectangle (double top,double left,double bottom,double right);
double GetTop(){return itsTop;}
double GetLeft(){return itsLeft;}
double GetBottom(){return itsBottom;}
double GetRight(){return itsRight;}
double Perimeter();
double Area();
private:
double itsTop,itsLeft,itsBottom,itsRight;
};

void Rectangle::SetRectangle (double top,double left,double bottom,double right)
{
itsTop=top;
itsLeft=left;
itsBottom=bottom;
itsRight=right;
}
double Rectangle::Perimeter()
{
double Width,Height;
Width=itsRight-itsLeft;
Height=itsTop-itsBottom;
return(2*(Width+Height));
}
double Rectangle::Area()
{
double Width,Height;
Width=itsRight-itsLeft;
Height=itsTop-itsBottom;
return (Width*Height);
}
int main()
{
Rectangle Rect;
Rect.SetRectangle(6.3,2.1,3.2,5.2);
cout<<"左下角与右上角坐标分别为("<<Rect.GetLeft()<<","<<Rect.GetBottom()<<")、("<<Rect.GetRight()<<","<<Rect.GetTop()<<")的矩形";
cout<<endl;
cout<<"周长:"<<Rect.Perimeter()<<endl;
cout<<"面积:"<<Rect.Area()<<endl;
return 0;
}
但我想改成自己输入参数的,于是就: (但编译没通过,正确的应该是怎样的?)
#include<iostream.h>
class Rectangle
{
public:
void SetRectangle (double top,double left,double bottom,double right);
double GetTop(){return itsTop;}
double GetLeft(){return itsLeft;}
double GetBottom(){return itsBottom;}
double GetRight(){return itsRight;}
double Perimeter();
double Area();
private:
double itsTop,itsLeft,itsBottom,itsRight;
};

void Rectangle::SetRectangle (double top,double left,double bottom,double right)
{
cout<<"请按顺序输入左下标和右上标"<<endl;
cin>>top>>left>>bottom>>right;
itsTop=top;
itsLeft=left;
itsBottom=bottom;
itsRight=right;
}
double Rectangle::Perimeter()
{
double Width,Height;
Width=itsRight-itsLeft;
Height=itsTop-itsBottom;
return(2*(Width+Height));
}
double Rectangle::Area()
{
double Width,Height;
Width=itsRight-itsLeft;
Height=itsTop-itsBottom;
return (Width*Height);
}
int main()
{
Rectangle Rect;
Rect.SetRectangle(top,left,bottom,right);
cout<<"左下角与右上角坐标分别为("<<Rect.GetLeft()<<","<<Rect.GetBottom()<<")、("<<Rect.GetRight()<<","<<Rect.GetTop()<<")的矩形";
cout<<endl;
cout<<"周长:"<<Rect.Perimeter()<<endl;
cout<<"面积:"<<Rect.Area()<<endl;
return 0;
}

[此贴子已经被作者于2007-5-12 14:25:44编辑过]

搜索更多相关主题的帖子: 参数 
2007-05-12 14:09
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 

建议用运算符重载.
能得到如下的效果:S = (width+height)*2;


=×&D o I p R e E n C g T l X&×=
2007-05-12 17:31
norlan
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-5-3
收藏
得分:0 
不明白
不过还是谢谢你啦


是不是我的程序太长了
2007-05-12 20:59
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
SetRectangle()
写成这样,不要参数.
void Rectangle::SetRectangle ()
{
int top, left, bottom, right;
cout<<"请按顺序输入左下标和右上标"<<endl;
cin>>top>>left>>bottom>>right;
itsTop=top;
itsLeft=left;
itsBottom=bottom;
itsRight=right;
}

=×&D o I p R e E n C g T l X&×=
2007-05-12 22:12
norlan
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-5-3
收藏
得分:0 
回复:(踏魔狼)SetRectangle()写成这样,不要参数....

像你这样改的话还是有错喔~·

c:\documents and settings\administrator.31e049c0ddeb450\桌面\cpp1.cpp(17) : error C2511: 'SetRectangle' : overloaded member function 'void (void)' not found in 'Rectangle'
c:\documents and settings\administrator.31e049c0ddeb450\桌面\cpp1.cpp(3) : see declaration of 'Rectangle'
c:\documents and settings\administrator.31e049c0ddeb450\桌面\cpp1.cpp(42) : error C2065: 'top' : undeclared identifier
c:\documents and settings\administrator.31e049c0ddeb450\桌面\cpp1.cpp(42) : error C2065: 'left' : undeclared identifier
c:\documents and settings\administrator.31e049c0ddeb450\桌面\cpp1.cpp(42) : error C2065: 'bottom' : undeclared identifier
c:\documents and settings\administrator.31e049c0ddeb450\桌面\cpp1.cpp(42) : error C2065: 'right' : undeclared identifier
执行 cl.exe 时出错.

后来我改成这样,就通过了
int main()
{
double l,b,r,t;
Rectangle Rect;
Rect.SetRectangle(6.3,2.1,3.2,5.2);
cout<<"左下角与右上角坐标分别为("<<Rect.GetLeft()<<","<<Rect.GetBottom()<<")、("<<Rect.GetRight()<<","<<Rect.GetTop()<<")的矩形";
cout<<endl;
cout<<"周长:"<<Rect.Perimeter()<<endl;
cout<<"面积:"<<Rect.Area()<<endl;
cout<<"请按顺序输入左下角与右上角的坐标"<<endl;
cin>>l>>b>>r>>t;
Rect.SetRectangle(l,b,r,t);
cout<<"左下角与右上角坐标分别为("<<Rect.GetLeft()<<","<<Rect.GetBottom()<<")、("<<Rect.GetRight()<<","<<Rect.GetTop()<<")的矩形";
cout<<endl;
cout<<"周长:"<<Rect.Perimeter()<<endl;
cout<<"面积:"<<Rect.Area()<<endl;
return 0;
}

2007-05-12 22:56
norlan
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-5-3
收藏
得分:0 
对于函数我还不是很了解,多多指教哈~。。
2007-05-12 23:07
快速回复:[求助]参数传递问题
数据加载中...
 
   



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

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