The actual third set of instructions are given
to the computer by you, the programmer, using one or more of the
languages that the operating system you are planning to use can
understand. Your job is going to consist of writing applications. As a
programmer, you write statements such as telling the computer, actually
the operating system, that "If the user clicks this, do the following,
but if he clicks that, do something else. If the user right clicks,
display this; if he double-clicks that, do that." To write these
instructions, called programs, you first learn to "speak" one of the
languages of the OS. Then, you become more creative... Some of the
application programs in the market are Microsoft Word, Lotus ScreenCam,
Adobe Acrobat, Jasc Paint Shop Pro, etc.
|
|
The last instructions are given by whoever uses
your program, or your application. For example, if you had programmed
Microsoft Word, you would have told the computer that "If a user clicks
the New button on the Standard toolbar, I want you to display a new
empty document. But if the user clicks File -> New..., I want you to
'call' the New dialog and provide more options to create a new document.
If the same user right-clicks on any button on any of the toolbars, I
want you to show, from a popup menu, all the toolbars available so she
can choose which one she wants. But if she right-clicks on the main
document, here is another menu I want you to display."
At this time, you have probably realized that
the users of your programs depend on your techniques as a developer to
provide an easy to use application (that's what recruiters and employers
call experience and creativity). You depend on the computer language
that you are actually using (every computer language has its ups and
downs). Your computer language depends on the operating system it is
running on (different
|
operating systems have different strengths
and weaknesses). The operating system depends on the microprocessor or
the machine it is running in (the biggest difference between two
microprocessors is the speeds at which each processes information).
Your interest here is on the computer languages, since you are going to write programs. There are various computer languages, for different reasons, capable of doing different things. Fortunately, the computer can distinguish between different languages and perform accordingly. These instructions are given by the programmer who is using compilers, interpreters, etc, to write programs. Examples of those languages are Basic, C++, Pascal, etc. |
Introduction to Header Files
|
C++ is a huge language so much that it uses
various sets of instructions from different parts to do its work. Some
of these instructions come
in computer files that you simply "put" in your program. These
instructions or files are
also called libraries. To make your job easier, some of these
libraries have already been written for you so that as you include them
in your program, you already have a good foundation to continue your
construction. Yet, some of these libraries have their limitations, which
means you will expand them by writing or including your own libraries.
As noted already, there are libraries
previously written for you. One of them asks the computer to receive
keyboard strokes from you the user (when you press a key) and another
asks the machine (the computer performing some operations) to give back a
result. The libraries are files that you place at the beginning of your
program as if you were telling the computer to receive its preliminary
instructions from another program before expanding on yours. The
libraries are
(also) called header files and, as computer files, they have the
extension ".h". An example would be house.h, or person.h. As you see,
they could have any name; when you start creating your own libraries,
you will give your files
custom and recognizable names.
The first library we will be interested in is called iostream. It asks the computer to display stuff on the monitor's
screen.
To see how to put a library in your program, you put it at the beginning of the file. Here is an example:
iostream.h
To use a library in your program, you simply include it by using the word "include" before the name of the library, as follows:
include iostream.h
Since this is a computer language, the
computer will follow particular instructions to perform appropriately,
which will make this language distinct from the everyday languages. C++
has some words it treats specially and some that will completely depend
on you the programmer. For example, the word "include" could be a
special word used by C++ or a regular you want to use in your program.
In this particular situation, if you want the computer to "know" that
the word "include" means, "I want to include the following library", you
will have to append a special sign to it. The pound sign "#" will do
just that. Therefore, to include a library, you precede the include word
with the # sign.
Here is an example:
#include iostream.h
There are usually two kinds of libraries or
files you will use in your programs: libraries that came with C++, and
those that you write. To include your own library, you would enclose it
between double quotes, like this
#include "books.h"
When you include a library that came with C++, you enclose it between < and > as follows:
#include <iostream.h>
Following this same technique, you can add as
many libraries as you see fit. Before adding a file, you will need to
know what that file is and why you need it. This will mostly depend on
your application. For example, you can include a library called stdio
like this:
#include <iostream.h> #include <stdio.h> |
Introduction to Namespaces
|
A namespace is a section of code, delimited and referred to using a
specific name. A namespace is created to set apart a portion of code
with the goal to reduce, otherwise eliminate, confusion. This is done by
giving a common name to that portion of code so that, when referring to
it, only entities that are part of that section would be referred to.
Because C++ is so huge, its libraries are created in
different namespaces, each with a particular name. To use an existing
namespace in your program, you must know its name. To use such a
namespace, you can type the using namespace expression followed by the
name of the namespace and a semi-colon. For example, to use a namespace
called django, you would type:
using namespace django;
One of the namespaces used in C++ is called std.
Therefore, to use it, you can type:
using namespace std;
After typing this, any part of the namespace becomes
available to you. The iostream library we mentioned above is part of the
std namespace. When you use it, you don't need to include the extended of
the iostream file. For this reason, you can start your program with:
#include <iostream> using namespace std; |
C++ Projects
|
C++ Instructions
|
C++ works by giving (separate) instructions to the computer. These instructions can be treated as assignments.
On this site, such an assignment will be called a function. The primary function used in C++ is called
main. To distinguish a function from the other types of
things you will be using in your programs, a function's name is followed
by an opening and a closing parentheses. For example, the main function
will always be written at least as
main(). When we perform a better study of functions, we will learn more about
functions, their parentheses, and other related issues.
When a program is written and you ask the computer to "execute" it, the first thing to look for is the
main() function. This means that every C++ program should have the
main() function. Because a function is an assignment, in
order to perform its job, a function has a body; this is where the
behavior (assignment) of the function would be "described". The body of
a function starts with an opening curly bracket "{" and closes with a
closing curly bracket "}". Everything in between belongs to, or is part
of, the function. Therefore, the
main() function can be written as:
|
main() {}
As we learned that we should (must) always include the libraries that we would need, our program now would include
main(). Whenever you create a program, it is important to isolate any inclusion of a library on its own line. Here is an example:
|
#include <iostream> using namespace std; main(){}
C++ is the computer language we are going to
study to write programs. C++ is a very universal language, it can be
used to write programs for Linux, MS Windows, Macintosh, BeOS, Unix,
etc. C++ is very powerful and can be used to create other compilers or
languages, it can also be used to write an operating system. This means
that you can use C++ to create/write your own computer language. You can
also use C++ to create/write your own compiler; this means that, using
C++, you can create your own implementation of C++, Pascal, Basic, Perl,
or any other existing or non-existing language.
|
There are many products you can use to create a
program in C++. Before a program is made available, it is called a
project because you are working on it. Although in the beginning you
will usually be working alone, most programs involve a lot of people.
That is why during the development of a program or software product, it
is called a project. Each one of the available environments provides its
own
technique(s) of creating a C++ program or working on a C++
project. Therefore, the person who, or the company that, made the
environment available to you must tell you how to use that environment
(it is neither your responsibility, nor the C++ Standard’s job to tell
you how to create a program or how to start a project). I will try to
cover those that I know.
The programs we will be creating on this site
are called console applications. They can also be called Bash programs
(especially on Unix/Linux). The technique you follow to create a project
depends on the environment you are using.
|
Executing a Program
|
To see what your program does, you need to
realize that the lines we have typed are English language instructions
asking C++ to perform the main() function. Unfortunately, the computer
doesn't understand what all of this means (to a certain extent). The
computer has its own language known as the machine language. So, we need
to translate it in a language the computer can understand. A program
was created to that effect and supplied to you with C++. This is what we
call a compiler.
In the past, a program used to be created from
various parts all over the computer, some of the techniques are still
used to "debug" a program to isolate problems or "bugs". Since this is a
small program, we will just ask the computer to "execute" it and see
the result. Throughout this site, the words (or verbs) "execute" and
"run" will be used interchangeably to mean the same thing.
The C++ language doesn't define how to create a
project. When you buy or acquire a c++ compiler, its documentation should
tell you how to create and execute a project. We describe here how how
to create a project in most familiar environments. If you have an
environment or compiler that is not in our list, consult its documentation
to know how to use it.
|
One of our most valuable goals in writing a
site is to avoid including in a program an issue that has not previously
been addressed or explained. This
site is written as a (general) reference towards the C++ language.
To learn C++, you need a C++ compiler, and we
describe how to create a C++ project with some of the most
commonly used compilers or programming environments. As it happens, and
as you may have noticed, different companies (and different individuals
for that matter) choose to implement a language as they see
fit.
Depending on the programming environment you are using, even
depending on how you create your program (for example KDevelop, Borland
C++ Builder, and Microsoft Visual C++ all provide more than one way to
create or start a console application), sometimes you have a starting
empty file or a file with a few lines. Whatever is in the file, you do
not need to delete it. For example, KDevelop displays a commented
message in the file. You should not delete that text and it will never
interfere with your program. Borland C++ Builder opens a file with a
couple of "#pragma" lines. You will never have any reason to delete
those lines, although you can, without any risk; but since they do not
affect your program, why waste your time deleting
them? Depending on the programming environment you are using and how you create your program, the first file may display a line as #include <iostream.h> or another #include line. The file may also have a main() function already included. Here is how we will deal with this issue:
|
Project Creation
|
Creating and Executing a Dev-C++ 4 Application
|
Dev-C++ is a free programming environment. To get it,
you can download it from http://www.bloodshed.net.
If you decide to use it, you should help the developers with a financial
contribution.
|
Borland C++BuilderX
|
Borland C++BuilderX is a commercial programming
environment developed by Borland. To help programmers, Borland published a
free version, called Personal Edition, that you can download and use for
your lessons.
|
- On the main menu of C++BuilderX, click File -> New...
- In the Object Gallery dialog box, click New Console
- Click OK
- In the New Console Application - Step 1 of 3, enter the name of the new
application in the Name edit box. In this case, you can type Exercise1
- Click Next
- In the New Console Application Wizard - Step 2 of 3, accept all defaults and click Next
- In the New Console Application Wizard - Step 3 of 3, click the check box under Create
- Click Untitled1 and delete it to replace it with Exercise
- Click Finish
- In the Project Content frame, double-click Exercise.cpp to display it in
the right frame
- To execute the application, on the main menu, click Run -> Run Project
Borland C++ Builder (Console) Applications
|
Borland C++ Builder is a commercial programming
environment developed by Borland. To get, you usually must purchase it.
|
|
Linux C++ (Terminal) Applications
|
Most, or all, Linux distributions provide various
free C++ compilers. The following instructions are from Red Hat Linux 7.2.
|
|
KDevelop C++ Projects
|
KDevelop is a free programming environment available
for the Linux operating system. In some cases, when installing the
operating system, you may be prompted whether you want to install KDevelop.
Even after the installation, you can add it. If you didn't install it or
don't have it on CD or DVD, you can download it free from http://www.kdevelop.org.
|
|
Microsoft Visual C++ (5, 6) Console Applications
|
Visual C++ is a commercial programming environment
developed by Microsoft. You must purchase it if you want to use it
(normally, because there is a new version of Visual C++, you may not find
Visual C++ 6 anymore from Microsoft).
|
|
Microsoft Visual C++ .NET Console Applications
|
Visual C++ .NET is a commercial programming
environment developed by Microsoft. It is usually shipped with Microsoft
Visual Studio .NET but in some cases, you can purchase it alone. In fact,
there is a version made for students and sold at a low price.
|
|
Code Fundamentals
|
Using cout
|
There are various ways data get in your
program. The first means of entering data in a C++ program is by typing
it from the keyboard. Another way would be to instruct the program to
receive data from another program then process it. A program can also
receive its data or part of it from other hardware devices such as a
CD-ROM, a DVD-ROM, a modem, a video camera, etc.
To display stuff on the monitor, C++ uses
operators. The operator used to display something on the screen is
called cout (pronounce see - out) (actually, cout is a class and not an
operator, but we haven't learned what a class is). The cout word is
followed by the extraction operator <<, then some simple rules to
display anything. For example, to display a word or sentence, you
include it in double quotes " and ".
While you are giving these instructions, you type them in your program. Each C++ instruction is terminated by a semi-colon ";".
We have already seen that a program is a set
of instructions you give to the computer. These instructions are given
inside of functions. This means that an instruction is part of a
function. The thing we want to display in our program will be performed
by the main() function. In other words, the instruction to display
something will be given in main().
Here is an example:
|
#include <iostream> using namespace std; int main() { cout << "Computer Programming With C++"; return 0; }
Comments
|
The most basic documentation you will (have to) perform is to put
comments as much as you can. Comments help you and other people who read
your code to figure out what you were doing.
Comments are not read by the compiler while
executing your program. That means you write them in everyday
conversation. There are two usual ways of including comments in your
program. To comment the contents of one line, you start it with double
forward slashes like this //
Here is an example:
// include the first library #include <iostream> using namespace std; int main() { // Here is a simple sentence cout << "Hi, this is my program!"; return 0; } // The end of my program
You can include many lines of comments in your program. To do
that, comment each line with the double slashes. An alternative is to
start the beginning of the commented paragraph with /* and end the
commented section with */
Here is another commented version of our program:
|
// The exo.cpp program // Include the ostream library #include <iostream> using namespace std; int main() { /* Here is a simple sentence that I want to display when the program starts. It doesn't do much. I am planning to do more stuff in the future. */ cout << "Hi, this is my program!"; return 0; } // The end of my program
No comments:
Post a Comment