1. Code reuse in C++:
l Create objects of your existing class inside the new class. This is called composition because the new class is composed of objects of existing classes.
l Create a new class as a type of an existing class. You literally take the form of the existing class and add code to it, without modifying the existing class. This magical act is called inheritance, and most of the work is done by the compiler.
2. When you inherit, you are saying, “This new class is like that old class.” You state this in code by giving the name of the class as usual, but before the opening brace of the class body, you put a colon and the name of the base class (or base classes, separated by commas, for multiple inheritance). When you do this, you automatically get all the data members and member functions in the base class.
3. It’s also interesting that the order of constructor calls for member objects is completely unaffected by the order of the calls in the constructor initializer list. The order is determined by the order that the member objects are declared in the class.
评论