2

我在 java 中使用过它们,似乎没有太多问题,但我在 C++ 中并没有很好地掌握它们。任务是:

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

    year. An int that holds the car's model year.
    make. A string that holds the make of the car.
    speed. An int that holds the car's current speed.

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.

    Accessors.  Appropriate accessor functions should be created to allow 
        values to be retrieved from an object's year, make, and speed 
        member variables.

    accelerate.  The accelerate function should add 5 to the speed member 
        variable each time it is called.

    brake.  The brake function should subtract 5 from the speed member 
        variable each time it is called.

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 five times.  After each call to 
    the brake function, get the current speed of the car and display it.

到目前为止,这就是我所拥有的,但我有理由确定我完全错了。如果有人有任何建议,我将不胜感激,谢谢!

#include<iostream>
#include<string>

using namespace std;

class Car
{
    public:
        int year;
        string make;
        int speed;
    Car()
    {
        setYear(newYear);
        setMake(newMake);
        setSpeed(0);
    }

    void setYear(int newYear)
    {
        year = newYear;
    }
    void setMake(string newMake)
    {
        make = newMake;
    }
    int getYear()
    {
        return year;
    }
    string getMake()
    {
        return make;
    }
    int accelerate(int speed)
    {
        return (speed+5);
    }
    int brake(int speed)
    {
        return (speed-5);
    }
};

int main()
{
    return 0;
}

PS:main有return 0;纯粹作为一个占位符,只是试图理解整个“获取和设置”的事情。

4

6 回答 6

4

通常,您的 get/set 函数应该可以正常工作。其他一些评论:

  • ,和变量应该是私有的year,否则实际上不需要为它们设置 get/set 函数,因为变量也可以直接更改。makespeed
  • 可能根本不应该有任何设置功能。我不认为它可以直接更改yearormake或设置speed
  • 构造函数应采用newYearandnewMake作为参数。
  • accelerate()并且break()应该更改speed保存在汽车对象中,而不仅仅是返回一个不同于speed.
  • using namespace std;可以将大量意外名称导入全局命名空间,通常最好使用明确限定的名称,例如std::string
于 2010-02-09T04:17:13.950 回答
1

我看到的一些问题:

您指的是构造函数中未传递给构造函数(newYear, newMake)的变量

accelerateanddecelerate函数不修改任何状态;他们只是从传入的速度中加减 5 - 我认为他们不应该这样做。请注意,问题描述说他们添加到/从speed 成员变量中减去。

您的所有成员变量都是公开的。你会在 Java 中这样做吗?

于 2010-02-09T04:11:26.690 回答
1

您可能希望将内部变量设为私有,因为它们只能由类内的方法更新。其次,您需要构造函数的参数,以便您可以设置年份和初始值。

前任:

public: Car(int newYear, string newMake) {...}

class Car
{
private:
        int year;
        string make;
        int speed;
public:
    Car(int newYear, string newMake)
    {
        setYear(newYear);
        setMake(newMake);
        setSpeed(0);
    }
    ...
}

您也没有更新您的acceleratebrake方法的速度值。尝试:

return (speed -=5);

或者

return (speed += 5);
于 2010-02-09T04:11:54.663 回答
0

accelerate()andbrake()函数应该对成员进行操作,而speed不是仅仅返回修改后的值。这意味着分配给speed适当的。

此外,具有访问器的成员通常是公开的privateprotected而不是公开的。

于 2010-02-09T04:11:42.073 回答
0

使用getter和setter方法实现数据封装,使得只有类成员才能访问类的数据成员。

于 2010-02-09T04:12:26.567 回答
0

几条评论:

  1. 您的成员变量应该是private,而不是public,因为使它们成为 public 会破坏封装并破坏使用访问器(getter/setter)函数访问它们的目的。
  2. 您的函数名称和函数参数掩盖了成员变量的名称,这导致了一些混乱。就像在 Java 中需要使用this.x来区分成员变量“x”和参数“x”一样,同样需要使用this->x. 但是,如果您总是给成员变量某种前缀,则可以避免这种情况。两种常见的约定是使用下划线作为成员变量的前缀(例如,命名您的成员变量_speedspeed用作参数的名称)或使用'm'(代表“成员”)后跟下划线。
  3. 任何不修改数据的函数——即你所有的“getter”函数——都应该用关键字const声明,以便可以从 aconst Car或访问这些数据const Car&。例如,使用int getSpeed()const而不是将int getSpeed()其声明为常量。
于 2010-02-09T04:51:15.630 回答