我在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;
}

附:main 已返回 0;纯粹作为占位符只是想理解整个“获取和设置”的事情。

有帮助吗?

解决方案

一般来说,您的获取/设置函数应该可以正常工作。其他一些评论:

  • year, makespeed 变量可能应该是私有的,否则实际上不需要为它们提供 get/set 函数,因为变量也可以直接更改。
  • 可能根本不应该有任何集合函数。我认为这不可能改变 year 或者 make 或设置 speed 直接地。
  • 构造函数应该采取 newYearnewMake 作为参数。
  • accelerate()break() 应该改变 speed 保存在 car 对象中,不仅仅是返回一个与 speed.
  • using namespace std; 可以将大量意外名称导入到全局命名空间,并且通常最好使用显式限定名称,例如 std::string 反而。

其他提示

的一些问题我看到:

你是指未传递到构造(newYearnewMake

这在构造变量

acceleratedecelerate功能不修改任何状态;他们只是从在传递速度加减5 - 我不认为他们应该到这样的行为。注意,问题描述说,他们添加到/从speed减去的成员变量

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

您可能想使内部私有变量,因为它们只能由类中的方法进行更新。其次,你需要的参数的构造函数,以便您可以设置一年,并开始。

例如:

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);

accelerate()brake()功能应该在所述speed构件操作的刚刚返回修改后的值来代替。这意味着分配给speed适当。

此外,具有访问器通常由privateprotected而不是被左公众。成员

吸气和setter方法用于实现数据封装,使得仅类成员只能类的访问数据成员。

几点评论:

  1. 你的成员变量应该是 私人的, , 不是 民众 因为将它们公开会破坏封装并违背使用访问器(getter/setter)函数访问它们的目的。
  2. 您的函数名称和函数参数 蒙上阴影 成员变量的名称,这会导致一些混乱。就像在Java中你需要使用 this.x 为了区分成员变量“x”和参数“x”,类似地,您需要使用 this->x. 。但是,如果您始终为成员变量提供某种前缀,则可以避免这种情况。两个常见的约定是在成员变量前添加下划线(例如命名你的成员变量 _speed 并使用 speed 作为参数的名称)或使用“m”(表示“成员”)后跟下划线。
  3. 任何不修改数据的函数——即所有的“getter”函数——都应该用关键字声明 常量, ,以便可以从 const Car 或者 const Car&. 。例如,使用 int getSpeed()const 代替 int getSpeed() 声明它为常量。
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top