- 论坛徽章:
- 1
|
c++ primer plus fourth edtion:
The Default Constructor
The default constructor is the constructor used to create an object when you don't provide explicit initialization values. That is, it's the constructor used for declarations like this:
Stock stock1; // uses the default constructor
Hey, Listing 10.3 already did that! The reason this statement works is that if you fail to provide any constructors, C++ automatically supplies a default constructor. It's a default version of a default constructor, and it does nothing. For the Stock class, it would look like this:
Stock::Stock() { }
The net result is that the stock1 object is created with its members uninitialized, just as
int x;
creates x without providing it a value. The fact that the default constructor has no arguments reflects the fact that no values appear in the declaration.
A curious fact about the default constructor is that the compiler provides one only if you don't define any constructors. Once you define any constructor for a class, the responsibility for providing a default constructor for that class passes from the compiler to you. If you provide a nondefault constructor, such as Stock(const char * co, int n, double pr) and don't provide your own version of a default constructor, then a declaration like
Stock stock1; // not possible with current constructor
becomes an error. The reason for this behavior is that you might want to make it impossible to create uninitialized objects. If, however, you wish to create objects without explicit initialization, you must define your own default constructor. This is a constructor that takes no arguments. You can define a default constructor two ways. One is to provide default values for all the arguments to the existing constructor:
Stock(const char * co = "Error", int n = 0, double pr = 0.0);
The second is to use function overloading to define a second constructor, one that has no arguments:
Stock();
You can have only one default constructor, so be sure that you don't do both. (With early versions of C++, you could use only the second method for creating a default constructor.)
Actually, you usually should initialize objects in order to ensure that all members begin with known, reasonable values. Thus, the default constructor typically provides implicit initialization for all member values. Here, for example, is how you might define one for the Stock class:
Stock::Stock()
{
strcpy(company, "no name");
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Tip
When you design a class, you usually should provide a default constructor that implicitly initializes all class members.
After you've used either method (no arguments or default values for all arguments) to create the default constructor, you can declare object variables without initializing them explicitly:
Stock first; // calls default constructor implicitly
Stock first = Stock(); // calls it explicitly
Stock *prelief = new Stock; // calls it implicitly
However, don't be misled by the implicit form of the nondefault constructor:
Stock first("Concrete Conglomerate"); // calls constructor
Stock second(); // declares a function
Stock third; // calls default constructor
The first declaration calls the nondefault constructor, that is, the one that takes arguments. The second declaration states that second() is a function that returns a Stock object. When you implicitly call the default constructor, don't use parentheses. |
|