| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 413 人关注过本帖
标题:难题啊对于这个"类"
只看楼主 加入收藏
liuguanglei
Rank: 2
等 级:论坛游民
帖 子:41
专家分:26
注 册:2012-8-15
结帖率:80%
收藏
已结贴  问题点数:10 回复次数:3 
难题啊对于这个"类"
//大家帮我看一下怎样用string来给这个矩形输出颜色(例如:输出red)
#include<iostream.h>
#include<string>
class Rectangle
{
public:
    double GetWidth();
    double GetLength();
    double GetArea();
    double GetPerimeter();
    void SetWidth(double myWidth);
    void SetLength(double myLength);
private:
    double length;
    double width;
};
double Rectangle::GetWidth()
{
    return width;
}
double Rectangle::GetLength()
{
    return length;
}
double Rectangle::GetArea()
{   
    return width*length;
}
double Rectangle::GetPerimeter()
{
    return 2*(width+length);
}
void  Rectangle::SetWidth(double myWidth)
{
   if(myWidth>0)
       width=myWidth;
}
void  Rectangle::SetLength(double myLength)
{
    if(myLength>0)
        length=myLength;
}
void main()
{
    Rectangle t1;
    double n,m;
    cout<<"please input width and length:\n";
    cin>>n;
    cin>>m;
    t1.SetWidth(n);
    t1.SetLength(m);
    t1.GetArea();
    t1.GetPerimeter();
    cout<<"width="<<t1.GetWidth()<<"\n"<<"length="<<t1.GetLength()<<"\n";
    cout<<"Area="<<t1.GetArea()<<"\n"<<"Perimeter="<<t1.GetPerimeter()<<"\n";
}
搜索更多相关主题的帖子: double void private include public 
2012-11-07 00:36
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:3 
class Rectangle
 {
 public:
     double GetWidth();
     double GetLength();
     double GetArea();
     double GetPerimeter();
     void SetWidth(double myWidth);
     void SetLength(double myLength);
     string GetColor();
     void SetColor(string clr);
 private:
     double length;
     double width;
     string color;
 };
 void Rectangle::SetColor(string clr)
 {
     color = clr;
 }
string Rectangle::GetColor()
{
     return color;
}


cout<<"GetColor="<<t1.GetColor().c_str();

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-11-09 12:47
single010203
Rank: 1
等 级:新手上路
帖 子:8
专家分:6
注 册:2012-10-26
收藏
得分:3 
楼上最有一个cout<<"GetColor="<<t1.GetColor().c_str()中最后的c_str()啥意思啊?求解,谢谢,
2012-11-09 12:54
mmmmmmmmmmmm
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:8
帖 子:388
专家分:1809
注 册:2012-11-2
收藏
得分:3 
函数声明:const char *c_str();
  c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.

以下是引用single010203在2012-11-9 12:54:16的发言:

楼上最有一个cout<<"GetColor="<<t1.GetColor().c_str()中最后的c_str()啥意思啊?求解,谢谢,

我们的目标只有一个:消灭0回复!
while(1)
++money;
2012-11-09 14:56
快速回复:难题啊对于这个"类"
数据加载中...
 
   



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

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