Vous êtes sur la page 1sur 5

LECTURE NUMBER 5

Introduction to C+ + Programming
The ANSI-C++ standard accepted as an international standard is relatively recent. It was
published in November 199! nevertheless the C++ lan"ua"e e#ists $rom lon" a"o %19&'(). There$ore
there are many compilers which do not support all the new capabilities included in ANSI-C++! especially
those released prior to the publication o$ the standard.
Compilers
Creatin"! compilin" and runnin" are the three sta"es o$ developin" a computer pro"ram.
The computer pro"ram which you are most li*ely to use will have all these parts inte"rated into the
one system! o$ten *nown as an inte"rated development environment %I+,). -hen you start up the
compiler! you are actually loo*in" at an editor which has been specially tailored to ma*e creatin" C++
pro"rams easier than i$ you used an editor li*e Notepad %$or -indows) or emacsjvi in .ni#. ,#tendin" the
idea o$ ma*in" it all a lot easier $or you! compilin" and runnin" the program is an done $rom the same
screen /ust by selectin" the appropriate menu item.
Creating
Creatin" a pro"ram involves two main sta"es0
1 desi"nin" a pro"ram to achieve the re2uired "oals! and
1 physically creatin" the pro"ram by typin" the instructions into the te#t editor compiler.
To create a physical pro"ram it has to be entered into the computer. To do this! normally a $ile is
opened and the te#t o$ the pro"ram is typed in. -hen pro"rammin" in C++ the name o$ the $ile normally
ends with .cpp %$ull stop! lower case cpp) . ,#ample $ilenames are - pro"ram.cpp! hello.cpp or my$ile.cpp
3ou will probably $ind that your compiler will automatically add the correct $ilename e#tension to
your pro"ram. I$ it doesn4t you simply type it in. The part o$ the $ilename a$ter the . %dot) is *nown as the
$ile e#tension.
The in$ormation held in this $ile is *nown as the source code. This is an ordinary te#t $ile which you
could load into a te#t editor such as Notepad %$or -indows) and read it.
Compiling and Running
To convert the pro"ram to a $orm $or the computer to run it! it must be compiled. This converts
the source code to instructions $or the computer to run and stores these as an e#ecutable $ile %the $ile may
have an .e#e $ilename e#tension). The process o$ compilation will depend both on the computer system
the so$tware which you are usin".
5nce the compiler has success$ully compiled the pro"ram an e#ecutable $ile will e#ist. I$ you are
usin" an I67 compatible machine then the e#ecutable II have the same $ilename as your source code! but
the $ilename e#tension has been chan"ed $rom .cpp to .e#e. So i$ you called your source code
mypro"ram.cpp en the e#ecutable $ile would be called mypro"ram.e#e.
A couple o$ points which may sound silly! but maybe not...
1 chan"in" the $ilename e#tension $rom .cpp to .e#e will have no e$$ect. 3ou have not created a
pro"ram by doin" this.
1 3ou can run the pro"ram you have created /ust by double-clic*in" on it. 3ou don4t need to run it
$rom the compiler - it is "enuinely a stand-alone pro"ram which can "ive to anyone who has a
machine runnin" the same operatin" system as you.
Linking
5ne phase .o$ the compilation which you probably didn4t notice %unless there were errors which
brou"ht it to your attention) is that there is another part to creatin" a runnin" pro"ram. This is called
lin*in". -hat happens in this phase is that all the in$ormation which tells the computer how to write
messa"es to the screen! and how %$or e#ample) to read in$ormation $rom the *eyboard! is lin*ed into the
pro"ram you write. I$ this didn4t happen you would have to write all this yoursel$ every time you wanted
to write te#t to the screen.
Fundamentals of a C++ Programming Language
Basic Elements of a C+ + Program
A. Character Set
A C++ pro"ram is constructed as a se2uence o$ characters. Amon" the characters that can be used
in the pro"ram are0
1 8owercase letters %a - 9)
1 .ppercase letters %A - :)
1 +i"its %' - 9)
1 Special characters %+! ;! -! <! =! %! >
6. Variables. Data types. Constants
-ithin a C++ pro"ram! variables are the basic data ob/ects manipulated in the pro"ram. They are
values which are re$erred to by name which can ta*e on any value while a pro"ram is runnin"! althou"h
they must be initiali9ed be$ore use. I$ you declare a variable it is not "iven a value o$ ' %9ero)? rather it
contains a random number. It is a very common bu" to use a variable in a calculation without puttin" a
value in it $irst. 3ou must initiali9e a variable - probably "ivin" it a value o$ 9ero - be$ore you use it in a
calculation or you need to ma*e sure that it is "iven a value in some other way.
6e$ore you use variables in the pro"ram they must be declared in the declarations list. -ithin this
declarations list their type is speci$ied as one o$ the data types - char! int! $loat! etc.
Variable Names
The variables can be "iven any name up to @1 characters in len"th which is composed o$ any o$ the
$ollowin" characters0
1characters a to 9 and A to : %N5T,0 upper case and lower case are distinct). Aenerally you are
better o$$ stic*in" to lower case names as much as possible.
1underscore character - Blease do not use this as the $irst character o$ a name
1numerical di"its ' to 9! but not as the $irst character o$ a name
The variable names shown in the table are valid.
Valid Variable Names
tempCdate velocity unitC1
payCperiod dayCmonthCyear $red
The variable names shown in the table are not valid.
Invalid Variable Names
D'
th
Cbirthday Ediscount FCrate
Gpercent bad-variable H$red
In addition! a number o$ *eywords are reserved in C++ $or use as commands. These *eywords are
listed in Table below and must not be used as variables. Iemember that *eywords in C++ are case
sensitive and should be in lowercase. 5ther names which you should not use as variables are the names o$
types such as int, char! $loat.
C+ + Jeywords
asm +ynamicCconst namespace template
auto else new this
bool enum operator throw
brea* e#plicit private true
case e#tern protected try
catch $alse public typede$
char $loat re"ister typeid
class $or retu m union
const $riend short typename
cons8class "oto si"ned unsi"ned
continue i$ static usin"
de$ault inline si9eo$ virtual
delete int staticCcast void
do lon" struct volatile
double mutable switch while
7eanin"$ul variable names should always be chosen to produce code which is more easily
understood. And what is a 4meanin"$ul4 nameK Simply one which describes what the variable stands $or.
Declaring Variables
The declaration o$ a variable involves statin" the data type and the variable name in the pro"ram
code! $ollowed by a semicolon to terminate the statement! i.e.
datat!pe "ariablename#
Lariables and *eywords %i.e. $loat) are case sensitive in C++ so i$ you have declared nurn as above!
then typin" Num would be incorrect. The compiler would see num and Num as two 2uite di$$erent
variables. It would *now that num was a $loat as you have /ust declared it. In reality you would have to
declare it separately li*e this0
float Num#
The $ollowin" are incorrect0
Float num#
FL$%& num#
The data type! $loat! is a built-in *eyword and must always be in lowercase. ,#amples o$ other
declarations0
Int "alue#
c'ar letter#
A char type can only hold a sin"le letter - it cannot hold chun*s o$ te#t. 3ou will see some
strai"ht$orward te#t handlin" later on in this course.
Initiali(ing Variables
A variable may be assi"ned an initial value in the declaration! e.".
float sum ) *+*#
This initial value may be altered at any point in the pro"ram by assi"nin" another value to it. So
sum can be chan"ed simply by sayin" sum ) ,+- which means that its value has chan"ed $rom '.' to @.G.
Note that0
1 all variables must have a value be$ore bein" used in an e#pression i.e. they must be initiali9ed?
1 $loats are assi"ned values with decimal points. So ' %9ero) is '.' and @ would be @.'
Mor instance in N01 previous e#ample! the $ollowin" lines o$ code could be combined! i.e.
int "alue.#
int "alue-#
int sum#
"alue. ) ,-#
"alue- ) -/#
could be replaced with
int "alue. ) ,-#
int "alue- ) -/#
int sum#

Vous aimerez peut-être aussi