[英]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; 純粹是作為占位符,只是試圖了解整個“獲取並設置”的內容。
通常,您的獲取/設置功能應該可以正常工作。 其他一些評論:
year
, make
和speed
變量可能應該是私有的,否則實際上不需要任何獲取/設置函數,因為也可以直接更改這些變量。 year
或make
或設定的speed
直接。 newYear
和newMake
作為參數。 accelerate()
和break()
應該更改保存在汽車對象中的speed
,而不僅僅是返回不同於speed
的值。 using namespace std;
可以將許多意外的名稱導入到全局名稱空間,通常更可取的是使用顯式限定名稱,例如std::string
。 我看到一些問題:
您正在引用構造函數中未傳遞給構造函數的變量( newYear
, newMake
)
accelerate
和decelerate
功能不修改任何狀態。 他們只是從傳入的速度中加減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);
}
...
}
您也不會在accelerate
和brake
方法上更新速度值。 嘗試:
return (speed -=5);
要么
return (speed += 5);
accelerate()
和brake()
函數應在speed
成員上運行,而不是僅返回修改后的值。 這意味着要適當分配speed
。
同樣,具有訪問器的成員通常被設為private
或protected
而不是公開。
使用getter和setter方法實現數據封裝,以便只有類成員才能訪問該類的數據成員。
幾點評論:
this.x
來區分成員變量“ x”和參數“ x”一樣,類似地,您將需要使用this->x
。 但是,如果始終為成員變量提供某種前綴,則可以避免這種情況。 兩種常見的約定是在成員變量前加上下划線(例如,將成員變量_speed
為_speed
並使用speed
作為參數的名稱),或在后跟下划線的情況下使用“ m”(對於“ member”)。 const Car
或const Car&
訪問該數據。 例如,使用int getSpeed()const
而不是int getSpeed()
聲明其常量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.