Definitely struggling with C++ and could use some help with my assignment. I'm not looking for someone to do it. Just clarification on what needs to be done.
I am creating a currency exchange program. A member function named getdata will ask the user for the type of currency. Then input the character into the data member "entered.type".
What does it mean by data member "entered.type"? I'm looking through the book and don't see an example of what this means.
If someone could explain it or show me an example I would appreciate it. Thanks.
All data within a class is by default private, so you have to manually set public if you want to access it. Nothing from the class can be accessed by outside the class, for example...
1 2 3 4 5 6 7 8 9
class MyClass
{
public:
void GetMoney();
void TransferMoneyType();
private:
int myMoney;
int moneyType;
};
Now if you try to do this..
1 2
MyClass mc;
mc.myMoney = 10;
You will get an error because you're trying to access a private class member. So to get around this you need to set up GetMoneyType() or whatever function you have to change the information, like this..
1 2 3 4 5 6 7
void MyClass::GetMoney()
{
cout << "What currency are you trying to transfer (USD, CAD, etc): ";
cin >> moneyType;
cout << "How much money do you have: ";
cin >> myMoney;
}
Then do this...
1 2
MyClass mc;
mc.GetMoney();
You could technically set all of the data variables (such as myMoney and moneyType) to public, but that is bad programming. All variables should be private and then you should set up functions to access private class members.