tailieunhanh - A Complete Guide to Programming in C++ part 36

A Complete Guide to Programming in C++ part 36. 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. | CLASS ARRAYS 329 Declaring Class Arrays Array elements can also be objects of a class type. The array is known as a class array in this case. When you declare an array of this type you only need to state the type of the array elements. Example Result temperatureTab 24 This statement defines the class array temperatureTab that stores 24 objects of type Result. This class was introduced at the beginning of the last chapter. As the statement does not initialize the array explicitly the default constructor is automatically called for each array element. NOTE Class arrays can only be defined without explicit initialization if a default constructor exists for the class. Thus the previous example is only valid for the first version of the Result class as this class contains a default constructor. Explicit Initialization A class array is initialized as usual by an initialization list. The list contains a constructor call for each array element. Example Result temperatureTab 24 Result 0 30 30 Result At present time Just so Result tempi Copy constructor temp2 Just so The first five array elements are initialized by the constructor calls implicitly contained in these statements. Instead of using a constructor with one argument you can simply supply the argument. The default constructor is then called for the remaining elements. If the size of an array is not stated explicitly the number of values in the initialization list defines the size of the array. The public interface of the objects in the array is available for use as usual. Example temperatureTab 2 .setTime 2 30 21 No additional parentheses are needed in this statement since the subscript operator and the class member operator . are read from left to right although they have the same precedence. 330 CHAPTER 16 ARRAYS MULTIDIMENSIONAL ARRAYS Sample program Demonstrates multidimensional arrays. ------------------------------------------------------- include iostream include iomanip using .

TỪ KHÓA LIÊN QUAN