tailieunhanh - A Complete Guide to Programming in C++ part 34
A Complete Guide to Programming in C++ part 34. 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. | ENUMERATION 309 Definition An enumeration is a user-definable integral type. An enumeration is defined using the enum keyword. A range of values and a name for these values are also defined at the same time. Example enum Shape Line Rectangle Ellipse This statement defines the enumerated type Shape. The names quoted in the list identify integral constants. Their values can be deduced from the list order. The first constant has a value of 0 and each subsequent constant has a value that is one higher than its predecessor. In the previous example Line thus represents a value of 0 Rectangle a value of 1 and Ellipse a value of 2. A Shape type variable can only assume one of these values. Example Shape shape Rectangle Variable shape . switch shape To evaluate shape case Line . etc. However you can also define the values of the constants explicitly. Example enum Bound Lower -100 Upper 100 You can leave out the type name if you only need to define the constants. Example enum off out 0 on in i This statement defines the constants OFF and OUT setting their value to 0 and the constants ON and IN with a value of 1. The values for OFF and ON are implicit. Class-Specific Constants Enumeration can be used to define integral symbolic constants in a simple way. In contrast to define directives which merely replace text strings enum constants are part of a declaration and thus have a valid range. This allows you to define constants that are visible within a namespace or class only. The example on the opposite page shows the enumerated type State which was defined within the Lights class. This means that the type and enum constant are only available for direct use within the class. The enumeration itself is declared as public however and access from outside the class is therefore possible. Example if Lights red . 310 CHAPTER I 5 MEMBER OBJECTS AND STATIC MEMBERS Z L z U L L EXERCISES Copy constructor of class Article The copy constructor creates a copy of an existing .
đang nạp các trang xem trước