如何通过我的 if 循环使我的 setfunction 运行 ?

How do I make my setfunction run through my if loop?

我正在做一个程序,我需要做一个关于汽车的 class。我需要添加仅允许 1930 -2030 的汽车制造年份作为输入的 setFunctions,其他任何内容都将自动设置为 2012。我的代码如下所示:

// code will be following
void setYear(int yr) 
{
    if (yr > 2030 || yr < 1930){
        yr = 2012;
    }
    else {
        year = yr;
    }
}
// end of segment.

我也有 setSpeed 的代码,它不能小于 0,如下所示

// code following
void setSpeed(int spd) 
{
    if (spd < 0){
        spd = 0;
    }
    else {
        speed = spd;
    }
}

问题是,当我尝试更改对象的变量时,set 函数无法按预期工作。我相信问题出在我的构造函数上,

// constructor with default parameters
car(int yr = 2012, string mk = "unknown", int spd = 0)
{
    year = yr;
    make  = mk;
    speed = 0;
}

我的代码基本上是按要求做的,只是和要求的不完全一样。

这是我的代码应该做的:

Create a program that illustrates C++ classes. Your program should do the following:

Write a class named Car that has the following member variables:

  • year (An int that holds the car’s model year. The year must be between 1900-2030 (inclusive)).

  • make (A string that holds the make of the car.)

  • speed (An int that holds the car’s current speed. The speed must be 0 or greater. ) In addition, the class should have the following member functions:

  • Constructor - The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables. The constructor should initialize the speed member variable to 0. Notice that this constructor will have 2 parameters: one for the year and one for the make of the Car.

When you define a Car object, you will need to send the two arguments the constructor is expecting:

Car myCar(2002, "Jetta");

In the constructor you need to assign a value to each of the member variables:

  • Since year has specific values that are allowed, call the setYear function with the value sent.
  • Set make to the value passed.
  • Set speed to zero. You can do this with a simple assignment statement or you can call the setSpeed function.

Get functions:
Appropriate access functions should be created to allow values to be retrieved from an object’s year, make, and speed member variables. There will be three get functions, one for each member variable. Each of these functions return one thing and have NO parameters.

Set functions:
There will be three set functions, one for each member variable. Each of these functions receive one thing and return nothing.

  • The speed cannot be less than zero. Have your set function protect the speed so it is never negative. If a negative value is sent, simply set the speed to zero.
  • The year must be between 1900 and 2030 (inclusive). If a year outside the range is sent, set the year to 2012. No validation of the make is required.

Accelerate:
The accelerate function should add 5 to the speed member variable each time it is called. This member function returns nothing and has no parameter. It simply does its job: each time it is called, it adds 5 to speed.

Brake:
The brake function should subtract 5 from the speed member variable each time it is called. If the speed is already zero, no change is to be made to speed. This member function returns nothing and has no parameter. It simply does its job: each time it is called, it subtracts 5 from speed unless speed is already at zero. Then it makes no change.

Notes:
Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it.

Then, call the brake function seven times. After each call to the brake function, get the current speed of the car and display it. Each time you display the speed include the year and make of the vehicle in the output label.

Now, set the year, make, and speed to 2009, Jeep, and 75 respectively. Once again call the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function seven times. After each call to the brake function, get the current speed of the car and display it. Each time you display the speed include the year and make of the vehicle in the output label.

Finally, set the year, make, and speed to 1827, Jetta, and -30 respectively. Using the get functions, print out the year, make, and speed.

这是我的代码:

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

      // car class declaration
      class car
      {
      private:
int year;
string make;
int speed;

public:
//creation of set functions 

void setYear(int yr) 
{
if (yr > 2030 || yr < 1930){
    yr = 2012;
}
else {

    year = yr;
}
}

void setMake(string mk) {
    make = mk;
}

void setSpeed(int spd) 
{
if (spd < 0){
    spd = 0;
}
else {
    speed = spd;
}
}
// constructor with default parameters
 car(int yr = 2012, string mk = "unknown", int spd = 0)
 {
     year = yr;
         make  = mk;
         speed = 0;
 }

 // Accessors ( The get functions)

 int getYear()
 { return year; }

 string getMake()
 { return make; }

 int getSpeed()
 { return speed; }

 // Mutators
 void accelerate ()
 {  speed += 5;  }

 void brake()
 {

    if ( speed >= 5)
        speed -=5;
    else
        speed = 0;
}

};

int main()
{
// creation and display of first object of car class
car hotRod (2020, "Ford");
hotRod.setSpeed(40);
 
 cout << "I've spotted a " << hotRod.getYear()<<" "
      << hotRod.getMake() << " car, going 40 kmph!\n\n";
      
// accelerator function to run 5 times
cout << "It is accelerating \n\n";
for (int run =0; run < 5; run++)
{

 hotRod.accelerate();
 cout << hotRod.getYear()<<" " << hotRod.getMake()
 << " current speed: " << hotRod.getSpeed() << "kmph.\n";
}
 cout << endl;
 
 // breaking function to run 7 times
 cout << "Now it's 'breaking... " << endl;
 for(int brk = 0; brk < 7; brk++)
 {
     hotRod.brake();
     cout << hotRod.getYear()<<" "<< hotRod.getMake()
     << "  Current speed: " << hotRod.getSpeed() << "kmph.\n";
}
// intermediary input to break the program apart
// this allows for better display of program
string x;
cout << endl << endl << endl << "Would you like to know what car I saw next? ";
cin >> x;

// creation and display of second object in car class        
car hotRod2 (2009, "Jeep");
hotRod2.setSpeed(75);

     cout << endl << endl <<endl
     << "I've spotted a " << hotRod2.getYear()<<" "
      << hotRod2.getMake() << " car, going 75kmph!\n\n";
// accelerator function to run 5 times
cout << "It is accelerating \n\n";
for (int run =0; run < 5; run++)
{

 hotRod2.accelerate();
 cout << hotRod2.getYear()<<" " << hotRod2.getMake()
 << " current speed: " << hotRod2.getSpeed() << "kmph.\n";
}
 cout << endl;
 
 // Breaking function to run 7 times
 cout << "Now it's 'breaking... " << endl;
 for(int brk = 0; brk < 7; brk++)
 {
     hotRod2.brake();
     cout << hotRod2.getYear()<<" "<< hotRod2.getMake()
     << "  Current speed: " << hotRod2.getSpeed() << "kmph.\n";
     
     
 }
     string y;
cout << endl << endl << endl << "Would you like to know what car I saw next? ";
cin >> y;

// creation of 3rd object of car class   
car hotRod3;
hotRod3.setMake("Jetta");
hotRod3.setYear(1827);
hotRod3.setSpeed(-30);

     cout << endl << endl <<endl
     << "I've spotted a "
     <<hotRod3.getYear()
     <<" "
     << hotRod3.getMake() << " car, going "
     << hotRod3.getSpeed()
     << "kmph! \n\n";

 // end of program
 return 0;
 }

setYear()setSpeed() 都犯了同样的错误。如果输入值超出范围,它们不会为 class 数据成员分配任何值。

试试这个:

void setYear(int yr) 
{
    if (yr > 2030 || yr < 1930){
        yr = 2012;
    }
    year = yr;
}

void setSpeed(int spd) 
{
    if (spd < 0){
        spd = 0;
    }
    speed = spd;
}

此外,您的构造函数应该调用 set... 函数,否则创建者可能会传入您正在接受的不可接受的值 as-is:

car(int yr = 2012, string mk = "unknown", int spd = 0)
{
    setYear(yr);
    setMake(mk);
    setSpeed(spd);
}

事实上,说明书特别告诉你要这样做:

In the constructor you need to assign a value to each of the member variables:

  • Since year has specific values that are allowed, call the setYear function with the value sent.
  • Set make to the value passed.
  • Set speed to zero. You can do this with a simple assignment statement or you can call the setSpeed function.