Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Absolute C++ (4th Edition) part 61. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | Programming with Inheritance 607 When overloading the assignment operator in a derived class you normally use the overloaded assignment operator from the base class. To help understand the code outline we will give remember that an overloaded assignment operator must be defined as a member function of the class. If Derived is a class derived from Base then the definition of the overloaded assignment operator for the class Derived would typically begin with something like the following Derived Derived operator const Derived rightside Base operator rightSide The first line of code in the body of the definition is a call to the assignment operator of the Base class. This takes care of the inherited member variables and their data. The definition of the overloaded assignment operator would then go on to set the new member variables that were introduced in the definition of the class Derived. A complete example that includes this technique is given in the programming example Partially Filled Array with Backup later in this chapter. A similar situation holds for defining the copy constructor in a derived class. If Derived is a class derived from Base then the definition of the copy constructor for the class Derived would typically use the copy constructor for the class Base to set the inherited member variables and their data. The code would typically begin with something like the following Derived Derived const Derived Object Base Object probably more initializations The invocation of the base class copy constructor Base Object sets the inherited member variables of the Derived class object being created. Note that since Object is of type Derived it is also of type Base therefore Object is a legal argument to the copy constructor for the class Base. A complete example that includes a copy constructor in a base class is given in the programming example Partially Filled Array with Backup later in this chapter. Of course these techniques do not work unless you have a .