C++ 如何在if语句中使用特定的Void函数 #包括 使用名称空间std; 等级车辆 { 公众: 空心车轮(){ cout>wh; 如果(wh==2){ cout
在不同的类和不同的函数之间有很多方法与变量交互。正如@TheUndeadFish所提到的,返回值是一个很好的起点。如果你一心想使用空函数,一个选择就是在你的Vehicles类中使用一个公共变量,你在wheels函数中设置了该变量 因此,您的车辆等级将变为:C++ 如何在if语句中使用特定的Void函数 #包括 使用名称空间std; 等级车辆 { 公众: 空心车轮(){ cout>wh; 如果(wh==2){ cout,c++,C++,在不同的类和不同的函数之间有很多方法与变量交互。正如@TheUndeadFish所提到的,返回值是一个很好的起点。如果你一心想使用空函数,一个选择就是在你的Vehicles类中使用一个公共变量,你在wheels函数中设置了该变量 因此,您的车辆等级将变为: #include <iostream> using namespace std; class Vehicles { public: void wheels () { cout << "Enter num
#include <iostream>
using namespace std;
class Vehicles
{
public:
void wheels () {
cout << "Enter number of wheels: ";
int wh;
cin >> wh;
if (wh == 2) {
cout << "You chose a Motorcycle!\n";
} else if (wh == 3) {
cout << "You chose a Tricycle!\n";
} else if (wh == 4) {
cout << "You chose a Car!\n";
}
}
} ; //number of wheels chosen
class brandMotorcycle : public Vehicles
{
public:
void brandM () {
cout << "Enter brand: \n 1: Yamaha \n 2: Ducati \n 3: Honda \n 4: Kawasaki \n 5: Suzuki \n";
int br;
cin >> br;
if ( br == 1) {
cout << "You chose Yamaha! \n";
} else if (br == 2) {
cout << "You chose Ducati! \n";
} else if (br == 3) {
cout << "You chose Honda! \n";
} else if (br == 4) {
cout << "You chose Kawasaki! \n";
} else if (br == 5) {
cout << "You chose Suzuki! \n";
}
}
} ; //brand of motorcycle in case the user will chose 2 wheels
class brandTricycle : public Vehicles
{
public:
void brandT () {
cout << "Enter brand: \n 1: Classic \n 2: CATs \n 3: KYTO \n 4: Prime Green \n 5: Sun Etrike \n";
int br;
cin >> br;
if ( br == 1) {
cout << "You chose Classic! \n";
} else if (br == 2) {
cout << "You chose CATs! \n";
} else if (br == 3) {
cout << "You chose KYTO! \n";
} else if (br == 4) {
cout << "You chose Prime Green! \n";
} else if (br == 5) {
cout << "You chose Sun Etrike! \n";
}
}
} ; //brand of tricycle in case the user chose 3 wheels
class brandCar : public Vehicles
{
public:
void brandC () {
cout << "Enter brand: \n 1: Mazda \n 2: Honda \n 3: Toyota \n 4: Kia \n 5: Volkswagen \n";
int br;
cin >> br;
if ( br == 1) {
cout << "You chose Mazda! \n";
} else if (br == 2) {
cout << "You chose Honda! \n";
} else if (br == 3) {
cout << "You chose Toyota! \n";
} else if (br == 4) {
cout << "You chose Kia! \n";
} else if (br == 5) {
cout << "You chose Volkswagen! \n";
}
}
} ; //brand of cars in case the user chose 4 wheels
int main () {
Vehicles number;
brandMotorcycle brandM;
brandTricycle brandT;
brandCar brandC;
number.wheels();
if (wheels == 2) {
brandM.brandM();
} else if (wheels == 3) {
brandT.brandT();
} else if (wheels == 4) {
brandC.brandC();
} // this part is my problem
return 0;
}
编辑:请注意,如果注释掉对number.wheels()的调用,您将得到一个未初始化的变量错误。实际上,您希望它初始化(可能是-1)在Vehicles类构造函数中,以避免出现此错误。您正在
wheels
中收集数据,以便在返回时使用这些数据。因此,是时候阅读函数的返回值了。非常感谢,联机类非常困难。
class Vehicles
{
public:
int wh;
void wheels() {
cout << "Enter number of wheels: ";
cin >> wh;
if (wh == 2) {
cout << "You chose a Motorcycle!\n";
}
else if (wh == 3) {
cout << "You chose a Tricycle!\n";
}
else if (wh == 4) {
cout << "You chose a Car!\n";
}
}
}; //number of wheels chosen
int main() {
Vehicles number;
brandMotorcycle brandM;
brandTricycle brandT;
brandCar brandC;
number.wheels();
int wheels = number.wh;
if (wheels == 2) {
brandM.brandM();
}
else if (wheels == 3) {
brandT.brandT();
}
else if (wheels == 4) {
brandC.brandC();
} // this part is my problem
return 0;
}