Share Price: $2.00 Total Worth: $4.00
...
The following “Program Notes” section explains the "Bye, Boffo Objects!" line of this output.
Note
You may have noticed that Listing 10.6 has an extra brace at the beginning and near the end of main(). Automatic variables such as stock1 and stock2 expire when the program exits the block that contains their definitions. Without the extra braces, that block would be the body of main(), so the destructors would not be called until after main() completed execution. In a windowing environment, this would mean that the window would close before the last two destructor calls, preventing you from seeing the last two messages. But with the braces, the last two destructor calls occur before the return statement is reached, so the messages are displayed.
Program Notes
In Listing 10.6, the statement
Stock stock1("NanoSmart", 12, 20.0);
creates a Stock object called stock1 and initializes its data members to the indicated values:
Constructor using NanoSmart called
Company: NanoSmart Shares: 12
The following statement uses another syntax to create and initialize an object called stock2:
Stock stock2 = Stock ("Boffo Objects", 2, 2.0);
The C++ Standard gives a compiler a couple ways to execute this second syntax. One is to make it behave exactly like the first syntax:
Constructor using Boffo Objects called
Company: Boffo Objects Shares: 2
The second way is to allow the call to the constructor to create a temporary object that is then copied to stock2. Then the temporary object is discarded. If the compiler uses this option, the destructor is called for the temporary object, producing this output instead:
Constructor using Boffo Objects called
Bye, Boffo Objects!
Company: Boffo Objects Shares: 2
The compiler that produced this output disposed of the temporary object immediately, but it’s possible that a compiler might wait longer, in which case the destructor message would be displayed later.
The following statement illustrates that you can assign one object to another of the same type:
stock2 = stock1; // object assignment
As with structure assignment, class object assignment, by default, copies the members of one object to the other. In this case, the original contents of stock2 are overwritten.
Note
When you assign one object to another of the same class, by default C++ copies the contents of each data member of the source object to the corresponding data member of the target object.
You can use the constructor for more than initializing a new object. For example, the program has this statement in main():
stock1 = Stock("Nifty Foods", 10, 50.0);
The stock1 object already exists. Therefore, instead of initializing stock1, this statement assigns new values to the object. It does so by having the constructor create a new, temporary object and then copying the contents of the new object to stock1. Then the program disposes of the temporary object, invoking the destructor as it does so, as illustrated by the following annotated output:
Using a constructor to reset an object
Constructor using Nifty Foods called >> temporary object created
Bye, Nifty Foods! >> temporary object destroyed
Revised stock1:
Company: Nifty Foods Shares: 10 >> data now copied to stock1
Share Price: $50.00 Total Worth: $500.00
Some compilers might dispose of the temporary object later, delaying the destructor call.
Finally, at the end, the program displays this:
Done
Bye, NanoSmart!
Bye, Nifty Foods!
When the main() function terminates, its local variables (stock1 and stock2) pass from your plane of existence. Because such automatic variables go on the stack, the last object created is the first deleted, and the first created is the last deleted. (Recall that "NanoSmart" was originally in stock1 but was later transferred to stock2, and stock1 was reset to "Nifty Foods".)
The output points out that there is a fundamental difference between the following two statements:
Stock stock2 = Stock ("Boffo Objects", 2, 2.0);
stock1 = Stock("Nifty Foods", 10, 50.0); // temporary object
The first of these statements invokes initialization; it creates an object with the indicated value, and it may or may not create a temporary object. The second statement invokes assignment. Using a constructor in an assignment statement in this fashion always causes the creation of a temporary object before assignment occurs.
Tip
If you can set object values either through initialization or by assignment, choose initialization. It is usually more efficient.
C++11 List Initialization
With C++11, can you use the list-initialization syntax with classes? Yes, you can, providing the brace contents match the argument list of a constructor:
Stock hot_tip = {"Derivatives Plus Plus", 100, 45.0};
Stock jock {"Sport Age Storage, Inc"};
Stock temp {};
The braced lists in the first two declarations match the following constructor:
Stock::Stock(const std::string & co, long n = 0, double pr = 0.0);