Sometimes we need to handle limited set of values which can be referred by labels. For example the day of week, year names etc…
1 | i.e. enum week{Mon, Tue, Wed, Thu, Fri, Sat, Sun} this_week; |
This concept in C/C++ is called enumeration. Here this_week is the variable of enum type week.
Enumerated Types
The enumerated type is used in C++ in exactly the same way it was used in ANSI-C with one small exception, the keyword enum is not required to be used again when defining a variable of that type, but it can be used if desired. It may be clearer to you to use the keyword when defining a variable in the same manner that it is required to be used in C, and you may choose to do so.
Examine the file named ENUM.CPP for an example that uses an enumerated type variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // enum.cpp #include <iostream.h> enum game_result {win, lose, tie, cancel}; main() { game_result result; enum game_result omit = cancel; for (result = win;result <= cancel;result++) { if (result == omit) cout << "The game was cancelled\n"; else { cout << "The game was played "; if (result == win) cout << "and we won!"; if (result == lose) cout << "and we lost."; cout << "\n"; } } } // Result of execution // // The game was played and we won! // The game was played and we lost. // The game was played // The game was cancelled |
The example program uses the keyword enum in line 9, but omits it in line 8 to illustrate to you that it is indeed optional. The remainder of this program should be no problem for you to understand. After studying it, be sure to compile and execute it and examine the output.
A Simple Structure
Examine the example program named STRUCTUR.CPP for an illustration using a very simple structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // structure.cpp #include <iostream.h> struct animal { int weight; int feet; }; main() { animal dog1, dog2, chicken; animal cat1; struct animal cat2; dog1.weight = 15; dog2.weight = 37; chicken.weight = 3; dog1.feet = 4; dog2.feet = 4; chicken.feet = 2; cout << "The weight of dog1 is " << dog1.weight << "\n"; cout << "The weight of dog2 is " << dog2.weight << "\n"; cout << "The weight of chicken is " << chicken.weight << "\n"; } // Result of execution // // The weight of dog1 is 15 // The weight of dog2 is 37 // The weight of chicken is 3 |
This structure is no different from that used in ANSI-C except for the fact that the keyword struct is not required to be used again when defining a variable of that type. Lines 11 and 12 illustrate the declaration of variables without the keyword, and line 13 indicates that the keyword struct can be included if desired. It is up to you to choose which style you prefer to use in your C++ programs.
You should take note of the fact that this is a valid ANSI-C program except for the fact that it uses the stream library, the C++ comments, and the lack of use of the keyword struct in two of the lines.
Once again, be sure to compile and execute this program after studying it carefully, because the next example program is very similar but it introduces a brand new construct not available in standard C, the class.
A VERY SIMPLE CLASS
Examine the example program named CLASS1.CPP for our first example of a class in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // class1.cpp #include <iostream.h> class animal { public: int weight; int feet; }; main() { animal dog1, dog2, chicken; animal cat1; class animal cat2; dog1.weight = 15; dog2.weight = 37; chicken.weight = 3; dog1.feet = 4; dog2.feet = 4; chicken.feet = 2; cout << "The weight of dog1 is " << dog1.weight << "\n"; cout << "The weight of dog2 is " << dog2.weight << "\n"; cout << "The weight of chicken is " << chicken.weight << "\n"; } // Result of execution // // The weight of dog1 is 15 // The weight of dog2 is 37 // The weight of chicken is 3 |
This is the first class example, but it will not be the last, since the class is the major reason for using C++ over ANSI-C or some other programming language. You will notice the keyword class used in line 4, in exactly the same way that the keyword struct was used in the last program, and they are in fact very similar constructs. There are definite differences, as we will see, but for the present time we will be concerned more with their similarities.
The word animal in line 4 is the name of the class, and when we declare variables of this type in lines 12 through 14, we can either omit the keyword class or include it if we desire as illustrated in line 14. In the last program, we declared 5 variables of a structure type, but in this program we declare 5 objects. They are called objects because they are of a class type. The differences are subtle, and in this case the differences are negligible, but as we proceed through this tutorial, we will see that the class construct is indeed very important and valuable. The class was introduced here only to give you a glimpse of what is to come later in this tutorial.
The class is a type which can be used to declare objects in much the same way that a structure is a type that can be used to declare variables. Your dog named King is a specific instance of the general class of dogs, and in a similar manner, an object is a specific instance of a class. It would be well to take note of the fact that the class is such a generalized concept that there will be libraries of prewritten classes available in the marketplace soon. You will be able to purchase classes which will perform some generalized operations such as managing stacks, queues, or lists, sorting data, managing windows, etc. This is because of the generality and flexibility of the class construct. In fact, a few class libraries are available now.
The new keyword public in line 5, followed by a colon, is necessary in this case because the variables in a class are defaulted to a private type and we could not access them at all without making them public. Don’t worry about this program yet, we will cover all of this in great detail later in this tutorial.
Be sure to compile and run it to see that it does what we say it does with your compiler. Keep in mind that this is your first example of a class and it illustrates essentially nothing concerning the use of this powerful C++ construct.