C++ Concept Map

Objects

An object is an instance of a class, which is the OO (object-oriented) way of saying it. In more common terms, an object is a variable of a given type. In C++, a class declaration also declares a new type.

Objects are declared in C form, by preceding the name, or names of the object or objects by the type (class) name. Thus:

NewType1 obj1, obj2;

declares two objects obj1 and obj2, both of type NewType1, where NewType1 is the name of a previously declared class.

Objects can also be initialized, i.e. its internal state can be set, by passing arguments to a constructor function when the object is declared. Thus:

NewType2 obj3(32);

declares an object of type NewType2, and passes 32 to its constructor. There can be any number of constructors, as long as each has a different signature, since member functions can be overloaded.

A class can also have a default constructor that takes no arguments. The parentheses may then be omitted, leaving a "regular" C declaration, like the first example above. The default constructor can also initialize the object since it can use constants to do so.

Objects have a lifetime, either governed by a local scope, or they can be created or destroyed dynamically.


Please mail any corrections and/or suggestions to Roger Hartley at
rth@cs.nmsu.edu

Copyright © 2003 Roger Hartley