簡體   English   中英

如何在C ++中使用集合和獲取?

[英]How do you use sets and gets in C++?

我在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:主要有返回0; 純粹是作為占位符,只是試圖了解整個“獲取並設置”的內容。

通常,您的獲取/設置功能應該可以正常工作。 其他一些評論:

  • yearmakespeed變量可能應該是私有的,否則實際上不需要任何獲取/設置函數,因為也可以直接更改這些變量。
  • 可能根本不應該有任何設置函數。 我不認為這是為了能夠改變的yearmake或設定的speed直接。
  • 構造函數應將newYearnewMake作為參數。
  • accelerate()break()應該更改保存在汽車對象中的speed ,而不僅僅是返回不同於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而不是公開。

使用getter和setter方法實現數據封裝,以便只有類成員才能訪問該類的數據成員。

幾點評論:

  1. 您的成員變量應該是private ,而不是public,因為將它們設為public會破壞封裝並破壞使用訪問器(getter / setter)函數訪問它們的目的。
  2. 您的函數名稱和函數參數使成員變量的名稱黯然失色 ,這導致了一些混亂。 就像在Java中需要使用this.x來區分成員變量“ x”和參數“ x”一樣,類似地,您將需要使用this->x 但是,如果始終為成員變量提供某種前綴,則可以避免這種情況。 兩種常見的約定是在成員變量前加上下划線(例如,將成員變量_speed_speed並使用speed作為參數的名稱),或在后跟下划線的情況下使用“ m”(對於“ member”)。
  3. 任何不修改數據的函數(即所有“ getter”函數)都應使用關鍵字const聲明,以便可以從const Carconst Car&訪問該數據。 例如,使用int getSpeed()const而不是int getSpeed()聲明其常量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM