Top Banner

of 32

CS503 Lec1

Jul 05, 2018

Download

Documents

gaascr
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/16/2019 CS503 Lec1

    1/32

    CS 503: C++ programming languageCourse Materials:

    1. Lecture notes 2. Textbooks:

    a. Robert Lafore: Object-Oriented Programming in C++, 4th edition, Sams Publishing, !! .

    b. ".S. #ali$: C++ Programming: %rom Problem &nal'sis to Program "esign, Cengage Learning, !().3. t!er recommen"e" re#erences:

    a. Richard L. *alterman: %undamentals of C++ Programming, !( . b. "eitel, P., and "eitel *.: C++ *o o Program, th edition, Pearson /ducation, !().

    Topics:

    (. C++ Programming 0asics

    . "ecisions and Loo1s). %unctions4. Structs

    . &rra's2. Pointers3. O erloading5. Objects, Classes, and inheritance

    . Streams and %ilesStu"ent $ssessment:

    (. #idterm e6am 72848 !( , ! 1oints9.. Practical ui;;es 7(! 1oints9.

    ).

  • 8/16/2019 CS503 Lec1

    2/32

    = recommend that 'ou t'1e all of the e6am1les in e er' lecture, e enhen the' are er' sim1le. >e'ing stuff ma$es it less li$el' that 'ou

    ill forget things later. "on?t be afraid to e61eriment ith the code.#a$ing mista$es is er' educational in 1rogramming. he moremista$es 'ou ma$e earl', the more 'ou are li$el' to learn.

    he best a' to learn to 1rogram is b' riting 1rograms@ &t the end ofeach lecture and la1, 'ou ill find some e6ercises and8or a ui;.Please ans er them and e aluate 'our or$. he later lectures buildon hat 'ou ha e learned in the earlier ones, so be certain 'ou full'

    understand the material before mo ing on.

    he e6ercises further reinforce learning and challenge students to riteC++ 1rograms ith a s1ecified outcome.

  • 8/16/2019 CS503 Lec1

    3/32

    Lecture 1:

    */ 0&S=C /L/#/< S O% C++

    his lecture e61lains the basic elements of C++. &fter com1leting thislecture, students become familiar ith the basics of C++ and areread' to rite 1rograms that are com1licated enough to do somecom1utations. hese 1rograms teach 1roblem-sol ing s$ills.

  • 8/16/2019 CS503 Lec1

    4/32

    Getting Started

    Aour "e elo1ment /n ironment: this ill be e61lained in the la1s.

    Source files are te6t files and ha e e6tension .CPP

    /6ecutable files ha e the ./B/ e6tension

    Com1ilers ta$e source code and transform it into e6ecutable files, hich'our com1uter can run as it does ith other 1rograms.

  • 8/16/2019 CS503 Lec1

    5/32

    %etting Starte" &Cont.'

    he ste1s to create an e6ecutable file are:

    (. Create a source code file ith a .c11 e6tension.

    . Com1ile the source code into an object fileith the .obj or .o e6tension.

    ). Lin$ 'our object file ith an' needed librariesto 1roduce an e6ecutable 1rogram.

  • 8/16/2019 CS503 Lec1

    6/32

    Basic Program Construction

  • 8/16/2019 CS503 Lec1

    7/32

    1 // first.cpp

    2 // The first program in C++

    3 #include 4 Using namespace std

    ! int main"

    $ %

    & cout

  • 8/16/2019 CS503 Lec1

    8/32

    Comments

    - he' hel1 understand the code b' 1ro iding e61lanation about it, and

    - hel1 'ou remember the $e' details of 'our 1rogram in future.

    Should concentrate on the big 1icture, clarif'ing 'our reasons forusing a certain statement.

    he com1iler ignores the comments, so the' do not addto the file si;e or e6ecution time of the e6ecutable

    1rogram.

  • 8/16/2019 CS503 Lec1

    9/32

    Comments &Cont.'

    Comment S(ntax

    (. Start ith a double slash s'mbol )) and terminate at the end of the line.7 his is one of the e6ce1tions to the rule that the com1iler ignores hites1ace9.

    Can start at the beginning of the line or on the same line follo ing a 1rogram statement.

    /B:)) "emonstrates comments )) at t!e beginning o# t!e lineEinclude FiostreamG )) #ollo*ing a program statement )) preprocessor "irecti eusing names1ace stdH )) ,using- "irecti eint main79 )) #unction name ,main-I )) start t!e #unction bo"(cout FF J*ello orldKn H )) statementreturn !H )) statementM )) en" o# t!e #unction bo"(

  • 8/16/2019 CS503 Lec1

    10/32

    Comments &Cont.'

    . 0egins ith the 8N and ends ith N8 7not ith the end of the line9.

    /B:

    8NT!is is an ol" st(le comment.

    /ut it is a goo" approac! to make a comment o# large text since itsa es inserting )) in e er( line.

    t !as a" antages in special situations. ou can *rite a multi linecomment *it! onl( 2 comment s(mbols t!at are: ) an" )

    N8

  • 8/16/2019 CS503 Lec1

    11/32

    irecti es

    1. 4reprocessor irecti es: is an instruction to the com1iler . =t starts ith E.

    /B: Einclude FiostreamGhe 1re1rocessor directi e inclu"e tells the com1iler to insert another file into

    'our source file. =t ill be re1laced b' the contents of the indicated file.

    he file usuall' included b' Einclude is called a !ea"er #ile . 67: =OS R/that is concerned ith basic in1ut8out1ut o1erations, and contains thedeclarations that are needed b' the cout and the FF o1erator.

    ithout these declarations, the com1iler on?t recogni;e cout and ill thin$ FFis being used incorrectl'.

    $ preprocessor 7that is a 1art of the com1iler9 deals ith these directi es before it begins the real com1ilation 1rocess.

  • 8/16/2019 CS503 Lec1

    12/32

    irecti es &Cont.'

    . T!e using "irecti e

  • 8/16/2019 CS503 Lec1

    13/32

    $l*a(s start *it! main& 'he 1rogram ma' consist of man' functions, classes, and other 1rogram

    elements. 0ut on startu1, control al a's goes to main7 9. =f there is no

    function called main7 9 in 'our 1rogram, an error ill be re1orted hen'ou run the 1rogram. =n most C++ 1rograms, main7 9 calls memberfunctions and other standalone functions.

    =unction namehe 1arentheses 7 9 follo ing the ord main are the distinguishing feature

    of a function. he'?re used to hold function arguments- alues 1assedfrom the calling 1rogram to the function.

    /races an" t!e #unction bo"(he bod' of a function is surrounded b' Q T braces I M. he' surround a bloc$ of 1rogram statements.

    / er' function must use this 1air of braces around the function bod'.

  • 8/16/2019 CS503 Lec1

    14/32

    4rogram Statements

    Program statements are instructions to the com1uter to do something. suchas adding t o numbers or 1rinting a sentence.

    & semicolon H signals the end of the statement. his is a crucial 1art of th

    s'nta6 but eas' to forget.

    =f 'ou lea e the semicolon, the com1iler ill often 7although not al a's9signal an error. See the ne6t slide.

    67: cout >> 89elcome to C++ ;n8<

  • 8/16/2019 CS503 Lec1

    15/32

    9!ite space

    he end of a line isn?t im1ortant to a C++ com1iler.

    Aou can 1ut se eral statements on one line, se1arated b' an' number of s1aces or tabs,or 'ou can run a statement o er or more lines. =t?s all the same to the com1iler.

    67:he 1re ious e6am1le can be ritten this a':

    Einclude FiostreamGusingnames1ace stdHint main 79 I coutFFJ*ello orld @KnHreturn!HM

    =t ill be com1iled correctl' but it is hard to read.

  • 8/16/2019 CS503 Lec1

    16/32

    9!ite space &Cont.'

    here are se eral e6ce1tions to the rule that hites1ace is in isible to thecom1iler:

    (. Einclude line must be ritten on one line.

    . &lso, string constants, such as J/ er' age has a language of its o n ,cannot be bro$en into se1arate lines. 7=f 'ou need a long string constant'ou can insert a bac$slash 7K9 at the line brea$ or di ide the string intot o se1arate strings, each surrounded b' uotes.9

  • 8/16/2019 CS503 Lec1

    17/32

    Common /sca1e Se uenceshe name reflects the fact that the bac$slash causes an Jesca1e from the normal a' characters ar

    inter1reted. /B: t is inter1reted not as the character t but as the tab character./sca1e se uences can be used as se1arate characters or embedded in string constants.

    here are multi le a s to rint te6t as follo s.

    6scapeSe?uence

    @ame escription

    Kn

  • 8/16/2019 CS503 Lec1

    18/32

    1. Load

    2. main

    2.1 Print '(elcome'

    2.2 Print 'to C++)'

    2.3 newline

    2.4 exit ( return - )

    Program Output(elcome to C++)

    1 // first.cpp

    2 // rinting a line ith multiple statements

    3 #include

    4 using namespace st"<

    ! int main"

    $ %

    & cout

  • 8/16/2019 CS503 Lec1

    19/32

    1. Load

    2. main

    2.1 Print '(elcome'

    2.2 newline

    2.3 Print 'to'

    2.4 newline2.5 newline

    2.6 Print 'C++)'

    2.7 newline

    2.8 exit ( return - )

    Program Output

    1 // first.cpp

    2 // rinting multiple lines ith a single statement

    3 #include

    4

    ! int main"

    $ %

    & std cout

  • 8/16/2019 CS503 Lec1

    20/32

    More Useful Programs

  • 8/16/2019 CS503 Lec1

    21/32

    Aariables W Location in memor' here a alue can be stored for use b' a 1rogram. W #ust be declared ith a name and a data t'1e be#ore the' can be used.

    W Some common data t'1es are:Xint - integer numbersX"ouble - floating 1oint numbersXc!ar - characters

    W 67 : int m( ariable< ))"eclares a ariable named m( ariable of t'1e int W 67 : int ariable1B ariable2 < ))"eclares t o ariables, each of t'1e int

    Aou can 1lace ariable declarations an' here in a 1rogram. =t?s not necessar' todeclare ariables before the first e6ecutable statement. *o e er, it?s morereadable if commonl'-used ariables are located at the beginning of the 1rogram.

  • 8/16/2019 CS503 Lec1

    22/32

    & ariable ith a name total and of t'1e int in memor'.

  • 8/16/2019 CS503 Lec1

    23/32

    Aariable names W / er' ariable has a name, a t'1e, a si;e and a alue.

    W he names gi en to ariables are called identifiers.

    W hene er a ne alue is 1laced into 7assigned to9 a ariable, it re1lacesthe 1re ious alue W the old alue is destro'ed.

    W Reading ariables from memor' does not change them.

    T!e rules #or *riting i"enti#iers: W Aou can use u11er and lo ercase letters, the digits from (to , and the underscore Y W he first character must be a letter or underscore.

    W he com1iler distinguishes bet een Z[ \]^_u11er and lo ercase letters, so`ar is not the same as ar or `&R. W Aou can?t use a C++ $e' ord as a ariable name. & keyword is a

    1redefined ord ith a s1ecial meaning such as int, class, if, hile

  • 8/16/2019 CS503 Lec1

    24/32

    nput *it! cin

    he statement cin GG ftem1H 88 ftem1 is a ariable

    causes the 1rogram to ait for the user to t'1e a number. he resultingnumber ill be 1laced in the ariable ftem1.

    =n1ut ith cin.

    $

  • 8/16/2019 CS503 Lec1

    25/32

    $ssignment Statements

    X &ssign alue to a ariable.

    X he e ual sign causes the alue on the right to be assigned to theariable on the left.

    67: ar( !H

    Operators

  • 8/16/2019 CS503 Lec1

    26/32

    Operators

    X Rules of o1erator 1recedence:

    X &rithmetic o1erators:

  • 8/16/2019 CS503 Lec1

    27/32

    X &rithmetic calculations W =nteger di ision truncates remainder

    & / ! e aluates to (

    W #odulus o1erator returns the remainder& 5 ! e aluates to

    X O1erator 1recedence W Some arithmetic o1erators act before others 7/B: multi1lication before addition9.

    X 0e sure to use 1arenthesis hen needed W 67: %ind the a erage of three ariables a, b and c

    X "o not use: a + 6 + c / 3X Vse: "a + 6 + c / 3

    1 // 9ddingT o:nteger;um6ers.cpp

  • 8/16/2019 CS503 Lec1

    28/32

    1.Load

    2. main

    2.1 Initialize variablesinteger1 ,integer2 , and sum

    2.2 Print '7nterfirst integer'

    2.2.1 G et input2.3 Print '7nter

    second integer' 2.3.1 G et input

    2.4 Add variables and

    put result into sum 2.5 Print '8um is' 2.5.1 Output sum

    2.6 exit ( return - )

    Program Output

    g g ; pp

    2 // 9ddition program

    3 #include

    4 // *e "i"n t *rite using namespace st"< so *e ll repeat st"::cout man( times! int main"

    $ %

    & int integer1 integer2 sum // declaration

    , std cout > integer1 // read an integer

    11 std cout > integer2 // read an integer

    13 sum = integer1 + integer2 // assignment of sum 14 std cout

  • 8/16/2019 CS503 Lec1

    29/32

    minute uestion:

    hat is the out1ut of the follo ing 1rogram

    Einclude FiostreamGusing names1ace stdHint main79Iint 6 H

    int ' 3Hcout FF 6 + ' 8 FF endlHreturn !HM

  • 8/16/2019 CS503 Lec1

    30/32

    minute uestion: 7Cont.9

    he ans er is 5 because it ill e6ecute first '8 that is e ual )then ill add to it 6. ) 1lus results 5

    he solution is:Einclude FiostreamGusing names1ace stdHint main79Iint 6 Hint ' 3HcoutFF76+'98 H88 ill first e6ecute the addition of 6+' and then di ide the result of the addition that is( on .return !HM

    Sample ?uestions:

  • 8/16/2019 CS503 Lec1

    31/32

    Sample ?uestions:(. hat the follo ing 1rogram does ithout running itEinclude FiostreamGusing names1ace stdHint main79 Iint 6 Hint ' 3Hcout FF 6 + ' FF J FF 6 N 'Hcout FF endlH

    return !HM88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

    . hat should be corrected in the follo ing 1rograminclude FiostreamGusing names1ace stdHint main79 Icout FF D*ello orld KnDHreturn !HM88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

    ). hat is the out1ut of: cout FF JK Run, S1ot, run,K she said. H

    Sel# stu"( example:

  • 8/16/2019 CS503 Lec1

    32/32

    Sel# stu ( example:

    88 fahren.c1188 demonstrates cin, ne line

    Einclude FiostreamGusing names1ace stdHint main79Iint ftem1H 88for tem1erature in fahrenheit

    cout FF J/nter tem1erature in fahrenheit: Hcin GG ftem1Hint ctem1Hctem1 7ftem1 - ) 9 N 8 Hcout FF J/ ui alent in Celsius is: FF ctem1 FF JKn Hreturn !HM888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

    *ere?s some sam1le interaction ith the 1rogram:/nter tem1erature in fahrenheit: (/ ui alent in Celsius is: (!!