C++ Object Identifiers
C++ Taking the Bull by the Horns – Part 4
Forward: In this part of the series, we learn how to identifier objects in C++.
By: Chrysanthus Date Published: 21 Aug 2012
Introduction
Recall
The memory of a computer is a long series of cells. Imagine the cells placed next to one another in one long line. A short consecutive group of these cells is called a region. The group should have a minimum of one cell. If this region has been designated to hold datum, such as an int or float or any of the other data types, it is called an object. Such a designated region is called an object whether or not it is actually holding datum. So, an object can consist of one or more consecutive cells. An object can hold a datum. This datum is the value of the object. There are different types of data. In the previous part of the series we talked about the int, float, char, _Bool and void object types. These are the fundamental object types. A derived object type is a combination of two or more of the fundamental types. In this part of the series, we shall not look at the derived object types (except what is called the enum type). We shall look only at the fundamental (single) object types.
The series of cells of the memory are numbered numerically, consecutively. These numbers are called addresses. So each memory cell has a unique memory address.
You will take things in this tutorial the way I give you. Do not try to make analogy with the human language (English). Do not also try to make analogy with mathematic statements. Just take things the way I give you in order not to be misled by human language or mathematics.
Note that I have been talking about object types and not data types. The phrase, data type, is having resemblance in human language and mathematics. The type of datum an object will hold is determined, when the object is created. That is why I prefer to talk about object types instead of data types. The phrase, data types, sounds more arbitrary.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
This is important: Do not add or subtract in your mind, any idea to what I give you in this part of the series and in the rest of the series. In order for you to do that you would have to liken what I give you to the human language or mathematics. I believe doing that would be misleading, and you would not find the study of C++, easy.
If an object is to hold an integer, say 45, you can write that in a statement as follows:
int myInt = 45;
The statement begins with the type of value the object holds. This is followed by a space and then a name that identifies the object. This name is called the identifier. You are the one who decides on which name to give as the identifier. Then you have the = symbol. Here the = symbol is called the assignment operator. Do not call it the equal sign as you do in mathematics; that would be misleading. Then you have the value of the integer. Finally you have the semicolon, which makes the line a statement. Now, we say the value 45 is assigned to the integer object, identified by myInt.
Try the following code:
#include <iostream>
using namespace std;
int main()
{
int myInt = 45;
cout << myInt;
return 0;
}
You should see 45 displayed in the command prompt window. I will explain just the first two lines inside the block (curly braces).
The first statement is,
int myInt = 45;
The explanation was given previously. The second statement (ending with a semicolon) is:
cout << myInt;
This is the predefined cout object that sends the output to the command prompt window. What you have inside the parentheses are called arguments. Note that in the second statement the identifier, myInt is not in quotes, because it is the value of the object identified by myInt that cout obtains (from myInt) and prints at the monitor.
If an object is to hold a float, say 56.74, you can write that in a statement as follows:
float myFloat = 56.74;
The statement begins with the type of value the object holds; the object holds a float. This is followed by a space and then a name that identifies the object. This name is called the identifier. You are the one who decides on which name to give as the identifier. Then you have the assignment operator. Do not call it the equal sign as you do in mathematics; that would be misleading. Then you have the value of the float. Finally you have the semicolon, which makes the line a statement. We say the value 56.74 is assigned to the float object, identified by myFloat.
Try the following code:
#include <iostream>
using namespace std;
int main()
{
float myFloat = 56.74;
cout << myFloat;
return 0;
}
The output should be 56.74.
The first statement in the block should be self-explanatory, as it is similar to the first statement of the previous block. He second statement displays the value held by the object identified by myFloat.
Now, do not call identifiers variables, as you would do in mathematics; that would be misleading. Some authors call identifiers, variables because they are likening C++ to human language and mathematics. I am likening C++ to machine language, and that is why I am using the vocabulary of the inventors (scientists), since C++ is closer to the machine language than the human language and mathematics; I believe, this is not misleading.
The char Identifier
If an object is to hold a char, say 'W', you can write that in a statement as follows:
char myChar = 'W';
The statement begins with the type of value the object holds; the object holds a char. This is followed by a space and then the identifier of the object. Then you have the assignment operator. Then you have the value of the character (char). Finally you have the semicolon, which makes the line a statement. We say the value 'W' is assigned to the char object, identified by myChar.
With the char object, the value assigned has to be in single quotes as with 'W'. Double quotes is used for strings (see later).
Try the following code:
#include <iostream>
using namespace std;
int main()
{
char myChar = 'W';
cout << myChar;
return 0;
}
The value, ‘W’ should be printed at the command prompt window, without the quotes. The first and second statements in the block should be self-explanatory.
In the above examples the identifiers are said to be defined and initialized at the same time. We shall now look at the meaning of defining identifiers and initializing objects.
An identifier identifiers an object. An object is a region in memory. Defining an identifier means deciding what type of datum (value) the object will hold. The following statement defines an identifier to be of type, int:
int hisInt;
When the program is being executed, and it sees the above statement, a number of consecutive memory cells are allocated somewhere in memory. This group of cells form an object and will have to hold only an integer, not a float or char or some other value. This object is identified by the identifier, hisInt. It can only hold an integer because of the preceding word, int. At the moment it is holding nothing. Above is an independent statement that ends with a semicolon.
You can define float and char identifiers in a similar way. The following identifier (herFloat) identifiers an object that can only hold a float:
float herFloat;
The following identifier (theChar) identifiers an object that can only hold a character (char):
char theChar;
Identifiers are defined this way and the syntax is:
Type ident;
where ident means identifier, and Type stands for int, or float, or char, etc.
You can define an identifier and then assign a value to the identifier’s object, later in the program. The following code segment illustrates this:
int myInt;
myInt = 45;
The above code segment has two statements. The first one defines the identifier, myInt. So, if the object identified by myInt will have any value, it will be an integer, and nothing else. The second statement assigns the integer value, 45 to the object identified by myInt. That is, the second statement gives the object a value (of type int). In other words, the second statement makes the value of 45 to be kept in the region (group of cells) identified by myInt.
Note that in the second statement, you do not have the preceding, int. The preceding int or type only occurs during definition (the first statement). After definition, you just use the identifier for assignment, without the preceding type.
Read and try the following code:
#include <iostream>
using namespace std;
int main()
{
char herChar;
herChar = 'q';
cout << herChar;
return 0;
}
You should have the character, q at the output.
Note: After definition, you use the identifier without the preceding type, as we have done in the second and third statements in the block above.
You can define more than one identifier in one statement. Read and try the following code that illustrates this:
#include <iostream>
using namespace std;
int main()
{
int myInt, yourInt, hisInt;
yourInt = 702;
cout << yourInt;
return 0;
}
Your result should be 702. Three identifiers are defined in one statement. Only one is used. That is allowed. Nothing stops you from using two or all three for different things (assignments).
Initialization
When you define an identifier and assign a value at the same time, that is initialization. That is what we did in the top part of this tutorial. After initialization, you use the identifier without the preceding type. Read and try the following code:
#include <iostream>
using namespace std;
int main()
{
float theFloat = 25.63;
cout << theFloat;
return 0;
}
Your output should be 25.63.
Note that in the second statement of the block, we have used the identifier without the preceding type (float). That is, after initialization or definition of the object, you use the identifier without the preceding type.
Changing the Value of an Object
Whether you start an object by definition and later on assign a value to it or you start by initialization where the value is assigned at the definition stage, the value (content) of the object can be changed, later in the code. Read and try the following that illustrates this:
#include <iostream>
using namespace std;
int main()
{
int myInt = 99;
myInt = 88;
cout << myInt;
return 0;
}
Your output should be 88, the changed value for the object identified by myInt. Remember, after definition or initialization, you do not need to precede the identifier with the Type indicator, even when you are changing the value.
Constant
An identifier identifies an object. In other words, an identifier identifies a region in memory. In an initialization process, an identifier would identify a region in memory, which has an value. During initialization, if you want to make the content (value) of the region un-modifiable (constant), then you would have to precede the initialization statement with the word const; something like:
const int myInt = 55;
Under this condition, the value 55, which is in the region of memory identified by myInt, can never be changed. Let us try to change it in the following code, which you should type in your text editor first:
#include <iostream>
using namespace std;
int main()
{
const int myInt = 55;
myInt = 40;
cout << myInt;
return 0;
}
Save the document as a file, with the name, temp.cpp in the working directory. Open your command prompt window. Go to the working directory, C:MinGW>. Compile the file by typing the following command at the working directory DOS prompt, and pressing the Enter Key, then note that you will have an error message:
g++ temp.cpp -o temp.exe
You should have an error message as a result, on the command prompt window. Do not worry for now about the exact meaning of the error message. The error message comes as a result of the fact that the first statement in the block (curly braces) indicates that the content of the object whose identifier is myInt, can never be changed. The second statement in the block tries to change this; that is why you have an error.
Now, do not liken the constant feature here to constant in mathematics; that would be misleading. In mathematics, you can have an equation such as “x=K”. Here K is the y-intercept and it is called a constant. There are two things to note here when comparing the mathematical constant and the constant we have seen above. In the mathematical equation, = is called the equal sign, but in programming it is called the assignment operator, (an operator here means something that will cause an action), which is to put value in a region of memory. The other thing to note is that the word, const, does not precede the mathematical equation.
As you can see, trying to make analogy between C++ statements and mathematics and human language can be misleading. Take everything in this tutorial as I give you. Do not add or subtract anything in your mind; do not make any analogy with mathematics and human language (English).
It is possible for an object to hold the memory address of some other object, instead of holding a fundamental value such as 45, or 56.74 or ‘W’. Even though the memory address is a number, the object holding the address is not considered as a fundamental object type. Here we are talking about two objects, one that holds a value, e.g. 45 or 56.74 or ‘W’; the other object holds the address to the object that holds the value. The later object is called a pointer, because it points to another object by holding its address. We shall see more about pointers, including the identifier in the next part of the series.
The enum Identifier
enum means enumeration. The enum object type is known as a derived type. It is derived from integers. Let us look at a code sample containing an emun object before I explain. Just read and try the following code first:
#include <iostream>
using namespace std;
int main()
{
enum numbers {today, tomorrow, afterTomorow, theDayAfter};
cout << today; cout << "\n";
cout << tomorrow; cout << "\n";
cout << afterTomorow; cout << "\n";
cout << theDayAfter; cout << "\n";
return 0;
}
The output should display the following integers:
0
1
2
3
An enum object is a region in memory that has a range of integers. These integers are themselves, objects. Let us not worry how these integers are placed in the enum object in memory. Let us just know that an enum object is an object consisting of a range of integers. In the enum object construct, you have identifiers for the integers and not the integers themselves. So the enum object itself, has an identifier and the integers of the range that make up the object, have identifiers. In simple terms, when an enum object identifier is defined its integer identifiers for the integer objects, are implicitly assigned values (integers). In simple terms the syntax to define an enum object and implicitly have its integer identifier objects assigned values, is:
enum enumIdent {intIdent1, intIdent2, intIdent3, . . . }
It begins with the word, enum, then a space, then you have the identifier for the enum object itself. Then you have an optional space; then a block, obviously delimited by braces (curly brackets). Inside the block, you have identifiers for int objects. These identifiers for the int objects are not preceded by the word int, because by the enum object definition, they identify int objects. By default, the value of the first int object is zero; that of the second is 1; that of the third is 2, that of the fourth is 3, and so on. Note that the counting begins from zero and not 1. This numbering scheme can be changed, but I will not go into that in this basic tutorial.
In the above code sample, the identifier for the enum object is, enumIdent. The identifier for the integer value, zero is, today; the identifier for the integer value, 1 is, tomorrow; the identifier for the integer value, 2 is, afterTomorow; the identifier for the integer value, 3 is, theDayAfter. It is you who decides on what name to give an identifier, whether it is the identifier for an enum object, or identifier for an int object inside the enum block or for some other object.
There are four cout statements above for the four integers in the sample code. Each cout statement uses the variable of an integer in the enum set to print the integer. Each line for the cout statement actually has two cout statements. Remember, a statement ends with a semicolon. Do not confuse between a line and a statement.
The second statement in each line prints an un-displayed character, ‘n’. This is a character, even though it is made up of two symbols, and n. The n character is not displayed, but it causes what is to be displayed next in the command prompt window, to be displayed on the next line. That is why you have the numbers, 0, 1, 2, and 3, displayed in separate lines.
Allocation of Memory Size for Fundamental Objects
When a fundamental object, such as the int or float is defined, a particular region of memory is allocated for that object. This is done even if no value (content) is assigned to the object as in initialization. In each computer, all ints have the same size; all floats have the same size; all _Bools have the same size; and all chars have the same size. Of course, the sizes of the different fundamental types are different, for example the size of a char is different from that of a float. For each object type, the size may differ between computers.
Case Sensitivity
C++ is said to be case sensitive. This means for example that the identifier, myInt is not the same as MyInt or MYINT or myint and so on.
Well, we have done a lot. We should take a break here. We continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT