A Complete Guide to Programming in C++ part 54

A Complete Guide to Programming in C++ part 54. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | REDEFINING MEMBERS 509 Redefinition There are two options for the names of data members or methods in derived classes 1. The name does not occur in the base class no redefinition. 2. The name already exists in the base class redefinition. In the second case the member of the same name continues to exist unchanged in the base class. In other words redefining members in a derived class has no effect on the base class. However the name lookup rules for the compiler lead to the following scenario if a member is redefined in a derived class it will mask the corresponding member in the base class. This situation is similar to the one seen for local and global variables. A local variable will mask a previously defined global variable with the same name. Redefinition and Overloading Normally methods are redefined in derived classes. This adopts the methods to the new features of the class. When a method is redefined the signature and the return type of the method can be changed. However a redefinition does not overload functions since the derived class has a different scope. Redefining a method will always mask a method with the same name in the base class. Of course you can overload methods within the same class and this means you can repeatedly redefine a base class method for a derived class. Access to the Members in the Base Class If you redefine a method in a derived class this does not alter the fact that the base class method still exists. If the method was declared in the public section of the base class you can call it to redefine a method. The range operator is used to access the base class method. The new version of the display method opposite illustrates this point. The display method defined in the base class is used to output the data members of the base class. To do so you must use the range operator with the name of the base class. Otherwise the display method in the derived class will call itself and head off into an indefinite recursion. 510 CHAPTER 23 .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.