[英]C++, please explain Classes to a Python user?
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 我正在尝试学习C ++,由于这篇文章,我发现C ++与Python和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. 但是我根本无法理解C ++类,它们看起来像Javascript原型,但并不那么容易。
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. 编辑:老实说,我比Python更喜欢C ++,因为我可以学习所有东西的真实含义,而不是简单的自动化命令,例如,我更喜欢单独使用“ int X”而不是“ X”。
Thanks 谢谢
myLOG
is the name of the class. myLOG
是类的名称。 It inherits (look it up 2 ) from CLxLogMessage
and has the functions GetFormat
(which is virtual
and can be overridden by subclasses and called through base class pointers, look it up 2 ), Error
, and Info
. 它从
CLxLogMessage
继承(查找2 ),并具有GetFormat
函数(它是virtual
,可以被子类覆盖并通过基类指针调用,查找2 ), Error
和Info
。 It has the data member authoringTool
which is a string. 它具有数据成员
authoringTool
,它是一个字符串。
The public
and private
stuff is access specifiers. public
和private
内容是访问说明符。 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. private
部分中的内容只能由该类的成员函数使用,而public
部分中的内容则可以由任何人使用。 There is another type of section called protected
which means that only a class and its subclasses can access it, but nobody else 1 . 还有另一种类型的部分称为
protected
,这意味着只有一个类及其子类可以访问它,而其他人则不能1 。
If you start adding stuff to a class without setting an access level first, it defaults to private
. 如果您在不先设置访问级别的情况下开始向类中添加内容,则默认为
private
。
You can have as many public
, private
, and protected
sections as you want, in any order. 您可以按任意顺序有任意多个
public
, private
和protected
部分。
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.
例如,如果您有一个代表分数的类,那么您不希望有人在您的鼻子下面将分母更改为0。 They'd have to go through a setter function which would check that the new value was valid before setting the denominator to it.
他们必须通过setter函数,该函数将在设置分母之前检查新值是否有效。 That's just a trivial example though.
不过,这只是一个简单的例子。 The fact that Python does not have these is a shortcoming in the language's design.
Python没有这些功能的事实是该语言设计的一个缺点。
All your questions would be answered if you had read a C++ book. 如果您读过C ++书籍,则所有问题都将得到解答。 There is no easy way out with C++.
C ++没有简单的出路。 If you try to take one, you'll end up being a horrible C++ programmer.
如果您尝试使用它,那么您最终将成为可怕的C ++程序员。
1 You can let somebody else access private
and protected
members by declaring them as friend
s (look it up 2 ). 1您可以通过将其他人声明为
friend
来让其他人访问private
成员和protected
成员(查找2 )。
2 Sorry for saying "look it up" so much, but it's too much information for me to put here. 2很抱歉说了“查找”,但是对我来说太多了。 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 . 即使无法给出全面的答案或类似的答案,也可以这样考虑:类是type 。 Consider this:
考虑一下:
int n;
Here "int" is the name of a type, and "x" is a variable of type "int". 这里的“ int”是类型的名称,“ x”是类型“ int”的变量。 There are basic types in C++, like "int", "char", "double".
C ++中有一些基本类型,例如“ 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;
这定义了一个新的类型,称为“ Foo”,和
Foo x;
makes a new variable of that type. 生成该类型的新变量。 Now we can add some magic to the type "Foo":
现在,我们可以为“ 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
. 关键字
struct
和class
几乎具有相同的含义,因此我们仍然具有一个具有两个成员变量n
和d
的复合类型。 However, this type also has a member function , and this one gets called every time you create a new Foo object. 但是,此类型还具有一个成员函数 ,每次创建新的Foo对象时都会调用该成员函数 。 So when you say,
Foo x;
所以当你说
Foo x;
, then this variable's member value xn
will be set to 20 and xd
will be set to 0.5. ,则此变量的成员值
xn
将设置为20, xd
将设置为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.
我建议阅读一些面向对象的理论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.