Docsity

Introduction to C++ Programming: A First Program and Program Structure, Slides of Computer Programming

Partial preview of the text

Download Introduction to C++ Programming: A First Program and Program Structure and more Slides Computer Programming in PDF only on Docsity!

Introduction to programming,C++ language.^ •

Anatomy of a C++ Program

•^

How the program is compiled and linked

docsity.com

Books^ 

Beginning C++,the complete language, byIvor Horton,

Wrox Publishers.

C++ How to program by

Deitel &

Deitel

docsity.com

Fig.

fig01_02.cpp

A

first

program

in

C++

#include

int

main()

cout

"Welcome

to

C++!\n";

return

indicate

that

program

ended

successfully

Welcome

to

C++!

preprocessor directive Message to the C++ preprocessor.Lines beginning with

#

are preprocessor directives.

#include

tells the preprocessor to

include the contents of the file

, which

includes input/output operations (such as printing tothe screen). Comments Written between

/*

and

*/

or following a

//

.

Improve program readability and do not cause thecomputer to perform any action.C++ programs contain one or more functions, one ofwhich must be

main

Parenthesis are used to indicate a function int

means that

main

"returns" an integer value.

More in Chapter 3.A left brace

{

begins the body of every function

and a right brace

}

ends it.

Prints the

string

of characters contained between the

quotation marks.The entire line, including

cout

, the

<<

operator

,^ the

string

"Welcome

to

C++!\n"

and the

semicolon

( ;

), is called a

statement

.

All statements must end with a semicolon.

return

is a way to exit a function

from a function. return

0 , in this case, means that

the program terminated normally.

docsity.com

A Simple Program:Printing a Line of Text^ 

Cout^ ◦

Output stream object ◦^

“

Connected

”

to the screen

<<^ ◦

Stream insertion operator ◦^

Value to the right of the operator (right operand)inserted into output stream (which is connected tothe screen

)

◦^

cout <<

“

Welcome to C++!\n

”

;

**^ ◦

Escape character ◦^

Indicates that a

“

special

”

character is to be output

docsity.com

Program Structure^ 

Two kinds of files that are used to hold yoursource code:

◦^

Header files

◦^

Source files

Header files are used to contain code thatdescribes the data types that your programneeds along with other sort of declarations.

Referred as header files as they are at thebeginning of the code.

◦^

Extensions :.h and .hxx

docsity.com

Program Structure(Contd.)^ 

Source files, which have the filenameextension .cpp

contain function definitions-the executablecode for your program.

any declaration or definition of data types.

docsity.com

Creating an Executable from SourceFiles^ 

Creating a program module that you canexecute from your C++ source code isessentially a two-step process.

◦

In first step, your compiler converts.

cpp

files

to

object files

that contains machine code

equivalent of the source file contents. ◦

In second step, the linker combines the objectfiles produced by the compiler into a filecontaining the complete executable program.

docsity.com

Creating an Executable from SourceFiles(Contd.)

docsity.com

Compiling(Contd.)

docsity.com

Linking^ 

The output from the compiler of a givesource code is machine code, it is quitelong to execute.

No connection will be establishedbetween one object file to another.

Object files of source files containreferences to functions or other nameitems.

Similarly links to library functions will notyet be established.

docsity.com

Linking(Contd.)

docsity.com

Basic types

◦^

C++ has a set of basic types:

◦^

Various type operators for altering type meaning, including:

unsigned,

long,

short,

const,

static

◦^

This means we can have types such as

long

int

and

unsigned

char.

docsity.com

Declaration of variables^ 

In order to use a variable in C++, we must firstdeclare it specifying which data type we want it tobe. 

The syntax to declare a new variable is to writethe specifier of the desired data type (like

int,

bool,

float

...) followed by a valid variable

identifier. For example:

docsity.com

Declaration of

variables(Contd.)

These are two valid declarationof variables. 

The first one declares a variable of type

int

with the

identifier

a.

The second one declares a variable of type

float

with the identifier

mynumber

.

Once declared, the variables

a

and

mynumber

can be

used within the rest of their scope in the program.

docsity.com