什么是 C++ 中的类和对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1093968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a Class and Object in C++?
提问by William Brendel
What is a Class and Object in C++?
什么是 C++ 中的类和对象?
Can we say that a Class is an Object?
我们可以说一个类是一个对象吗?
回答by CodeFusionMobile
A Class is like a blueprint, an object is like a house built from that blueprint.
一个类就像一个蓝图,一个对象就像一个根据蓝图建造的房子。
You can have many houses with the same layout/floorplan (read class), but each is it's own instance (read object). Each has it's own owner, furniture, etc.
您可以拥有许多具有相同布局/平面图(读取类)的房屋,但每个房屋都是它自己的实例(读取对象)。每个人都有自己的主人,家具等。
Note that there are also objects whose blueprint is not a class (e.g. integers).
请注意,还有一些对象的蓝图不是类(例如整数)。
回答by William Brendel
An object is an instance of a class.
对象是类的实例。
回答by user88637
I will try to give more technical explanation rather than an abstract one. I think that definitions like "a class is a blueprint and an object is something made from this blueprint" are impossible to understand for newbies simply because these kind of definitions are abstract and context-less.
我将尝试给出更多技术性的解释而不是抽象的解释。我认为像“类是蓝图,对象是由该蓝图制成的东西”之类的定义对于新手来说是不可能理解的,因为这些定义是抽象的和无上下文的。
Classes and objects have a pure abstract meaning in the object oriented world but for simplicity I will reduce the definition to a more practical one.
类和对象在面向对象的世界中具有纯粹的抽象意义,但为了简单起见,我将定义简化为更实用的定义。
Consider the following statement:
考虑以下语句:
int a;
"int" is a typeand is "a" is a variable which has the type "int".
“int”是一种类型,“a”是一个类型为“int”的变量。
C++ provides various ways to let the programmer define new types; for example:
C++ 提供了多种方式让程序员定义新类型;例如:
typedef int* int_ptr;
int_ptr a;
In this example , a new type is definedint_ptr. "int_ptr" is a type , "a" is a variable which has the type "int_ptr". Another example:
在本例中,定义了一个新类型int_ptr。“int_ptr”是一个类型,“a”是一个类型为“int_ptr”的变量。另一个例子:
struct Point
{
int x;
int y;
};
Point a;
Here, a new type is defined, "Point", and "a" is a variable which has the type "Point".
这里定义了一个新类型,“Point”,“a”是一个类型为“Point”的变量。
So what is a class in C++? A class is another way to define a new type, just like the other ways mentioned above.
那么什么是 C++ 中的类?类是定义新类型的另一种方式,就像上面提到的其他方式一样。
What is an object? An object is a variable which has a type that was defined using the class keyword.
什么是对象?对象是具有使用 class 关键字定义的类型的变量。
For example:
例如:
class SmartPoint
{
public:
Point(x,y);
Move(x,y);
protected:
int x,y ;
};
SmartPoint a;
In this example, a new type is defined, "SmartPoint", and "a" is a variable which has the type "SmartPoint".
在这个例子中,定义了一个新类型“SmartPoint”,“a”是一个类型为“SmartPoint”的变量。
You may ask then what is different between a type defined by using the "class" keyword or "struct" keyword or "typedef" — but that is a matter for another discussion.
您可能会问,使用“class”关键字或“struct”关键字或“typedef”定义的类型有什么不同——但这是另一个讨论的问题。
回答by ChrisW
An object is some data, which has an address in run-time memory.
对象是一些数据,它在运行时内存中有一个地址。
There are different types of object (e.g. int, float, etc.). You can create user-defined types, called 'classes'.
有不同类型的对象(例如 int、float 等)。您可以创建用户定义的类型,称为“类”。
For example, I can define Dog as a class ...
例如,我可以将 Dog 定义为一个类......
class Dog {};
... and then create several objects, each of which is one instance of that class ...
...然后创建多个对象,每个对象都是该类的一个实例...
Dog fido;
Dog spot;
回答by stevedbrown
No, an object is an instance of a class.
不,对象是类的实例。
回答by Rui Craveiro
No, an object is an instance of a class...
不,对象是类的实例...
Unless...
除非...
If you are implementing a software design tool that allows you to represent classes, interfaces, properties, inheritance, associations, aggregations, etc., then at runtime, yes, each class you place in the designer will be an object instance of the Class class. Ok, couldn't help myself finding an example so twisted and meta.
如果您正在实现一个软件设计工具,它允许您表示类、接口、属性、继承、关联、聚合等,那么在运行时,是的,您放置在设计器中的每个类都将是 Class 类的对象实例. 好吧,我忍不住找到了一个如此扭曲和元的例子。
Now seriously, a class is not an object.
现在说真的,一个类不是一个对象。
回答by Aftab
Class
is a collection of data member and member function.
Class
是数据成员和成员函数的集合。
Class
is a user define data type.
Class
是用户定义的数据类型。
Object
is a class type variable.
Object
是一个类类型变量。
Objects are also called instance of the class.
对象也称为类的实例。
Each object contains all members(variables and functions) declared in the class. We can access any data member or member function from object of that class using .
operator.
每个对象都包含在类中声明的所有成员(变量和函数)。我们可以使用.
运算符从该类的对象访问任何数据成员或成员函数。
回答by Surya
Here is an anology.
we have a classification called vehicles. Each vehicle will have some properties like :
这是一个类比。
我们有一个分类叫做车辆。每辆车都有一些属性,如:
- seating capacity
- fuel capacity
- fuel consumption
- type of transmission
- 座位数
- 燃料容量
- 燃油消耗
- 传输类型
Car, bike, truck, are some instances of vehicles. Each may have different set of properties.
Here vehiclesis a class and all the properties are it's members and car, bike, truck are objects of the class vehicles.
汽车、自行车、卡车是车辆的一些实例。每个可能有不同的属性集。
这里的车辆是一个类,所有属性都是它的成员,汽车、自行车、卡车是类车辆的对象。
回答by Nitesh
No,class is not a object.
不,类不是对象。
A class is a data type and object is the variable(instance) of class data type.
类是一种数据类型,对象是类数据类型的变量(实例)。
回答by Aidan
Class: A class defines a particular type's behaviours and properties.
类:类定义了特定类型的行为和属性。
Object: An object is an instance of a class.
对象:对象是类的实例。
For example, if you have a Dognamed Bingo.
例如,如果您有一只名为Bingo的Dog。
Dogwould be the classdefining its behaviours and properties
Bingowould be an objectthat is an instance of the Dogclass
Dog将是定义其行为和属性的类
Bingo将是一个对象,它是Dog类的一个实例
Strictly speaking, a Class is not an Object in C++. But in languages such as C# and Java that supports reflection, classes can be used like objects but that is a more advance topic and probaly not what the original question is asking.
严格来说,类不是 C++ 中的对象。但是在支持反射的 C# 和 Java 等语言中,类可以像对象一样使用,但这是一个更高级的主题,可能不是原始问题所要问的。