There are many ways to interact with variables between different classes and different functions. As @TheUndeadFish mentioned, return values are a good place to start. If you have your heart set on using void functions one option would be to use a public variable within your Vehicles class that you set in your wheels function.
So your Vehicles class would become:
class Vehicles
{
public:
int wh;
void wheels() {
cout << "Enter number of wheels: ";
cin >> wh;
if (wh == 2) {
cout << "You chose a Motorcycle!
";
}
else if (wh == 3) {
cout << "You chose a Tricycle!
";
}
else if (wh == 4) {
cout << "You chose a Car!
";
}
}
}; //number of wheels chosen
And then your main could be:
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;
}
EDIT: notice that if you comment out the call to number.wheels() you will get an uninitialized variable error. In practice you'd want it initialized (possibly to -1) in the Vehicles class constructor to avoid the possibility of this error.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…