15

What is a Class and Object in C++?

Can we say that a Class is an Object?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

22 Answers22

53

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

M.M
  • 138,810
  • 21
  • 208
  • 365
CodeFusionMobile
  • 14,812
  • 25
  • 102
  • 140
  • 3
    +1 Nice analogy. I think it could even work when you factor in things like inheritance and polymorphism. – William Brendel Jul 07 '09 at 18:32
  • 4
    Oh definately. You could think of inheritance as adding an addition to the home, or adding a garage or something of that nature. – CodeFusionMobile Jul 07 '09 at 18:34
  • My faith in humanity is restored. Your thoughtful, helpful answer has beat my short, flippant answer. I still can't believe mine got as many up votes as it did :-) – William Brendel Jul 07 '09 at 18:43
  • i like this answer. i was totally failing with my own one. – Johannes Schaub - litb Jul 07 '09 at 18:48
  • But, can we say that a Class is an Object? As in, a blueprint is something built from another blueprint? – Albert Jul 07 '09 at 18:52
  • 1
    @Albert, in C++, that would be a class-template, instantiating the classes, i think :) The color of the blueprint is its template parameter :p – Johannes Schaub - litb Jul 07 '09 at 18:59
  • 1
    @Albert: No. The analogy breaks there. In C++ there are no instnaces (objects) of the blue print. The blueprints are only used in the designing and building the house. At runtime you can have no instances of the blueprint. In langauges with reflection the line becomes a bit more blured but C++ has not reflection so the answer is no. – Martin York Jul 07 '09 at 20:07
  • That's really a nice way of explaining it. +1 for this :) – Juri Jul 14 '09 at 20:23
  • @dpp there is no such thing as a static class – M.M Feb 12 '17 at 23:21
17

An object is an instance of a class.

William Brendel
  • 31,712
  • 14
  • 72
  • 77
  • 2
    And not just in C++ either, in most if not all OO languages. – AlbertoPL Jul 07 '09 at 18:27
  • True, but doesn't explain anything. Not really helpful to the OP. – jalf Jul 07 '09 at 18:32
  • 1
    @jalf: To be honest, I thought this question would be closed long before this answer got any up votes. I answered almost as a joke, but for some reason people up voted it. Strange world we live in... – William Brendel Jul 07 '09 at 18:35
  • 1
    @CookieOfFortune: isn't Class actually a class in Java? Instances of Class are objects representing classes (types), and there can be a Class that means the Class class... but I'm pretty sure saying Class is an Object is false. – rmeador Jul 07 '09 at 19:25
  • 1
    @rmeador: Yes, Class is a class in Java. In fact, it's a generic, so when you load a class called Foo from an instance of ClassLoader, what you get back is an instance of Class. – William Brendel Jul 07 '09 at 19:30
  • @AlbertoPL: In most OO languages, not all of them. Some of them have no distinction between class and object: you just duplicate or inherit from what are essentially objects. There may be other ways of doing OO that I haven't run into yet. – David Thornley Jul 15 '10 at 14:21
  • In C++ there are objects that are not instances of classes. – Caleth Jan 19 '22 at 14:27
7

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

For example, I can define Dog as a class ...

class Dog {};

... and then create several objects, each of which is one instance of that class ...

Dog fido;
Dog spot;
ChrisW
  • 54,973
  • 13
  • 116
  • 224
6

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user88637
  • 11,790
  • 9
  • 37
  • 36
  • It should be Point a or SmartPoint a? – shingaridavesh Sep 30 '14 at 16:26
  • There is a fundamental difference between `Point` and `int_ptr`. The `struct` definition defines a new type. But the `typedef` does not define a new type. It defines another name for an existing type. The type `int *` is completely equivalent to `int_ptr`; for example you could not overload a function on `int_ptr` and `int *` separately. – M.M Feb 12 '17 at 23:24
3

No, an object is an instance of a class.

stevedbrown
  • 8,862
  • 8
  • 43
  • 58
  • 8
    You wasted 5 seconds by typing "No, " at the beginning of your sentence! (: – Alex Barrett Jul 07 '09 at 18:27
  • 1
    I have to say, by adding "no" at the beginning, stevedbrown did technically answer the question more accurately than I. Can't believe this is a real question though :-) – William Brendel Jul 07 '09 at 18:29
  • 1
    This is apparently a common point of confusion for beginners. – SingleNegationElimination Jul 07 '09 at 18:32
  • 3
    @TokenMacGuy: Oh I didn't mean to imply this topic isn't confusing for beginners. I guess I am just used to a time when if you knew enough to ask a question on SO, you knew the basics of OOP. I'm actually glad this type of question can be asked without being closed immediately. I think it shows that the community is expanding from a relatively small subset of "hardcore" programmers. – William Brendel Jul 07 '09 at 18:41
2

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.

Now seriously, a class is not an object.

Rui Craveiro
  • 2,224
  • 16
  • 28
1

Class is a collection of data member and member function.

Class is a user define data type.

Object is a class type variable.

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.

digitalextremist
  • 5,952
  • 3
  • 43
  • 62
Aftab
  • 1
  • 2
1

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 vehicles is a class and all the properties are it's members and car, bike, truck are objects of the class vehicles.

Surya
  • 171
  • 1
  • 9
1

No,class is not a object.

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

Nitesh
  • 303
  • 1
  • 5
0

C++ supports many paradigms, but not the 'everything is an object' style of object-oriented programming. Classes exist in the source code, but do not exist at run time. Even runtime type information doesn't preserve Classes as object, but only provides a capability to get opaque ids which correspond to the type.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
0

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 Dog named Bingo.

  • Dog would be the class defining its behaviours and properties

  • Bingo would be an object that is an instance of the Dog class

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.

Aidan
  • 41
  • 3
0

A class is not an object.

In simpler C language, a class is like a struct type, but more complex. Using a C struct example as analogy:

struct class_ {
    int attribute00;
    float attribute02;
    ...
}

struct class_ object_ = {0, 0.0, ...};

struct class_ is act like a class and object_ is act like an object. struct class_ has no physical storage in memory, object_ has physical storage in memory.

In human language, a word 'house' (as class) can defined in dictionary as place to stay, with doors, with windows, and rooms that you can only speak with your mouth to tell other people what is a house. A physical house (as object) is a solid build house on a land that you can move in and stay with your family.

A word 'house' take no physical occupation of land or space. A physical house occupy land and space.

ttchong
  • 307
  • 2
  • 9
  • Also, a physical house can have a name "ttchong house" or "white house" or "downing street 10". A word 'house' can not have a name. – ttchong Jul 15 '10 at 14:10
  • By the way, this seem to be a very beginning question for programmer in OO. However, I found it is really fun to get my self rephrase what I understand in a more precise and easy to understand way. – ttchong Jul 15 '10 at 14:12
0

Class is collection of data and functions,its user defined data type.Class is specification for object.So you can say that object is variable for class or object is instance of class.

Rohit Hajare
  • 125
  • 1
  • 7
0

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

Object is an data or a function which has an adress in run time memory and are also called as instances of class . Thus object is an class type variable or entity of c++ programming language.

Sriram
  • 1
0

In object-oriented programming , a class is a template definition of the method s and variable s in a particular kind of object . ... Subclasses can also define their own methods and variables that are not part of their superclass. The structure of a class and its subclasses is called the class hierarchy.

Tabzo
  • 1
-1

This is analogous to asking if a cat is a "My Kitten Mittens".

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
-1

In C++, Objects are essentially the variables and Classes are the types of their values.

Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
-1

Short Answer: In Languages C++, Java: No. In Languages like Python: Yes

  • In java, classes are objects and can be loaded at runtime. – Johannes Schaub - litb Jul 07 '09 at 18:52
  • 2
    @litb: it's a bit more subtle than that, though, because of Java's static typing. A Java class (or interface) is a static type, as well as being represented by an object at runtime. That type is not represented in javac by the same object as it is at runtime (how could it be - they're different JVMs). So I'm not sure it gives the full story to say that the object and the class are identical. It's much closer in Python, where a class really is just an object that happens to be capable of creating other objects and giving them a bunch of methods. – Steve Jessop Jul 07 '09 at 19:36
  • I stand corrected! Well some time ago i asked dudes in ##java about that, and they told me that classes are just objects, and you can access them with ClassName.class . But now i asked again, now some other nicks tell me the truth: Those "class objects" just represent the classes, they aren't the classes itself. The class still needs a definition at compile time, like you say. Thanks for your insight! – Johannes Schaub - litb Jul 07 '09 at 20:00
  • To be honest I wouldn't normally contradict someone who says "classes are objects in Java", any more than if they said "database rows are objects in the Hibernate ORM". Normally we allow "is" to mean "is represented by". It's just that comparing Java with Python, "classes are objects" is less true in Java. But still much more true than it is in C++, where a typeid obviously isn't the same thing as the class it represents :-) – Steve Jessop Jul 07 '09 at 22:10
  • I obviously need to get my hands dirty with python, i'm TEH python n00b out there. Just asked in #python about that classes-are-objects, but they confused me with "type is an instance of object, and object of type, and type is derived from object" and i was like "wtf??!". lol – Johannes Schaub - litb Jul 07 '09 at 22:32
-1

When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows:

class Box
{
   public:
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
};

The keyword public determines the access attributes of the members of the class that follow it. A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected which we will discuss in a sub-section.

hcarver
  • 7,126
  • 4
  • 41
  • 67
  • 1
    This looks like it's been copied from somewhere, and does not answer the question. It's also a terrible explanation. – Synchro Aug 05 '14 at 07:46
-1

A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. Example:

class Person {
//..properties
//..methods
}

An Object is an instance of a Class or created from a class. Example:

 Person John = new Person(); //Creating an object of Person class
    John.property1 = value;     //setting value of property1 for John which is of Person 
                               //Type
Baboo
  • 65
  • 1
  • 8
-2

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

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267