Reputation: 47
#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;
}
I made an Inheritance where if the user chose 2 wheels he will get a motorcycle and he gets to chose what brand it is. And if he chose 3 wheels he gets tricycle and chose the brand and same with the 4 wheels. But I am currently struggling to input the final if statement because I don't know which code I should use.
Upvotes: 1
Views: 82
Reputation: 26
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!\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
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.
Upvotes: 1