In C++ most data is stored in objects. An object is something with a type and a status, and, optionally, a name.
The type of an object is its kind. For example, the object ``Gandalf'' might be of type ``Wizard''3.1. Or the object ``Pi'' might be of type ``Number'', and so on.
The status of an object depends on its type. The status of the above mentioned object ``Pi'' might be its value (3.1415926...). The status of an object of type ``Wizard'' might be a lot more complicated; it might include the wizard's name, his current position, what he is doing, his mood, whether he is dead or alive, and so on. You will often hear an object's status referred to as ``value'' if the object is simple, like for example a number.
An object might have a name, but it will not necessarily have one. For example, an object of type ``Wizard'' might have the name ``Gandalf''. But there is no relationship at all between an object's status and its name; it would be very well possible having an object of the type ``Wizard'' containing data about Gandalf but with the name Sauron (or Frodo, or Bilbo, or whatever you prefer)3.2.
In C++ there are several different types of objects, and it is possible to create as many new ones as you like. For example, there are lots of different types dealing with numbers (because computers are designed to work with numbers), but there isn't even one to deal with wizards. To overcome that horrible lack, we'll design one further on.
To create an object you must first declare it, specifying its type, its name, and optionally its initial value. This is done as follows:
int number_of_wizards;where
int and number_of_oranges are the type and the
name for the new object, respectively. Of course, you could use any
other type instead of int and any other name instead of
number_of_wizards as long as they are both valid.
If you want to give the newly created element a new value while you create it, you can use the following syntax:
int number_of_wizards = 1;Giving an object an initial value during its declaration is called initializing.
Not all names you can think of are valid in C++. A valid name may be
composed only by alphanumeric character (a-z,
A-Z and 0-9), or the underline character
``_''. The first character may not be a numeric character,
though. Thus, the following names are valid:
a a1 s0 hello_world GandalfRulezwhile the following ones are not:
a b c 9oclock number-of-wizardsThe first one of them is invalid because it contains whitespace characters; the second one starts by a number, and the third one contains hyphens which are not allowed either.
There are some names which are allowed but which you shouldn't use
either. All names containing a sequence of two underline characters at
any position (__) are reserved for the implementation, and so
are any names starting by an underline character followed by an
upper-case letter. You can use them, but you should not. Avoid
problems and don't.
The most important3.3 types in C++ are
int, double and std::string.
int can hold both positive and negative integers (like 1, 2,
3...). It is the type you should generally use for integer values.
double can hold floating-point (decimal) numbers (like 1.32 or
-.3). Because of some problems with binary rounding3.4 double
should be used only if you have to use fractional values. If
anyhow possible, use int!
std::string is fundamentally different from int and
double, not only because it holds strings (text) and not
numbers, but because it is not a builtin type. ``Not a builtin type''
means that it is not a type which belongs to the language, but, like
for cout, a special file must be included if you want to use
it. That file is string, and, as for cout, you must
either use using namespace std; or put a std:: in front
of string before using it. I strongly recommend the first
option for the time being.
Now that you know everything about the declaration of objects3.5, it is about time to start doing things with (and to) them.
The first thing to do is assignment. It works like this:
number_of_wizards = 4;Basically it's the same as initializing, but with an object which already exists (while in initialization you give it a value while it is created). Of course, the two objects must be of the same type, or the compiler must know how to convert between them. (More on that later.) Have a look at the following full example:
int main() {
int a = 12; // a is now 12 (initialization)
a = 32; // a is now 32 (assignment)
a = a + 1; // and now 33 (assignment)
a = a / 2; // and now 16 (assignment)
}
First a is declared and initialized with the value of 12. Then
it is assigned the value 32. After that it is assigned its own
value plus one, thus 33. And then it is assigned its own value divided
by two, thus...16? But why not 16.5? The answer is simple. a
is an int. ints hold integer values only. If an
int is assigned a fractional value, it is rounded towards 0.
It is of course nice to have a program calculate things for you; it would be a lot nicer, though, if the values could be entered when the program is run and not hard-coded into the program. Doing that is quite easy; in fact, we've already done it (in part) in the ``Hello World'' program, which could print text on the screen.
To send data to standard output (generally the screen) in C++ you use
cout, as follows:
// This program demonstrates how to use cout
#include <iostream>
using namespace std;
int main() {
cout << "Hello ";
cout << "World.\n";
cout << "Hello World.\n";
int a = 5;
cout << "a = ";
cout << a;
cout << "\n";
int b = 23;
cout << "b = " << b << "\n";
}
As you can see, you can use cout to print text, ints,
and almost anything else you want. Notice how you can send more than
one object to output at the same time using multiple << signs,
and that cout does not begin a new line by default. You
must use \n for that. As cout is defined in
iostream we have to include it.
The // in the first line is a comment. Comments are
ignored by the compiler and used to write reminders to yourself into a
program. C++ has two different types of comments. The ones used in
this example are the ``C++-style'' comments, begin by // and
end at the end of the line. The other ones are ``C-style'' comments
and begin by /* and end by */. They can thus span
multiple lines.
Input works just like output: use cin instead of cout
and >> instead of << and you're done. Or almost. Look at
this simple example:
#include <iostream>
using namespace std;
int main() {
int year, age;
cout << "What year were you born? ";
cin >> year;
age = 2002 - year;
cout << "You are " << age << " years old.\n";
}
This program takes the year of your birth and calculates your age. You
might need to update the program if it is outdated in the moment you
read it. Notice that also cin is defined in iostream.
There are a few peculiarities of cin though. Look at the
following program:
// This program shows how cin only reads until the
// first whitespace character.
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter your name: ";
string name;
cin >> name;
cout << "Your name is " << name << "\n";
}
First of all, notice how we used the string type to read in
text strings. We need to include string to do so.
If you enter Joe when it asks you for your name it will nicely
display Joe as your name, just as it should. But if you enter
Joe Programmer it will display Joe again, just dropping
the Programmer part! Why that?
The answer is simple: cin only reads until the first whitespace
character, then it stops. This might seem absurd but it isn't as you
will see later. This is fine if you want to read in a number, because
it will not contain any whitespace; it might be a problem with strings
though. To read in a full line, you use getline like this:
// This program shows how getline works.
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter your name: ";
string name;
getline(cin, name);
cout << "Your name is " << name << "\n";
}
Of course, getline can be used with string only, as it
wouldn't make any sense with other data types.