| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 550 人关注过本帖
标题:C++类与继承问题,诚心请教,帮忙看下,谢谢!
只看楼主 加入收藏
思忆季节
Rank: 2
来 自:珠海
等 级:论坛游民
帖 子:42
专家分:62
注 册:2010-5-12
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
C++类与继承问题,诚心请教,帮忙看下,谢谢!
父类:Furniture//家具类
type:string//家具类型
mat:string//家具主材料
price:double//家具价格
+Furniture()
+Furniture(string,string,double)
+getMat():string
+getPrice():double
+getTyep():string
继承类1:Sofa//沙发类
seats:int //座位数
+Sofa()
+Sofa(string,double,int)
+getSeats():int
继承类2:Bed//床类
bedtype:string //床类型
+Bed()
+Bed(string,double,string)
+getBedType():string

Furniture、Bed和Sofa关系如上图,请先声明并定义这三个类完成以下程序:
void show(Furniture *f)
{
    …//输出f所指向的家具类型、主材料和价格
}
int main()
{
    Sofa sa(“竹子”,870,3);
    Bed ba(“木材”,1200,”单人”);
    Furniture * sb=new Sofa(“钢材”,410,1);
    show(&sa);
    show(&ba);
show(sb);
delete sb;
return 0;
}
输出结果:
家具类型:沙发   主材料:竹子    价格:870
家具类型:床     主材料:木材    价格:1200
家具类型:沙发   主材料:钢材    价格:410


我的程序如下:(诚心请教,帮忙看一下:谢谢!)
#include<iostream>
#include<string>
using namespace std;
class Furniture
{
protected:
    string mat;//家具主材料
    double price;//家具价格
    string type;//家具类型
public:
    Furniture(){}
    Furniture(string m,double p,string t="0")
    {
      mat=m;
      price=p;
      type=t;
    }
    string getType()
    {return type;}
    string getMat()
    {return mat;}
    double getPrice()
    {return price;}
};
class Sofa:public Furniture
{
private:
    int seats;
public:
    Sofa(){}
    Sofa(string m,double p,int s):Furniture(m,p,type="沙发")//{type="沙发";}
    {seats=s;}
    int getSeats()
    {return seats;}
};
class Bed:public Furniture
{
private:
    string bedtype;
public:
    Bed(){}
    Bed(string m,double p,string b):Furniture(m,p,type="床")
    {bedtype=b;}
    string getBedType()
    {return bedtype;}
    void show(Furniture *f)
{
    cout<<"家具类型:"<<f->getType()<<"  "<<"主材料:"<<f->getMat()<<"  "<<"价格:"<<f->getPrice()<<endl;
}
};
int main()
{
    Sofa sa("竹子",870,3);
    Bed ba("木材",1200,"单人");
    Furniture * sb=new Sofa("钢材",410,1);
    show(&sa);
    show(&ba);
    show(sb);
    delete sb;
    return 0;
}
搜索更多相关主题的帖子: 诚心 继承 
2010-06-01 22:13
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:5 
要帮什么?

★★★★★为人民服务★★★★★
2010-06-02 08:08
lijm1989
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:珠海
等 级:贵宾
威 望:12
帖 子:675
专家分:2844
注 册:2009-10-14
收藏
得分:15 
程序代码:
#include<iostream>
#include<string>
using namespace std;
class Furniture
{
protected:
    string mat;//家具主材料
    double price;//家具价格
    string type;//家具类型
public:
    Furniture(){}
    Furniture(string m,double p,string t="0")
    {
      mat=m;
      price=p;
      type=t;
    }
    string getType()
    {return type;}
    string getMat()
    {return mat;}
    double getPrice()
    {return price;}
};
class Sofa:public Furniture
{
private:
    int seats;
public:
    Sofa(){}
    Sofa(string m,double p,int s):Furniture(m,p,"沙发")//,type="沙发" 形参不能那么表示,在定义函数的时候才可以表示默认参数。
    {seats=s;}
    int getSeats()
    {return seats;}
};
class Bed:public Furniture
{
private:
    string bedtype;
public:
    Bed(){}
    Bed(string m,double p,string b):Furniture(m,p,"") // 同上
    {bedtype=b;}
    string getBedType()
    {return bedtype;}
};
void show(Furniture *f)  // 这个函数应该放在类外
{
    cout<<"家具类型:"<<f->getType()<<"  "<<"主材料:"<<f->getMat()<<"  "<<"价格:"<<f->getPrice()<<endl;
}
int main()
{
    Sofa sa("竹子",870,3);
    Bed ba("木材",1200,"单人");
    Furniture * sb=new Sofa("钢材",410,1);
    show(&sa);
    show(&ba);
    show(sb);
    delete sb;
    return 0;
} 
2010-06-02 21:01
思忆季节
Rank: 2
来 自:珠海
等 级:论坛游民
帖 子:42
专家分:62
注 册:2010-5-12
收藏
得分:0 
谢谢3楼的朋友了,我后来也是这样做出来了,太感谢你们了!
2010-06-03 21:22
思忆季节
Rank: 2
来 自:珠海
等 级:论坛游民
帖 子:42
专家分:62
注 册:2010-5-12
收藏
得分:0 
真晕,我后来改对了,可是运行不了,只好再手键输入一次运行就没问题了,不知道是不是VC++6.0有问题!
2010-06-03 21:33
快速回复:C++类与继承问题,诚心请教,帮忙看下,谢谢!
数据加载中...
 
   



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

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