| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1740 人关注过本帖
标题:向上转型机制中基类的getType成员没有输出结果
只看楼主 加入收藏
鸿蒙之灵
Rank: 4
来 自:异次元裂缝
等 级:贵宾
威 望:11
帖 子:126
专家分:244
注 册:2016-8-22
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:2 
向上转型机制中基类的getType成员没有输出结果
程序代码:
//Furniture.h

#include <iostream>
#include <string>
using namespace std;

class Furniture           //家具类
{
private:
    string type;      //家具类型
    string mat;     //家具主材料
    double price;     //家具价格
public:
    Furniture(string type,string mat,double price):mat(mat),price(price){}
    string getMaterial()
    {
        return mat;
    }
    double getprice()
    {
        return price;
    }
    string getType()
    {
        return type;
    }
};


class Sofa:public Furniture    //沙发类
{
private:
    int seats;
public:
    Sofa(string mat,double price,int seats):Furniture("沙发",mat,price),seats(seats){}
    int getseats()
    {
        return seats;
    }
};


class Bed:public Furniture   //床 类
{
private:
    string bedtype;
public:
    Bed(string mat,double price,string bedtype):Furniture("",mat,price),bedtype(bedtype){}
    string getBedType()
    {
        return bedtype;
    }
};
void show(Furniture *f)
{
    cout<<"家具类型:"<<f->getType()<<"   主材料:"<<f->getMaterial()<<"   价格:"<<f->getprice()<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
    cout<<"向上转型机制示例:"<<endl;
    Sofa sofa1("木材",870.00,3);
    Bed bed1("木材",1280.00,"双人");
    Furniture *sofa2=new Sofa("钢材",410.00,1);      //用派生类指针初始化基类指针
    Furniture &bed2=*new Bed("竹子",1280.00,"单人");     //用派生类对象初始化基类的引用
    Furniture bed3=bed1;       //派生类对象赋值给基类对象
    show(&sofa1);
    show(sofa2);
    show(&bed3);
    show(&bed2);
    delete sofa2;    //释放动态空间
    delete &bed2;    //释放动态空间
    return 0;
}



    问题描述,本来输出的结果应该是:
    家具类型:沙发  主材料:木材   价格:870
    家具类型:沙发  主材料:钢材   价格:410
    家具类型:床    主材料:木材   价格:1280
    家具类型:床    主材料:竹子   价格:1280

    可是我编译后的输出结果却是:
    家具类型:      主材料:木材    价格:870
    (下同)

    家具类型的这一部分没有输出,请问问题出在什么地方?






[此贴子已经被作者于2016-9-2 15:20编辑过]

2016-09-02 15:19
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
Furniture(string type,string mat,double price):mat(mat),price(price){}
中 type 没用到吧
我猜你想写的是
Furniture(string type,string mat,double price):type(type),mat(mat),price(price){}
2016-09-02 15:33
鸿蒙之灵
Rank: 4
来 自:异次元裂缝
等 级:贵宾
威 望:11
帖 子:126
专家分:244
注 册:2016-8-22
收藏
得分:0 
回复 2楼 rjsp

    不错,正是这个问题,我排查的时候忽略了参数列表,感觉实在找不到问题了,谢谢啊

对待编程,要像对待情人一样
2016-09-02 15:39
快速回复:向上转型机制中基类的getType成员没有输出结果
数据加载中...
 
   



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

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