【问题标题】:Class CAR change Price with %Class CAR 价格变化百分比
【发布时间】:2023-02-20 18:11:39
【问题描述】:

我有一个任务,我们应该创建一个私有的 CAR 类,然后添加 getter 和 setter 那部分我想我以一种体面的方式解决了..

下一个选项是假设用户选择一辆车然后设置自己的折扣价

所以我希望用户以奥迪为例,然后是 10%,汽车价格应该以新价格更新,但我真的不知道该怎么做......

 Bil Volvo;
 Volvo.setBrand("Volvo");
 Volvo.setColor("Red");
 Volvo.setYearModel(2005);
 Volvo.setPrice (3265);

 Bil Audi;
 Audi.setBrand("Audi");
 Audi.setColor("Green");
 Audi.setYearModel(2012);
 Audi.setPrice (12500);


 cout << "Brand: " << Volvo.getBrand() << endl;
 cout << "Color: " << Volvo.getColor() << endl;
 cout << "Year model: " << Volvo.getYearModel() << endl;
 cout << "Price: " << Volvo.getPrice () << endl;

  cout << "Brand: " << Audi.getBrand() << endl;
 cout << "Color: " << Audi.getColor() << endl;
 cout << "Year model: " << Audi.getYearModel() << endl;
 cout << "Price: " << Audi.getPrice () << endl;


 string buyCar;

 cout << "What car would you like to buy?" << endl;
 getline(cin, buyCar);`

【问题讨论】:

    标签: class get set


    【解决方案1】:

    顺便说一下,这就是班级的建设方式

    class Bil {
    
    
       private:
            string Brand;
            string Color;
            int YearModel;
            int Price;
    
       public:
             void setBrand (string aBrand) {Brand = aBrand;}
            string getBrand () {return Brand;}
             void setColor (string aColor) {Color = aColor;}
            string getColor () {return Color;}
             void setYearModel (int aYearModel) {YearModel = aYearModel;}
            int getYearModel () {return YearModel;}
             void setPrice (double aPrice) {Price = aPrice;}
            double getPrice () {return Price;}
    

    【讨论】:

      猜你喜欢
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 2021-09-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多