Can we say that a Class is a Object?
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.
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;
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.
In C++, Objects are essentially the variables and Classes are the types of their values.
Both classes and instances are objects, but object oriented programming doesn't force the language to have classes & instances.
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.
I will try to give more technical explanation rather then 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 , "a" is a variable which have the type : "int".
c++ enable various ways to let the programer 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 its type is "int_ptr". Another example:
struct Point
{
int x;
int y;
};
Point a;
Here , a new type is defined , "Point" , "a" is a varaible which is type is "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 type that was defined using the class keyword.
For example:
class SmartPoint
{
public:
Point(x,y);
Move(x,y);
protected:
int x,y ;
};
Point a;
In this example , a new type is defined , "SmartPoint". "a" is a variable which its type is "SmartPoint".
You may ask then what is different between a type defined by using the "class" keyword or "struct" keyword or "typedef" , well this is a matter for another discussion.
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.
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.