I’m trying to learn C++, Thanks to this article I find many similarity between C++ and Python and Javascript: http://www.cse.msu.edu/~cse231/python2Cpp.html
But I can’t understand C++ Classes at all, they looks like Javascript prototypes, but not that easy.
For example:
//CLxLogMessage defined in header
class myLOG: public CLxLogMessage{
public:
virtual const char * GetFormat (){
return "Wavefront Object";
}
void Error (const std::string &msg){
CLxLogMessage::Error (msg.c_str ());
}
void Info (const std::string &msg){
CLxLogMessage::Info (msg.c_str ());
}
private:
std::string authoringTool;
};
Question: What is this Public/Private stuff at all!?
Edit: To be honest, I more enjoy C++ than Python, because I can learn truth meaning of everything, not simple automated commands, for example I preferred to use “int X” rather than “X” alone.
Thanks
myLOGis the name of the class. It inherits (look it up2) fromCLxLogMessageand has the functionsGetFormat(which isvirtualand can be overridden by subclasses and called through base class pointers, look it up2),Error, andInfo. It has the data memberauthoringToolwhich is a string.The
publicandprivatestuff is access specifiers. Something in theprivatesection can only be used by the class’s member functions, and stuff in thepublicsection can be used by anybody. There is another type of section calledprotectedwhich means that only a class and its subclasses can access it, but nobody else1.If you start adding stuff to a class without setting an access level first, it defaults to
private.You can have as many
public,private, andprotectedsections as you want, in any order.You need these different protection levels because you don’t want other people messing with your data when you don’t know about it. For example, if you had a class representing fractions, you wouldn’t want someone to change the denominator to a 0 right under your nose. They’d have to go through a setter function which would check that the new value was valid before setting the denominator to it. That’s just a trivial example though. The fact that Python does not have these is a shortcoming in the language’s design.
All your questions would be answered if you had read a C++ book. There is no easy way out with C++. If you try to take one, you’ll end up being a horrible C++ programmer.
1 You can let somebody else access
privateandprotectedmembers by declaring them asfriends (look it up2).2 Sorry for saying “look it up” so much, but it’s too much information for me to put here. You’ll have to find a good resource for these kinds of things.