Absolute C++ (4th Edition) part 27

Absolute C++ (4th Edition) part 27. 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. . | Constructors 263 Display Class with Constructors part 2 of 2 41 DayOfYear DayOfYear int monthValue month monthValue day 1 42 43 testDate 44 45 DayOfYear DayOfYear month 1 day 1 46 Body intentionally empty. 47 uses iostream and cstdlib 48 void DayOfYear testDate 49 50 if month 1 month 12 51 52 cout Illegal month value n 53 exit 1 54 55 if day 1 day 31 56 57 cout Illegal day value n 58 exit 1 59 Definitions of the other member 60 functions are the same as in Display . Sample Dialogue Initialized dates February 21 May 1 January 1 datel reset to the following October 31 member function set which we included in the old version of the class shown in Display . Pitfall Constructors with No Arguments It is important to remember not to use any parentheses when you declare a class variable and want the constructor invoked with no arguments. For example consider the following line from Display DayOfYear date1 2 21 date2 5 date3 264 Constructors and Other Tools The object datel is initialized by the constructor that takes two arguments the object date2 is initialized by the constructor that takes one argument and the object date3 is initialized by the constructor that takes no arguments. It is tempting to think that empty parentheses should be used when declaring a variable for which you want the constructor with no arguments invoked but there is a reason why this is not done. Consider the following which seems like it should declare the variable date3 and invoke the constructor with no arguments DayOfYear date3 PROBLEM Not what you might think it is. The problem with this is that although you may mean it as a declaration and constructor invocation the compiler sees it as a declaration prototype of a function named date3 that has no parameters and that returns a value of type DayOfYear. Since a function named date3 that has no parameters and that returns a value of type DayOfYear is perfectly legal this notation always has that meaning. A different notation .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
487    100    2    24-06-2024
34    479    5    24-06-2024
Đã 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.