Monday, April 27, 2020

Throwback - Compound Types

Migrating Content from even an older blog.
The following content is from my even older blog which had to be removed due to discontinuation of the platform. I am posting it here as a new content for this blog.

ENUMERATED TYPES

Examine the file named ENUM.CPP for an example that uses an enumerated type variable. 
// ENUM.CPP
#include <iostream.h>

enum game_result {WIN, LOSE, TIE, CANCEL};

void main()
{
                game_result result;
                enum game_result omit = CANCEL;
                int index;

                for (index = WIN ; index <= CANCEL ; index++)
                {
                                result = (game_result)index;
                                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 numerated type is used in C++ in a very similar way that it was used in ANSI-C.  The keyword enum is not required to be used again when defining a variable of that type, but it can be used if desired.  The name game_result is defined as an enumerated type making the use of the keyword enum optional.  However, it may be clearer for 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.

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, but that is a trivial difference.  There is a bigger difference in the way an enumerated type is used in C++.  In C, the enumerated type is simply an int type variable, but in C++ it is not an int, but its own type.  Mathematical operations can not be performed on it, nor can an integer be assigned to it.  It cannot be incremented or decremented as it can be in C.  In the example program, an integer is used for the for loop because it can be incremented, then the value of the loop index is assigned to the enumerated variable by using a cast.  The cast is required or a compile error is reported.  The mathematical operations and the increment and decrement operators can be defined for the enumerated type, but they are not automatically available.  Operator overloading will be studied later, and the last sentence will make much more sense at that time.

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.  
// STRUCTUR.CPP
#include <iostream.h>

struct animal
{
                int weight;
                int feet;
};

void 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++.
// CLASS1.CPP
#include <iostream.h>

class animal
{
                public:
                                int weight;
                                int feet;
};

void 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 desired 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 are libraries of prewritten classes available in the marketplace.  You can purchase classes which 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.

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 this example program 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.


THE FREE UNION OF C++

Examine the program named UNIONEX.CPP for an example of a free union. 
// UNIONEX.CPP
#include <iostream.h>

struct aircraft
{
                int wingspan;
                int passengers;
                union
                {
                                float fuel_load;     // for fighters
                                float bomb_load;     // for bombers
                                int pallets;         // for transports
                };
} fighter, bomber, transport;

void main()
{
                fighter.wingspan = 40;
                fighter.passengers = 1;
                fighter.fuel_load = 12000.0;
  
                bomber.wingspan = 90;
                bomber.passengers = 12;
                bomber.bomb_load = 14000.0;
  
                transport.wingspan = 106;
                transport.passengers = 4;
                transport.pallets = 42;
  
                transport.fuel_load = 18000.0;
                fighter.pallets = 4;
  
                cout << "The fighter carries "
                              << fighter.pallets << " pallets.\n";
                cout << "The bomber bomb load is " << bomber.bomb_load << "\n";
  
}




// Result of execution
//
// The fighter carries 4 pallets.
// The bomber bomb load is 14000

In ANSI-C, all unions must be named in order to be used, but this is not true in C++.  When using C++ we can use a free union, a union without a name.  The union is embedded within a simple structure and you will notice that there is not a variable name following the declaration of the union in line 11.  In ANSI-C, we would have to name the union and give a triple name (three names dotted together) to access the members.  Since it is a free union, there is no union name, and the variables are accessed with only a doubly dotted name as illustrated in lines 18, 22, 26, and others.

You will recall that a union causes all the data contained within the union to be stored in the same physical memory locations, such that only one variable is actually available at a time.  This is exactly what is happening here.  The variable named fuel_load, bomb_load, and pallets are stored in the same physical memory locations and it is up to the programmer to keep track of which variable is stored there at any given time.  You will notice that the transport is assigned a value for pallets in line 26, then a value for fuel_load in line 28.  When the value for fuel_load is assigned, the value for pallets is corrupted and is no longer available since it was stored where fuel_load is currently stored.  The observant student will notice that this is exactly the way the union is used in ANSI-C except for the way components are named.

The remainder of the program should be easy for you to understand, so after you study and understand it, compile and execute it.


C++ TYPE CONVERSIONS

Examine the program named TYPECONV.CPP for a few examples of type conversions in C++. 
// TYPECONV.CPP
#include <iostream.h>

void main()
{
                int a = 2;
                float x = 17.1, y = 8.95, z;
                char c;

                c = (char)a + (char)x;
                c = (char)(a + (int)x);
                c = (char)(a + x);
                c = a + x;

                z = (float)((int)x * (int)y);
                z = (float)((int)(x * y));
                z = x * y;

                c = char(a) + char(x);
                c = char(a + int(x));
                c = char(a + x);
                c = a + x;

                z = float(int(x) * int(y));
                z = float(int(x * y));
                z = x * y;
}




// Result of execution
//
// (There is no output from this program)


The type conversions are done in C++ in exactly the same manner as they are done in ANSI-C,  but C++ gives you another form for doing the conversions. Lines 10 through 17 of this program use the familiar "cast" form of type conversions used in ANSI-C, and there is nothing new to the experienced C programmer.  You will notice that lines 10 through 13 are actually all identical to each other.  The only difference is that we are coercing the compiler to do the indicated type conversions prior to doing the addition and the assignment in some of the statements.  In line 13, the int type variable will be converted to type float prior to the addition, then the resulting float will be converted to type char prior to being assigned to the variable c.

Additional examples of type coercion are given in lines 15 through 17 and all three of these lines are essentially the same.

The examples given in lines 19 through 26 are unique to C++ and are not valid in ANSI-C.  In these lines the type coercions are written as though they are function calls instead of the more familiar "cast" method as illustrated earlier.  Lines 19 through 26 are identical to lines 10 through 17.

You may find this method of type coercion to be clearer and easier to understand than the "cast" method and in C++ you are free to use either, or to mix them if you so desire, but your code could be very difficult to read if you indescriminantly mix them.

Be sure to compile and execute this example program.

No comments:

Post a Comment