TAILIEUCHUNG - A Complete Guide to Programming in C++ part 33

A Complete Guide to Programming in C++ part 33. 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. | MEMBER OBJECTS 299 Has-A Relationship Data members belonging to one class can be objects of a different class. In our example the Account class we already made use of this feature. The name of the account holder is stored in a string type data member. An object of the Account class therefore has a string type member sub-object or member object for short. If a class contains a data member whose type is of a different class the relationship between the classes is referred to as a Has-A relationship. Calling Constructors When an object containing member objects is initialized multiple constructor calls are to be executed. One of these is the constructor for the complete object and the others are constructors for the member objects. The order of the constructor calls is significant in this case. First the member objects are created and initialized this then allows the constructor to create the whole object. Unless otherwise stated the default constructor will be called for each member object. The Constructors for the Sample Class Result The example on the opposite page defines a sample class called Result. In addition to a double type measurement the time of each measurement is recorded. For ease of reading the constructors were defined separately rather than as inline. The default constructor only sets the value of the measurement to 0. However initialization is complete since the default constructor is called for the member object time. Example Result current The default constructor for the member object time first sets the hours minutes and seconds to 0. Then the constructor for the Result class is called and a value of is assigned to val. The other constructors can be used to initialize an object explicitly. Example Result temperature1 Current Time Result temperature2 14 30 35 Since the compiler has no information on the relation of initial values and member objects it first calls the default constructor for the member object time. Subsequently the .

TỪ KHÓA LIÊN QUAN
Đã 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.