标题:输出有误!请大神指教!!!
只看楼主
IT男year
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:82
专家分:106
注 册:2013-6-9
结帖率:100%
 问题点数:0 回复次数:2 
输出有误!请大神指教!!!
#include<iostream>
using namespace std;
class car
{
private:
    char * brand,* type,* year;
    int price;
public:
    car(char * brand,char * type,char * year,int price);
    ~car();
    void pout();
};
int main()
{
    char * brand,char * type,char * year,int x;
    brand="宝马";
    type="Q5";
    year="2005年九月份";
    x=4000000;
    cout<<"                        这是书上第三章116页第3题的参考答案!"<<endl<<endl;
    system("color 3e");
    car p1(brand,type,year,x);
    p1.pout();
    return 0;
}
car::car(char * brand,char * type,char * year,int price)
{
    this->brand=new char[strlen(brand)+1];
    strcpy(this->brand,brand);
    this->type=new char[strlen(year)+1];
    strcpy(this->type,type);
    this->year=new char[strlen(year)+1];
    strcpy(this->year,year);
    this->price=price;
     
}
car::~car()
{
    delete []brand;
    delete []type;
    delete []year;
}
void car::pout()
{
    cout<<"品牌:"<<brand<<"  型号:"<<type<<"  生产年份:"<<year<<"  价格:"<<price<<endl<<endl;
}
为什么输出的价格不是4000000而是0???求大神指教呀!
搜索更多相关主题的帖子: 参考答案 private include public system 
2013-10-20 19:28
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6809
专家分:42393
注 册:2010-12-16
得分:0 
car::car(char * brand,char * type,char * year,int _price)
{
    this->brand=new char[strlen(brand)+1];
    strcpy(this->brand,brand);
    this->type=new char[strlen(type)+1];
    strcpy(this->type,type);
    this->year=new char[strlen(year)+1];
    strcpy(this->year,year);
    this->price=_price;
     
}

类成员中 price 等价于 this-》price的,修改下名字就不会有这个文字了

我行我乐
我的博客:
http://blog.yuccn. net
2013-10-20 20:35
IT男year
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:82
专家分:106
注 册:2013-6-9
得分:0 
回复 2楼 yuccn
大神你能解释的再详细点不?我按你说法改过了,好像还是不对也!
2013-10-20 20:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-422246-1-1.html




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

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