Understanding an Instance

I am trying to understand what an instance is. I was trying to read on the internet but it's still not clear. I post the code below as an example. What I understand is that there is an instance of m_a, m_b and m_c are associated with Object_C. But where is this instance, is it invisible?

Please I appreciate any explanation and clarification of an instance!

1
2
3
4
5
6
7
  class A
{
private:
int m_a;
double m_b;
int* m_c;
} Object_C;
That is a shorhand for:
1
2
3
4
5
6
7
8
9
class A
{
private:
  int m_a;
  double m_b;
  int* m_c;
};

A Object_C;

Assuming that the class definition is in global scope, then the Object_C is a global variable, just like Object_D is in the following:
1
2
3
4
5
6
int Object_D;

int main()
{
  return 0;
}

Now, Object_C is a variable, and you do access members of compound type as usual:
Object_C.m_a = 7;
Alas! The members are private and therefore only the member functions of class A are allowed to access the member variables.
closed account (jvqpDjzh)
Basically, an instance is an object of a class.
What is an object? An object is a variable, like in this situation:
int x = 10;//x is a variable of type int
But usually for instance/object we mean a variable more complex (than an integer), which means that it can hold other simpler variables (integers, doubles, etc.), like in your example:
1
2
3
4
5
6
7
8
9
10
class A//A, we can say, is a more complex type than a int
{
private://A contains other kinds of variables: int, double, int*
    int m_a;
    double m_b;
    int* m_c;
} Object_C;//Here you are declaring an instance/object of type "A"

//declaring another instance of type A
A objectXXX;


Let's take a more concrete real example. Think about cars. What is a car? A car is an object? No, a car is just a concept (an amount of characteristics) that every real car is supposed to have.
So what is a car, if it's not an object? It's a type, it's something that characterize all the real cars, it's a (abstract) class.
So what the heck is an object? Well an object is what you can manage, what you can drive, what you can modify, it's a variable, it's something concrete and real, it's an instance of the type car! Is for example a Ferrari or a Fiat, like in your example Object_C. In fact, objects of the same type, even though they all have the same base, they can be briefly different, for example a Ferrari is surely more fast than a Fiat, but all both have a max speed .
Last edited on
What I understand is that there is an instance of m_a, m_b and m_c are associated with Object_C. But where is this instance, is it invisible


To use C++ terminology, what you have here is an object with the name Object_C. It has member sub-objects with names Object_C.m_a, Object_C.m_b, and Object_C.m_c.

Those member subobjects are accessible, by name, from member functions and friends of Object_C (if you were to define any). Since you didn't define any, they are indeed not accessible in a program, although they contribute to Object_C's object representation: within the bytes that Object_C occupies in memory, there are bytes that belong to Object_C.m_a, there are bytes that belong to Object_C.m_b, and there are bytes that belong to Object_C.m_c. And there are a few bytes that do not belong to those members.

See http://en.cppreference.com/w/cpp/language/object for a summary
I totally appreciate all your responds guys. I understand the concept of an object in c++. So if an instance is an object, why do they have different names?
If caribou are reindeer, why do they have different names?
English is my second language, so i don't know what they are. I found this explanation online "Example, we have a blueprint (class) represents student (object) with fields like name, age, course (class member). And we have 2 students here, Foo and Bob. So, Foo and Bob is 2 different instances of the class (Student class) that represent object (Student people)".

So now I understand an instance represents an object, is that correct?
An object is an instance of a type.

When you instantiate a type, you create an object.
I think I have the idea and the concept but it would be more clear if someone can write a code pointing at the instance, class and object!
Topic archived. No new replies allowed.