开发者

C++, please explain Classes to a Python user?

开发者 https://www.devze.com 2023-04-03 01:25 出处:网络
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

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 ());
    }

开发者_StackOverflow中文版    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


myLOG is the name of the class. It inherits (look it up2) from CLxLogMessage and has the functions GetFormat (which is virtual and can be overridden by subclasses and called through base class pointers, look it up2), Error, and Info. It has the data member authoringTool which is a string.

The public and private stuff is access specifiers. Something in the private section can only be used by the class's member functions, and stuff in the public section can be used by anybody. There is another type of section called protected which 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, and protected sections 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 private and protected members by declaring them as friends (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.


Even though there's no way to give a comprehensive answer or anything near that, maybe think about it like this: classes are types. Consider this:

int n;

Here "int" is the name of a type, and "x" is a variable of type "int". There are basic types in C++, like "int", "char", "double". Now we can also make new, compound types from old types:

struct Foo
{
  int n;
  char c;
  double d;
};

This defines a new type called "Foo", and Foo x; makes a new variable of that type. Now we can add some magic to the type "Foo":

class Foo
{
  int n;
  double d;

public:
  Foo() : n(20), d(0.5) { }   // "constructor"
};

The keywords struct and class almost mean the same thing, so we still have a compound type that has two member variables, n and d. However, this type also has a member function, and this one gets called every time you create a new Foo object. So when you say, Foo x;, then this variable's member value x.n will be set to 20 and x.d will be set to 0.5.

So that's that in a nutshell: Classes are types with built-in magic. And you are the magician.


The private and public is to do with data encapsulation, it means you can change the implementation of the class without affecting how it is used. I suggest reading up on some of the theory of object orientation.

0

精彩评论

暂无评论...
验证码 换一张
取 消