tailieunhanh - A Complete Guide to Programming in C++ part 31
A Complete Guide to Programming in C++ part 31. 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. | STANDARD METHODS 279 Every class automatically contains four standard methods the default constructor the destructor the copy constructor and the assignment. You can use your own definitions to replace these standard methods. As illustrated by the sample class Account the compiler only uses the pre-defined default constructor if no other constructor is available. The default constructor and the implicit minimal version of a destructor were introduced earlier. Copy Constructor The copy constructor initializes an object with another object of the same type. It is called automatically when a second already existing object is used to initialize an object. Example Account myAccount Li Ed 2345 Account yourAccount myAccount In this example the object yourAccount is initialized by calling the copy constructor with the myAccount object. Each member is copied individually that is the following initialization process takes place The copy constructor is also called when an object is passed to a function by value. When the function is called the parameter is created and initialized with the object used as an argument. Assignment Assignment has been used in several previous examples. An object can be assigned to another object of the same type. Example hisAccount yourAccount The data members of the yourAccount object are copied to the corresponding members of hisAccount in this case also. In contrast to initialization using the copy constructor assignment requires two existing objects. Later in the book you will be introduced to situations where you need to define the copy constructor or an assignment yourself and the necessary techniques will be discussed. 280 CHAPTER 14 METHODS this POINTER Sample class DayTime The class DayTime represents the time in hours minutes and seconds. ----------------------------------------------------- ifndef _DAYTIME_ define _DAYTIME_ .
đang nạp các trang xem trước