我正在學習如何在 C 中使用類。現在我正在開發一個小程式,它應該根據給定的英里數和加侖數顯示車輛的每加侖英里數。賦值表示在主函式中呼叫成員函式,以便在 Auto 類中設定成員變數。這是我的代碼:
#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;
}
它應該顯示類似“豐田凱美瑞開 100 英里,使用 10 加侖汽油,每加侖 10 英里”這樣的內容。相反,我的輸出顯示“A 行駛了 -1538932792 英里,使用了 4.66265e-310 這輛車得到了 -infmpg。” 我在做什么導致輸出變成這樣?我剛開始使用類,所以我對它們沒有太多經驗。感謝您的建議。
uj5u.com熱心網友回復:
您遇到的錯誤是因為您正在創建四個不同的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;
}
請注意,現在只有一個名為“car”的“Auto”物件。
uj5u.com熱心網友回復:
您只需要創建一個類實體,Auto因為您只想顯示一輛車的資訊(名稱為“ Toyota Camry ”)。
#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;
}
uj5u.com熱心網友回復:
首先,在任何可能的地方使用 const T& ,你會在 99% 的情況下是正確的。此外,嘗試在設計級別處理諸如英里數之類的邊緣情況,僅在此處使用無符號型別將在未來節省時間。像 static_cast 這樣的東西在這里是不必要的,但明確告訴其他用戶這里有一些“奇怪”的東西。
#include <iostream>
class Auto {
public:
Auto
Auto() {}
Auto(const std::string& model, size_t miles, double gallons)
: m_model(model)
, m_milesDriven(miles)
{
if (gallons > 0.0)
m_gallonsOfGas = gallons;
}
double calculateMilesPerGallon() {
if (m_gallonsOfGas > 0.0)
return static_cast<double>(m_milesDriven) / m_gallonsOfGas;
retun 0.0; // or throw an exception insted
}
void setModel(const std::string& newModel){
m_model = newModel;
}
void setMilesDriven(size_t newMiles){
m_milesDriven = newMiles;
}
void setGallonsOfGas(double newGallons){
if (newGallons > 0.0)
m_gallonsOfGas = newGallons;
else {
// throw an exception?
}
}
void output(){
std::cout << "A " << model << " was driven " << m_milesDriven;
std::cout << " miles, and used " << m_gallonsOfGas << ".\n";
std::cout << "This car gets " << calculateMilesPerGallon() << "mpg.\n";
}
protected:
std::string m_model;
size_t m_milesDriven = 0;
double m_gallonsOfGas = 0.0;
};
int main()
{
Auto car;
std::string carModel = "Toyota Camry";
size_t carMiles = 100;
double carGallons = 10.0;
car.setModel(carModel);
car.setMilesDriven(carMiles);
car.setGallonsOfGas(carGallons);
car.output();
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/353662.html
