注册 登录
编程论坛 C++教室

输出有误!请大神指教!!!

IT男year 发布于 2013-10-20 19:28, 335 次点击
#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???求大神指教呀!
2 回复
#2
yuccn2013-10-20 20:35
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的,修改下名字就不会有这个文字了
#3
IT男year2013-10-20 20:51
回复 2楼 yuccn
大神你能解释的再详细点不?我按你说法改过了,好像还是不对也!
1