Err... no. That is a horrible way of doing things. The member variable is private in order to restrict direct access to it. Attempting to circumvent access in that way is nonsense.
Originally Posted by Oper1992
how access isnice?
Provide a member function that returns the value of isnice.
By the way, this is likely to result in a compile error:
Code:
CFood food = new CFood( TRUE, FALSE );
It should be:
Code:
CFood food( TRUE, FALSE );
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Your teacher might be factually correct, i.e., by stating that something can be done, but "morally" wrong, i.e., by teaching you something that should not be done.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
he is not isnice, but insane. As GCDEF said, get a new teacher.
Code:
class CFood
{
private:
BOOL isnice;
BOOL isbad;
public:
BOOL getIsnice () {return (isnice);}
void setIsnice (BOOL bIsnice) {isnice = bIsnice;}
CFood( BOOL a, BOOL b );
void Eat();
};
Something like this is much better...
What your teacher is showing you is not only morally wrong, but also technically. Your teacher really hasn't understood the idea behind object oriented programming.
Last edited by Skizmo; October 22nd, 2010 at 06:32 AM.
He's teaching really bad practices. For example, what happens if somebody inserts another variable before isnice? Your code breaks. One of the main design features of object oriented programming is encapsulation, which in part means that users of a class don't need to know about the inner workings of the class. I have no idea why your teacher is teaching you such a hideous hack, but you should NEVER do anything like that in real code. Never.
You should send him to this thread and let him explain himself.
Bookmarks