You'd have to prove it to me because I know you're wrong.
You'd have to prove it to me because I know you're wrong.
I'd guess that it's because you're not losing any bits, just changing the interpretation of the MSB.
Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.
Actually it depends on where you want to do that. Your teacher is right, since you can't access private member data, you have to use a public member function in code that uses the class. However, member functions will always have access to at least one set of data. For example:
myAccount.setAccBal(Amt);
Member function setAccBal will always have access to the data of the object it is called with, called this object by the implementation. Additionally, member functions will also have access to data members of other objects of the same class, if you pass them in as arguments.
ok, real sorry i didnt reply earlier but i lost my internet conenction for a while (heavy rains and stuff)
ok to reply to you all now:
the member functions have access, but I was told to avoid accessing it directly. So i was trying to find a way to use the set and get functions to do the functions in the implementation.
I was told to do it like this and we haven't learned exceptions yet as well so yeah
same as above, it was mostly structured in that we were given the basic premise, the code for the tester.cpp and we were to write the class header and cpp file to make the output the same as the one given to us.
i know the member functions of a class can access the private data members but my teacher personally prohibits us from accessing it directly for his class even though he told it it does work.Actually it depends on where you want to do that. Your teacher is right, since you can't access private member data, you have to use a public member function in code that uses the class. However, member functions will always have access to at least one set of data. For example:
myAccount.setAccBal(Amt);
Member function setAccBal will always have access to the data of the object it is called with, called this object by the implementation. Additionally, member functions will also have access to data members of other objects of the same class, if you pass them in as arguments.
@Elysia: I figured this much but how can you put the value that the get function in another variable without the direct access to the data member? I am trying not to use the data member itself but using the set and get to manipulate the value if possible. i could just do something like this:The getAccBal here does absolutely nothing. This get function is supposed to enable the outside to access the account balance, and returns that. So you would have to store that balance in some variable to use it.
Nevertheless, getAccBal simply returns the private member that contains the balance which you have access to since you are inside the class implementation, so you could use that instead if you want.
int X;
Account::getAccBal();
X = AccBal;
but that would be accessing the data member directly? or is there another way?
Last edited by program; 09-10-2011 at 02:03 PM.
thanks again! and thanks to everyone to who helped me as well
Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.