What's the differnece between object, class, and instance?

I was looking at my notes the other day and realized I still don’t fully understand the difference between an object, class, and instance.
From what I know:

  • An object is a thing … much more than this and I don’t know how to define an object. An object is a function, variable, etc. right?
  • A class is a way of encapsulating (or is it abstraction?) code to make it more simple to use. All the objects inside the class can be of varying types and lengths and are, by default, private.
  • I know that in C++ an instance and an object are the same. But I also read that when use a class it creates an instance of the class than can be treated as an object. What does this all mean?

Could anyone explain it and offer an example?
Thanks in advance.

A class is a blue print for objects. Objects are instantiated from the class, which means any object that is created from a class is an instance of that class. For example:

class Man{
     private String name;
     private int age;

     Man(String name, int age) {
          this.name = name;
          this.age = age;
     }

     private String getName() {
          return this.name;
     }
}

is a class called Man and has two attributes, name and age and two methods, one is called getName which can be used later and another one is called Man, which is a constructor. A constructor is called when the class is instantiated. Meaning, if I create an object of this class:

Man jack = new Man("Jack", 22) will create an object, which is an instance of class called Man. And if you ever want to get the name of your object called jack, you can do it by calling the method of the class your object is instantiated from: String name = jack.getName().

I tried to use class, object and instance as much as I could. I hope you understand it better now.

@LVila Your example is C#. C++ is different, an object doesn’t mean an instance of a class here. (Also modern C++ code shouldn’t be using new)

Basically some region of memory that has some value of some type. cppreference specifies an object as:

An object, in C++, is a region of storage that has

A class is a way of defining your own type which you typically build up from other types. Using standard C++. std::string is such thing, it’s not a built-in type i.e. it doesn’t come part of the language but the C++ Standard Library.

The difference is that you use the class to create objects.

#include <string>

class employee
{
    std::string name_;
    int age_;
    int id_;
public:
    employee(const std::string& name, int age, int id) : name_(name), age_(age), id_(id)
};

This emits absolutely no code. No objects are created, it’s just defining what the type is. If you were to then add this

employee person("Bob", 30, 1);

in main for example then you would be creating an object called person
https://godbolt.org/z/twPz8q

@LVila Thanks for your explanation! it was great other than it was in C# lol

@DanM Ok, I understand what objects and classes do. Classes are used as blueprints to create new types, and consequently objects, no? Where does the instance come into play? Is an object just the instantiation (a use/version) of the class? If this is the case, is the reason why an object and an instantiation are the same (in C++) due to the fact that to create an instance you have to create an object and vice versa? (hope that made sense…)

P.S. what does the : do in employee(const std::string& name, int age, int id) : name_(name), age_(age), id_(id)? And why is name declared as a reference?

Object and instance are synonymous in C++

int get_foo(); //function declaration

int x;
std::string y;
person bob;
get_foo();

x, y, bob, and what is returned from get_foo() are all objects. The last one being an example of an unnamed object.

Read the cppreference defintion again to see if it also defines your view of what an instance is.

It’s “member initialisation list” syntax. It’s the preferred way to initialise members in a constructor.

To avoid an unnecessary copy.

class foo
{
    std::string x;
    foo(std::string y) : x(y) {}
};

If it were written like this then y is a copy of the argument and then x makes a copy of y. Typically I would pass by value and then std::move it but I didn’t want to confuse you by adding in move semantics for no benefit.

Privacy & Terms