0% found this document useful (0 votes)
14 views

Lecture 1 Introduction To PROGRAMMING WITH C++-1

Uploaded by

vicentmachicho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture 1 Introduction To PROGRAMMING WITH C++-1

Uploaded by

vicentmachicho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAMMING WITH C++

Introduction
Computer programming is the art of writing instructions (programs) that ask the
computer to do something and give a result. A computer receives instructions in many
different forms, four of which are particularly important.

The first instructions are given by the manufacturers of various hardware parts such as
the microprocessor, the motherboard, the floppy and the CD-ROM drives, etc. These
parts are usually made by different companies, setting different and various goals that
their particular part can perform. The instructions given to the microprocessor, for
example, tell it how to perform calculations, at what speed, and under which
circumstances. The instructions given to the motherboard tell it to behave like a city
where people and cars can move from one part of the town to another, back and forth,
for various reasons; this allows information to flow from one part of the city, I mean
one section of the computer, to another.

Once the instructions given to the hardware parts are known, software engineers use
that information to give the second sets of instructions to the computer. These
instructions, known as an operating system, are usually written by one company. These
second instructions tell the computer how to coordinate its different components so the
result will be a combination of different effects. This time, the computer is instructed
about where the pieces of information it receives are coming from, what to do with
them, then where to send the result. This time also the operating system designers
impose a lot of behaviors to the computer as a machine. Again this time, some
computer languages are developed so that programmers can write applications as the
third set of instructions. It is like developing languages that people in a city can use to
talk to each other. Consider that from now on (once the OS is developed), people get
into the habit of doing things according to their particular culture or taste, speaking
different languages that their neighbor doesn't understand... Luckily, the computer, I
should say the OS, understands all these languages. Some of the operating systems on
the market are: Microsoft Windows 3.X, Corel Linux, IBM OS\2, Microsoft Windows 9X,
Apple OS 10, Red Hat Linux, Microsoft Windows Millennium, BeOS, Caldera Linux,
Microsoft Windows 2000 etc. A particular OS (for example Microsoft Windows 98)
depending on a particular processor (for example Intel Pentium) is sometimes referred
to as a platform. Some of the computer languages running on Microsoft Windows
operating systems are C++, Pascal, Basic, and their variants.

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

1
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"

2
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>

C++ works by giving (separate) instructions to the computer. These instructions can be
treated as assignments. In this notes, 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 as main().

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.h>
main(){}

Note that <iostream.h> is a preprocessor directive. It tells the preprocessor to include


the contents of iostream header file in the program before compilation. This file is
required for input output statements.

3
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.

Basic Input/output operations

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. To display anything on the monitor,
C++ uses operators. The operator used to display something on the screen is called
cout. The cout word is followed by the extraction operator <<, then some simple
rules to display anything.

Example

#include <iostream.h>

int main()
{
cout << "Computer Programming With C++";
return 0;
}

cin

Besides the cout extractor, C++ is equipped with another operator used to request
values from the user. The user usually provides such a value by typing it using the
keyboard. Unlike the cout operator, the cin uses two greater than signs “>>” followed
by the name of the expected value. The syntax of the cin operator is:

4
cin >> valueName;

If you are requesting many values from the user, you can write a statement that
would act accordingly by separating values with as many >> operators as necessary.
For example, to request a first name, last name, and an age on the same line, you
could use:

cin >> firstName >> lastName >> Age;

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.h>

int main()
{
// Here is a simple sentence
cout << "Hi, this is my program!";
return 0;
}
// The end of my program

Note:
Return Statement

Last line in the body of our first program is

return 0;

The keyword return is a special statement which is used to exit a function and return a
value. This statement exits the function main indicating its completion and returns the
value 0 to the operating system indicating that the program successfully ran to
completion. return statement is included at the end of every main function.
5

You might also like