What is a Class and Object in C++?

前端 未结 20 949
走了就别回头了
走了就别回头了 2020-12-09 10:33

What is a Class and Object in C++?

Can we say that a Class is an Object?

相关标签:
20条回答
  • 2020-12-09 11:06

    No, an object is an instance of a class.

    0 讨论(0)
  • 2020-12-09 11:06

    No,class is not a object.

    A class is a data type and object is the variable(instance) of class data type.

    0 讨论(0)
  • 2020-12-09 11:07

    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).

    0 讨论(0)
  • 2020-12-09 11:11

    A class is a logical construct while object is its physical copy.

    A class can be thought of as a mould from which multiple objects are created which appear identical to the class

    Objects can be thought of as carbon copies of the class. A perfect example of inheritance principle.

    A class can be thought of as a parent of its children - objects

    0 讨论(0)
  • 2020-12-09 11:12

    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 type and is "a" is a variable which has the type "int".

    C++ provides various ways to let the programmer define new types; for example:

    typedef int* int_ptr;
    int_ptr a;
    

    In this example , a new type is defined int_ptr. "int_ptr" is a type , "a" is a variable which has the type "int_ptr". Another example:

    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".

    So what is a class in C++? A class is another way to define a new type, just like the other ways mentioned above.

    What is an object? An object is a variable which has a type that was defined using the class keyword.

    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".

    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.

    0 讨论(0)
  • 2020-12-09 11:13

    Both classes and instances are objects, but object oriented programming doesn't force the language to have classes & instances.

    0 讨论(0)
提交回复
热议问题