my first oop based program
i'd like to know how to make the same pointer points to differents types of vars,
if you read the code, you'll see the comment I NEED A SOLUTION FOR THIS, and thats where im stuck...
Code:
#include <iostream>
using namespace std;
class person
{
public:
person();
string name;
int age;
bool sex; // true means male
string country;
string sexString()
{
string returnString;
returnString = (this->sex == true) ? "guy" : "woman" ;
return returnString;
}
};
person::person()
{
this->name = "Unnamed Person";
this->age = 50;
this->sex = true;
this->country = "United States";
}
void introduce(person person)
{
cout<<"Hi, my name is "<<person.name<<". I'm a "<<person.age<<" years old "<<person.sexString()<<", and I live in "<<person.country<<"."<<endl;
}
void callMenu(person& person)
{
int option;
string optionIdentifier;
string input;
string * pointer;
cout<<"\n WHAT WOULD LIKE TO CHANGE?\n";
cout<<"1. NAME\n";
cout<<"2. AGE\n";
cout<<"3. SEX\n";
cout<<"4. COUNTRY\n";
cout<<"5. NOTHING\n";
cout<<"\nCOMMAND: ";
cin>>option;
switch(option)
{
case 1:
optionIdentifier = "NAME";
pointer = &person.name;
break;
case 2:
optionIdentifier = "AGE";
//pointer = &person.age; I NEED A SOLUTION FOR THIS
break;
case 3:
optionIdentifier = "SEX";
//pointer = &person.sex; I NEED A SOLUTION FOR THIS
break;
case 4:
optionIdentifier = "COUNTRY";
pointer = &person.country;
break;
}
cout<<"TYPE YOUR NEW "<<optionIdentifier<<": ";
cin>>input;
*pointer = input;
introduce(person);
}
int main()
{
person someone;
introduce(someone);
callMenu(someone);
cin.get();
}
btw, advices for making the code cleaner are welcome