Creating Objects of Classes in C++

Remember that the declaration of item class as shown before does not define any objects (i.e., variables) of item but only specifies what they will contain. Once a class has been declared, we can create an variable of that type by using the class name (like any other built-in type variable).

For example:

creates a variable x of type item. In C++, the class variables are known as objects.
Therefore, x is called an object of type item. We may also declare more than one object in one statement link:

The declaration of the object is similar to that of a variable of any basic type.

Objects can also be created when a class is defined by placing their names immediately after the closing brace, as we do in the case of structures. That is, the definition:

would create the objects x, y and z of type item. This practice is seldom followed because we would like to declare the objects close to the place where they are used.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *