为什么我的成员 functions/class 变量输出错误的数字?
Why are my member functions/class variables outputting the wrong numbers?
我正在学习如何在 C++ 中使用 classes。现在我正在开发一个小程序,它应该根据给定的英里数和加仑数显示车辆每加仑的英里数。赋值表示在 main 函数中调用成员函数,以便在 Auto class 中设置成员变量。这是我的代码:
#include <iostream>
using namespace std;
class Auto {
public:
string model;
int milesDriven;
double gallonsOfGas;
double calculateMilesPerGallon(int milesDriven, double gallonsOfGas) {
return milesDriven / gallonsOfGas;
}
void setModel(string newModel){
model = newModel;
}
void setMilesDriven(int newMiles){
milesDriven = newMiles;
}
void setGallonsOfGas(double newGallons){
gallonsOfGas = newGallons;
}
void output(){
cout << "A " << model << " was driven " << milesDriven << " miles, and used " << gallonsOfGas << endl;
cout << "This car gets " << calculateMilesPerGallon(milesDriven, gallonsOfGas) << "mpg.";
}
};
int main()
{
Auto modelFunction;
Auto milesFunction;
Auto gasFunction;
Auto outputFunction;
string carModel = "Toyota Camry";
int carMiles = 100;
double carGallons = 10;
modelFunction.setModel(carModel);
milesFunction.setMilesDriven(carMiles);
gasFunction.setGallonsOfGas(carGallons);
outputFunction.output();
return 0;
}
它应该显示类似“一辆 Toyota Camry 以 100 英里的速度行驶,使用 10 加仑的汽油,得到 10 英里/加仑。”相反,我的输出显示“A 行驶了 -1538932792 英里,并使用了 4.66265e-310
这辆车得到 -infmpg。”我在做什么导致输出像这样?我刚开始使用 classes,所以我对它们没有太多经验。谢谢你的建议。
您遇到的错误是因为您正在创建四个不同的 Auto
对象,每个对象都有自己的成员变量。如果您将主要功能更改为以下内容,它将起作用:
int main()
{
Auto car;
string carModel = "Toyota Camry";
int carMiles = 100;
double carGallons = 10;
car.setModel(carModel);
car.setMilesDriven(carMiles);
car.setGallonsOfGas(carGallons);
car.output();
return 0;
}
请注意,现在只有一个 'Auto' 对象名为 'car'。
您只需创建 一个 class Auto
的实例,因为您只想显示一辆车的信息(名称为“丰田凯美瑞").
#include <iostream>
using namespace std; // strongly encouraging you to avoid using this
// in file scope
class Auto {
public:
string model;
int milesDriven;
double gallonsOfGas;
double calculateMilesPerGallon(/*no params required*/) {
return milesDriven / gallonsOfGas;
}
void setModel(string newModel){
model = newModel;
}
void setMilesDriven(int newMiles){
milesDriven = newMiles;
}
void setGallonsOfGas(double newGallons){
gallonsOfGas = newGallons;
}
void output(){
cout << "A " << model << " was driven " << milesDriven << " miles, and used " << gallonsOfGas << endl;
cout << "This car gets " << calculateMilesPerGallon() << "mpg.";
}
};
int main()
{
string carModel = "Toyota Camry";
int carMiles = 100;
double carGallons = 10;
Auto car;
car.setModel(carModel);
car.setMilesDriven(carMiles);
car.setGallonsOfGas(carGallons);
car.output();
return 0;
}
我正在学习如何在 C++ 中使用 classes。现在我正在开发一个小程序,它应该根据给定的英里数和加仑数显示车辆每加仑的英里数。赋值表示在 main 函数中调用成员函数,以便在 Auto class 中设置成员变量。这是我的代码:
#include <iostream>
using namespace std;
class Auto {
public:
string model;
int milesDriven;
double gallonsOfGas;
double calculateMilesPerGallon(int milesDriven, double gallonsOfGas) {
return milesDriven / gallonsOfGas;
}
void setModel(string newModel){
model = newModel;
}
void setMilesDriven(int newMiles){
milesDriven = newMiles;
}
void setGallonsOfGas(double newGallons){
gallonsOfGas = newGallons;
}
void output(){
cout << "A " << model << " was driven " << milesDriven << " miles, and used " << gallonsOfGas << endl;
cout << "This car gets " << calculateMilesPerGallon(milesDriven, gallonsOfGas) << "mpg.";
}
};
int main()
{
Auto modelFunction;
Auto milesFunction;
Auto gasFunction;
Auto outputFunction;
string carModel = "Toyota Camry";
int carMiles = 100;
double carGallons = 10;
modelFunction.setModel(carModel);
milesFunction.setMilesDriven(carMiles);
gasFunction.setGallonsOfGas(carGallons);
outputFunction.output();
return 0;
}
它应该显示类似“一辆 Toyota Camry 以 100 英里的速度行驶,使用 10 加仑的汽油,得到 10 英里/加仑。”相反,我的输出显示“A 行驶了 -1538932792 英里,并使用了 4.66265e-310 这辆车得到 -infmpg。”我在做什么导致输出像这样?我刚开始使用 classes,所以我对它们没有太多经验。谢谢你的建议。
您遇到的错误是因为您正在创建四个不同的 Auto
对象,每个对象都有自己的成员变量。如果您将主要功能更改为以下内容,它将起作用:
int main()
{
Auto car;
string carModel = "Toyota Camry";
int carMiles = 100;
double carGallons = 10;
car.setModel(carModel);
car.setMilesDriven(carMiles);
car.setGallonsOfGas(carGallons);
car.output();
return 0;
}
请注意,现在只有一个 'Auto' 对象名为 'car'。
您只需创建 一个 class Auto
的实例,因为您只想显示一辆车的信息(名称为“丰田凯美瑞").
#include <iostream>
using namespace std; // strongly encouraging you to avoid using this
// in file scope
class Auto {
public:
string model;
int milesDriven;
double gallonsOfGas;
double calculateMilesPerGallon(/*no params required*/) {
return milesDriven / gallonsOfGas;
}
void setModel(string newModel){
model = newModel;
}
void setMilesDriven(int newMiles){
milesDriven = newMiles;
}
void setGallonsOfGas(double newGallons){
gallonsOfGas = newGallons;
}
void output(){
cout << "A " << model << " was driven " << milesDriven << " miles, and used " << gallonsOfGas << endl;
cout << "This car gets " << calculateMilesPerGallon() << "mpg.";
}
};
int main()
{
string carModel = "Toyota Camry";
int carMiles = 100;
double carGallons = 10;
Auto car;
car.setModel(carModel);
car.setMilesDriven(carMiles);
car.setGallonsOfGas(carGallons);
car.output();
return 0;
}