|
|
|
Basic
C++ Tutorial
For this first lesson in C++ programming, we will begin
with a tutorial on basic input and output. An input device is
any device that allows a user to put data into a computer,
such as a keyboard or mouse. An output device allows data to
be sent out of the computer, usually via a graphical medium,
like that of a monitor or printer. In this lesson we will
focus on using the keyboard as our input device and the
monitor as our output device. In order to achieve access to
these devices in C++, we need to include a file in our source
code from the standard library, which is packaged with every
C++ compiler. We do this by using the include statement.
NOTE: // indicates a comment. Comments are ignored by the
compiler.
//Source code View
#include
For header files that are a part of C++'s Standard Library,
the filename without the .h extension should be placed within
angled brackets as above. If you create your own header file
or class, or use any other include file that is not part of
the standard library, you must add the file to your working
directory and include the extension .h when referencing in
your source. When including, you must place the filename
between quotation marks. Ex. #include "myClass.h"
We will cover classes in a later tutorial.
Now that we have access to C++'s input and output functions,
we want to be able to use them by their standard names, which
are in the namespace for the standard library, std.
//Source Code View #include
using namespace std;
We are now ready to use the input and output functions. The
two most common are cin and cout.
In order to create our C++ program, we first need a main
function. Main is the function that is ran upon program
execution. We will have it return an integer, 0 if execution
was successful, 1 if there was an error during the program.
The program below will run, but it doesn't do anything.
//Source Code View #include
using namespace std;
int main()
{
return 0;
}
NOTE: All statements in C++ are followed by a semicolon.
The first part of the function's signature is its return type,
int (integer). We then have the function's name followed by
its parameter list in the parentheses. We do not need to pass
any arguments to main, so we could have typed void between the
parentheses.
Within the body of the function is a return statement. You
will notice that all command statements in C++ are followed by
a semicolon. We had the function return 0 because at this
point in the code the program should end. In fact, our program
starts and stops without doing anything but sending a message
to the operating system letting it know that everything went
OK. If you haven't already ran the program, please compile it
and do so.
Now we can display some text in the program using cout, which
stands for "console output." In order to achieve
this, we type cout followed by the << operator and the
string we want as output. In this case, we will be using a
string literal.
//Source Code View
#include
using namespace std;
int main()
{
cout << "My First C++ Program" << endl;
return 0;
}
Endl, at the end of the cout statement, signifies a line
break. You could also use \n within quotation marks for a line
break. See Escape Sequences . It is all a matter of preference
at this point whether you use endl or \n. Run the program!
We can now display text on the monitor, but what about getting
input from the user? Well, that is also simple when using
C++'s cin. Since we are already familiar with the integer data
type, we will use an integer to store input from the keyboard.
First, we need to create a new variable to hold this data. To
do this, type the data type you want to create followed by the
variable name you want to use and a semicolon.
//Source Code View
#include
using namespace std;
int main()
{
int age;
cout << "My First C++ Program" << endl;
return 0;
}
We should also change the instructions for the user.
//Source Code View
#include
using namespace std;
int main()
{
int age;
cout << "My Second C++ Program" << endl;
cout << "Please enter your age in years: ";
return 0;
}
We want to be able to get the users age, so we first prompt
the user. Then we can use cin to pull in the data. We simply
type cin followed by the >> operator and the variable
that we want to store data in. You'll notice that the operator
used for cin is the opposite of that used for cout. Then, we
can print the data back out to the user, using cout. When
printing a variable, just use the variable's name without
quotes.
//Source Code View
#include
using namespace std;
int main()
{
int age;
cout << "My Second C++ Program" << endl;
cout << "Please enter your age in years followed by
: ";
cin >> age;
cout << "\nYou are " << age <<
" years old." << endl;
return 0;
}
Congratulations, you have now created a C++ program that can
get information from a user and display it back to them.
-kolij-
|
|