[英]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.