introduction to c++ - how c++ evolved most popular languages currently: cobol, fortran, c, c++, java...

30
Introduction to C++ Introduction to C++ - How - How C++ Evolved C++ Evolved Most popular languages currently: Most popular languages currently: COBOL, Fortran, C, C++, Java (script) COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T C was developed in 1970s at AT&T (Richie) (Richie) C is a procedure-oriented language C is a procedure-oriented language C++ early 80s C++ early 80s (Bjarne Stroustrup) (Bjarne Stroustrup) C++ is an C++ is an object-oriented object-oriented superset of C superset of C Should I learn C first? Should I learn C first? Learn procedural programming before OO Learn procedural programming before OO * * *

Upload: ethan-randall

Post on 25-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Introduction to C++Introduction to C++ - How C++ Evolved - How C++ Evolved

Most popular languages currently:Most popular languages currently:

COBOL, Fortran, C, C++, Java (script)COBOL, Fortran, C, C++, Java (script)

C was developed in 1970s at AT&T C was developed in 1970s at AT&T (Richie)(Richie)

C is a procedure-oriented languageC is a procedure-oriented language

C++ early 80sC++ early 80s (Bjarne Stroustrup)(Bjarne Stroustrup)

C++ is an C++ is an object-orientedobject-oriented superset of C superset of C

Should I learn C first?Should I learn C first?

Learn procedural programming before OOLearn procedural programming before OO

* * *

EditSourceCode

Start

Compile

CompileErrors

yes

no

LinkErrors

Link

yesyes

no

Link

yes

RunTimeErrors

Done

no

* * * * * *

Source to ExecutableSource to Executable

* *

source source codecode

compiler -h-files used

object codeobject codelinked to libraries

a.outa.out

To ensure compilation was successful:

1.rm a.out

2.g++ myprog.cc

3.ls a.out

A First ProgramA First Program

1.1. #include <iostream.h>#include <iostream.h>

2.2. void main()void main()

3.3. {{

4.4. cout <<"Hello World\n";cout <<"Hello World\n";

5.5. }}

#include < > #include < > Inserts the contents of a system fileInserts the contents of a system file

iostream.h iostream.h the particular file to includethe particular file to include

Can include user files, tooCan include user files, too

Breaking it DownBreaking it Down

Preprocessor can include a filePreprocessor can include a file

* *

Including Shared CodeIncluding Shared Code

source codesource code

preprocessorpreprocessor

expanded source code expanded source code

C++ compilerC++ compiler

#include< >#include< >

#include<iostream.h>#include<iostream.h>

void main( )void main( )

{{

}}

A Simple ProgramA Simple Program

Parts of a simple programParts of a simple program

* *

statements

Header

Body

int main ( )int main ( )

Function Header Line Function Header Line

main() is called by & returns to OSmain() is called by & returns to OS

function namefunction name

type of returned valuetype of returned value

argumentargument

* * * *

headerheader void main( )void main( )

{{

} }

Parts of all FunctionsParts of all Functions

There must be exactly one main()There must be exactly one main()

note alignment

*

body body {{

Function:Function:

<<<< insertion operator (“put to”)

\t\t tab\n \n endl endlhard return -

begin a new line

Intro to Intro to coutcout (Console OUTput)(Console OUTput)

Prints Prints valuesvalues to the screen to the screen

* *

cout <<"Here is 5: "<<5<<"\n";cout <<"Here is 5: "<<5<<"\n";

Output

Here is 5: 5Here is 5: 5

cout cout Example Example

Prints two Prints two stringsstrings and one and one literal integerliteral integer

* *

cout <<"A big number:cout <<"A big number:\t\t"<<70000<<endl;"<<70000<<endl;

OutputOutput

A big number: 70000A big number: 70000

Note: Note: endlendl does the same thing as does the same thing as “\n”“\n”

Another Another cout cout ExampleExample

Additional space from the Additional space from the TABTAB character character

* *

cout <<"The sum of 8 & 5 is "<<8 + 5<<“\n”;cout <<"The sum of 8 & 5 is "<<8 + 5<<“\n”;

OutputOutput

The sum of 8 & 5 is 13The sum of 8 & 5 is 13

Expressions & Expressions & coutcout

The value of the The value of the expressionexpression is displayed is displayed

* *

cout <<"Big # : "<< 7000*7000<<endl;cout <<"Big # : "<< 7000*7000<<endl;

Output

Big # : 4.9e+07Big # : 4.9e+07

A Look at A Look at coutcout

49000000 is bigger than default space49000000 is bigger than default space

* *

4.9 *107

Scientific notation

cout <<"A fraction: "<<5/8 <<endl;cout <<"A fraction: "<<5/8 <<endl;

Output

A fraction: 0.625A fraction: 0.625

A Look at A Look at coutcout

A decimal fractionA decimal fraction

* *

Reserved WordsReserved Words

Words that have special meanings in the

language. They must be used only for

their specified purpose. Using them for

any other purpose will result in a error.

e.g. cout do if switchcin while else return

*

Reserved WordsReserved Words

C++ is case-sensitive.

Thus:

cout COUT Cout cOut

all have different meanings. The reserved words are all in lowercase.

Don’t use reserved words other ways, even with different capitalization.

* *

StatementsStatements

A statement controls the sequence of

execution, evaluates an expression, or does

nothing, and ends with a semicolon.

Preprocessor statements do not end with semicolon

#include <iostream.h>

They do always start with #

4 Statements4 Statements

{{

cout <<"A fraction: "<<5/8 <<endl;cout <<"A fraction: "<<5/8 <<endl;

cout <<"Big # : "<< 7000*7000<<endl;cout <<"Big # : "<< 7000*7000<<endl;

cout <<8 + 5 <<" is the sum of 8 & 5\n";cout <<8 + 5 <<" is the sum of 8 & 5\n";

cout << “Hello world”;cout << “Hello world”;

}}

CommentsComments

These are important parts of a program.These are important parts of a program.

Two typesTwo types

//

/* */ or /*

*/

* *

CommentsComments

Do:Do:Add comments to source code.Add comments to source code.

Keep comments up to date.Keep comments up to date.

Use comments to explain sections of code.Use comments to explain sections of code.

Don't:Don't:Use comments for code that is self-explanatory.Use comments for code that is self-explanatory.

* * * *

Programming StyleProgramming Style

Always list function return type. Void is used Always list function return type. Void is used to indicate no value is returnedto indicate no value is returned

void main ( void main ( voidvoid ) ){{

statements; statements; }}

optional

* *

Programming StyleProgramming Style

group declarations at the beginninggroup declarations at the beginning

void main ( )void main ( ){{

declaration statements;declaration statements;

other statements;other statements;}}

Programming StyleProgramming Style

blank lines before and after control structuresblank lines before and after control structures

void main ()void main (){{

statements;statements;

if (expression)if (expression){{

statement;statement;statement;statement;

}}

statements;statements;}}

* * *

                                                                                          

      

     

                                                                                          

      

                                              

Slide 30 of 32

                                                                                          

      

      

                                                                                          

      

                                              

Slide 32 of 32

Success comes

before work only

in the dictionary.