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

可以帮我做道题吗? q2476547082

JQi 发布于 2020-04-01 10:54, 1888 次点击
设计一个Car类,它的数据成员要能描述一辆汽车的品牌,型号,出厂年份和价格,成员函数包括提供合适的途径来访问数据成员,在main()函数中定义类的对象并调用相应成员函数。
设计私有数据成员:
char *brand;
    char *type;
    int year;
    double price;
公有成员函数:
构造函数  :默认品牌undefinition,默认型号undefinition,默认年份2000,默认价格 0;
获取数据成员的函数  

      
   使用如下的main()函数:
int main()
{
Car car1("FIAT","Palio",2007,6.5);
cout<<car1.GetBrand (  ) <<" "<<car1.GetType (  ) <<" "<<car1.GetYear (  )
<<" " <<car1.GetPrice (  ) <<endl;
Car car2;
cout<<car2.GetBrand (  )<<" "<<car2.GetType (  )<<" "<<car2.GetYear (  )
<<" " <<car2.GetPrice (  )<<endl;
return 0;
}
7 回复
#2
雪影辰风2020-04-03 13:28
我临时写的代码,可能会有问题,目前运行结果正常:
只有本站会员才能查看附件,请 登录

这是报的警告,不知道有没有用,我是消除不了了,不好意思。

以下是代码:
程序代码:

#include<cstdio>
#include<iostream>
using namespace std;
class Car {
    public:
        Car(char *tBrand,char *tType,int tYear,double tPrice);
        ~Car();
        char *GetBrand() { return brand; }
        char *GetType() { return type; }
        int GetYear() { return year; }
        double GetPrice() { return price; }
    private:
        char *brand;
        char *type;
        int year;
        double price;
};
Car::Car(char *tBrand="undefinition",char *tType="undefinition",int tYear=2000,double tPrice=0) {
    brand=tBrand;
    type=tType;
    year=tYear;
    price=tPrice;
}
Car::~Car() {
    delete brand;
    brand=NULL;
    delete type;
    type=NULL;
    year=0;
    price=0;
}
int main() {
    Car car1("FIAT","Palio",2007,6.5);
    cout<<car1.GetBrand (  ) <<" "<<car1.GetType (  ) <<" "<<car1.GetYear (  )
        <<" " <<car1.GetPrice (  ) <<endl;
    Car car2;
    cout<<car2.GetBrand (  )<<" "<<car2.GetType (  )<<" "<<car2.GetYear (  )
        <<" " <<car2.GetPrice (  )<<endl;
    return 0;
}
#3
雪影辰风2020-04-03 13:30
一方面,我也是刚学的类,析构函数那块还是不懂,尤其是虚析构函数。我的大致理解是,写了虚析构函数的类派生下去是可以正常运行的,没写的话,子类会调用基类的虚构函数,不知道是不是这样,有错误请指出,谢谢!
#4
return_02020-04-03 14:02
回复 3楼 雪影辰风
雪影归来啦!
#5
叶纤2020-04-03 14:22
程序代码:

#include<cstdio>
#include<iostream>
using namespace std;
  class Car
  {
    public:
        Car(const char *tBrand,const char *tType,int tYear,double tPrice);
        ~Car();
    const char *GetBrand() { return brand; }
    const  char *GetType() { return type; }
        int GetYear() { return year; }
        double GetPrice() { return price; }
    private:
      const  char *brand;
     const   char *type;
        int year;
        double price;
};
Car::Car(const char *tBrand="undefinition",const char *tType="undefinition",int tYear=2000,double tPrice=0) {
    brand=tBrand;
    type=tType;
    year=tYear;
    price=tPrice;
}
Car::~Car() {
    delete brand;
    brand=NULL;
    delete type;
    type=NULL;
    year=0;
    price=0;
}
int main() {
    Car car1("FIAT","Palio",2007,6.5);
    cout<<car1.GetBrand (  ) <<" "<<car1.GetType (  ) <<" "<<car1.GetYear (  )
        <<" " <<car1.GetPrice (  ) <<endl;
    Car car2;
    cout<<car2.GetBrand (  )<<" "<<car2.GetType (  )<<" "<<car2.GetYear (  )
        <<" " <<car2.GetPrice (  )<<endl;
    return 0;
}
我没学到类只能帮你把char警告消除

#6
雪影辰风2020-04-03 20:02
回复 4楼 return_0
恭喜啊,当上版主了
#7
雪影辰风2020-04-03 20:05
回复 5楼 叶纤
谢谢,学到了
#8
叶纤2020-04-03 21:23
以下是引用雪影辰风在2020-4-3 20:02:51的发言:

恭喜啊,当上版主了

以后就有劳各位版主了,小白先在这里谢谢各位版主了
1