An object is a typed region of storage in the execution environment; in addition to retaining state information, it also defines behavior. Class-type objects are defined using class-name. Consider the following code fragment:
class Account // Class name is Account.
{
public:
Account(); // Default constructor.
Account( double ); // Construct from double.
double& Deposit( double );
double& Withdraw( double, int );
...
}:
Account CheckingAccount; // Define object of class type.
The preceding code declares a class (a new type) called Account. It then uses this new type to define an object called CheckingAccount.
The following operations are defined by C++ for objects of class type:
Assignment. One object can be assigned to another. The default behavior for this operation is a memberwise copy. This behavior can be modified by supplying a user-defined assignment operator.
Explicit initialization of an object. For example:
Point myPoint = thatPoint;
declares myPoint as an object of type Point and initializes it to the value of thatPoint.
Initialization caused by passing as an argument. Objects can be passed to functions either by value or by reference. If they are passed by value, a copy of each object is passed to the function. The default method for creating the copy is memberwise copy; this can be modified by supplying a user-defined copy constructor (a constructor that takes a single argument of the type “reference to class”).
Initialization caused by returning as the result of a function. Objects can be returned from functions either by value or by reference. The default method for returning an object by value is a memberwise copy; this can be modified by supplying a user-defined copy constructor. An object returned by reference (using pointer or reference types) should not be both automatic and local to the called function. If it is, the object referred to by the return value will have gone out of scope before it can be used.
“Overloaded Operators” in Chapter 12, on topic explains how to redefine other operators on a class-by-class basis.
You can declare empty classes, but objects of such types still have nonzero size. The following example illustrates this:
#include <iostream.h>
class NoMembers
{
};
int main()
{
NoMembers n; // Object of type NoMembers.
cout << “The size of an object of empty class is: ”
<< sizeof n << endl;
return 0;
}
The output of the preceding program is:
The size of an object of empty class is: 1.
The memory allocated for such objects is of nonzero size so that the objects have different addresses. Having different addresses is important because it must be possible to compare pointers to objects for identity. Without distinct addresses, such a comparison is impossible. Also, in arrays, each member array must have a distinct address.
Important:
An empty class is a class with no data members. Empty classes can have functions, including constructors, destructors, and so on, which define their behavior.
Microsoft Specific
An empty base class typically contributes zero bytes to the size of a derived class.¨