DEFINITION and DECLARATION

The words definition and declaration refer to two different things in

ANSI-C and C++. A declaration provides information to the compiler

about the characteristics of something such as a type or a function but it

does not actually define any code to be used in the executable program,

and you are permitted to make as many declarations of the same entity

as you desire. A definition, on the other hand, actually defines

something that will exist in the executable program, either some useful

variables, or some executable code, and you are required to have one

and only one definition of each entity in the program. In short, a

declaration introduces a name into the program and a definition

introduces some code.

If we declare a struct, we are only declaring a pattern to tell the

compiler how to store data later when we define one or more variables

of that type. But when we define some variables of that type, we are

actually declaring their names for use by the compiler, and defining a

storage location to store the values of the variables. Therefore, when

we define a variable, we are actually declaring it and defining it at the

same time.

We will refer to these definitions many times throughout the course of

this tutorial.

 

A better FOR loop

The for loop defined in line 13 is a little clearer than the for loop in

ANSI-C, because the loop index is defined in the for loop itself. The

scope of this loop index is from its declaration to the end of the

enclosing block. In this case its scope extends to line 17 since the

closing brace in line 17 corresponds to the most recent opening brace

prior to the declaration of the variable. Since the variable is still

available, it can be used for another loop index or for any other purpose

which an integer type variable can legally be used for. The variable

named count2 is declared and initialised during each pass through the

loop because it is declared within the block controlled by the for loop.

Its scope is only the extent of the loop so that it is automatically

deallocated each time the loop is completed. It is therefore declared,

initialised, used and deallocated five times, once for each pass through

the loop.

You will notice that the variable count2 is assigned a numerical value in

line 15 but when it is printed out, a character value is actually output.

This is because C++ (version 2.0 and later) is careful to use the correct

type.

Finally, as mentioned earlier, the static variable named goofy is declared

and automatically initialised to zero in line 18. Its scope is from the

point of its declaration to the end of the block in which it is declared,

line 20.

 

Operator precedence

Operator precedence is identical to that defined for ANSI-C, but there

is a small difference when some operators are overloaded which we will

learn to do later in this Module. Some of the operators act slightly

different when overloaded than the way they operate with elements of

the predefined language.

Examine the next program [ENUM.CPP] for an example of the

enumerated type variable. The enumerated type is used in C++ in

exactly the same way it is 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.

#include <iostream.h>

enum game_result {win, lose, tie, cancel};

void 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";

}

}

}

The example program uses the keyword enum in line 6, but omits it in

line 5 to show that it is indeed optional.

 

A Simple Structure

The next program [STRUCTUR.CPP] illustrates how to use a very

simple structure. 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.

#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";

}

Lines 8 and 9 illustrate the declaration of variables without the

keyword, and line 10 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 absence of the keyword struct in two of the lines. Compile and

execute this program after studying it carefully, because the next

example program is very similar but introduces a construct not available

in standard C, the class.

 

A Simple Class

Our first example of a class in C++ is to be found in the next program

[CLASS1.CPP]. The class is the major reason for using C++ over

ANSI-C (or perhaps some other programming language).

#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";

}

You will notice the keyword class is used in line 2, 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 2 is the name of the class, and when we declare

variables of this type in lines 9 to 11, we can either omit the keyword

class or include it if we desire as illustrated in line 11. 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 we will see that the class

construct is important and valuable. The class was introduced here only

to give you a glimpse of what is to come later.

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 generalised

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 generalised 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 3, followed by a colon, is necessary in

this case because the variables in a class default to a private type and we

could not access them at all without making them public.

 

Free UNION

The next program [UNIONEX.CPP] is example of a free union. In

ANSI-C, all unions must be named in order to be used, but this is not so

in C++.

#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";

}

In C++ we can use a free union, which is 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 9. 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.

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 21, then a value for fuel_load in line 22. 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.

 

C++ Type Conversions

Examine the next program [TYPECONV.CPP] for a few examples of

type conversions in C++. The type conversions are done in C++ in

exactly the same manner as they are done in ANSI-C, but C++ gives

you another way of doing the conversions.

#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 * (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) * int(y));

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

z = x * y;

}

Lines 8 to 15 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 8 to 11 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

10 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 11 to 14 and all four of these lines are essentially the

same.

The examples given in lines 15 to 18 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 ‘cast method illustrated earlier.

Lines 15 to 18 are identical to lines 7 to 10.

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 will be very difficult to

read if you mix them indiscriminately.

 

Dynamic Allocation, and Pointers.

 

Links:  Home C Programming Guide  C++ programming Guide

Contact Me