Midterm1 Review - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Midterm1 Review

Description:

... compatibility ... cout person1.birthday.year. Structures can be arguments in ... birthday.output( ); Call a public function from outside: must include ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 36
Provided by: hair4
Learn more at: http://cs.purduecal.edu
Category:

less

Transcript and Presenter's Notes

Title: Midterm1 Review


1
Midterm1 Review
  • Dr. Hairong Zhao
  • hairong_at_calumet.purdue.edu
  • Purdue University Calumet

2
Midterm 2
  • Wednesday, Feb 22
  • 1230-150pm
  • This room
  • Format
  • true or false
  • fill in blank
  • multiple choice
  • programming
  • Topics chapter 1 6, exclude ADT

3
Computer Organization
  • Input devices
  • Output devices
  • Processor (CPU)
  • Memory
  • Main memory
  • Memory locations containing the running program
  • Address number that identifies a memory location
  • Bit and Byte
  • A digit that can only be zero or one
  • Each memory location has eight bits
  • RAM Computer can directly access any memory
    location
  • Secondary memory
  • Permanent record of data often on a disk

4
C High-level Languages
  • C
  • a high-level language
  • derived from C
  • object oriented
  • Compiler
  • Translate source code into object code
  • Linker combines all the object codeinto an
    executable program

5
Program Errors
  • Syntax errors
  • Violation of the grammar rules of the language
  • Discovered by the compiler
  • Error messages may not always show correct
    location of errors
  • Run-time errors
  • Error conditions detected by the computer at
    run-time
  • Logic errors
  • Errors in the programs algorithm
  • Most difficult to diagnose
  • Computer does not recognize an error

6
Variables
  • Type int, float, char, structure, class
  • Value, address
  • Names (identifiers)
  • First character must be
  • a letter or the underscore character
  • Remaining characters must be
  • Letters, numbers, or underscore character
  • Can not be keywords (reserve words)
  • Before use, variables must be declared
  • Variables are initialized in assignment
    statements double mpg // declare the
    variable mpg 26.3 // initialize
    the variable

7
Input and output
  • Stream a sequence of data
  • cin extraction operator gtgt
  • cin skips blanks and line breaks
  • cout insertion operator ltlt
  • What is wrong with the following statements?
  • cin ltlt number
  • cout gtgt endl
  • Include Directives add library files to our
    programs include ltiostreamgt
  • Using Directives include a collection of defined
    names using namespace std
  • Include Directives and Using Directives do not
    have to be used togeter

8
Data Types
  • int 34 45 1 89
  • Other integer type long or long int, short or
    short int
  • double
  • 34.1 23.0034 1.0 89.9
  • 3.41e1 means 34.1
  • Other double type long double, float
  • char
  • What is the difference of a and a?
  • Hom many haracters can file_name contain?
  • char file_name16
  • bool

15
9
Data Types
  • Escape sequences
  • \n -- new line
  • \t -- a tab \\ -- a
    backslash character \" -- a quote
    character
  • What is the difference of \n and \n?
  • Formatting Real Numbers
  • setf(iosfixed)
  • setf(iosshowpoint)
  • precision(2)
  • setf(iosshowpos)

10
Type compatibility
  • int ?? double
  • Variables of type double should not be assigned
    to variables of type int
  • Integer values can normally be stored in
    variables of type double
  • char ? ? int not recommend
  • int value 'A' char letter 65
  • bool ? ? int not recommend

11
Operators
  • / and
  • 5/3, 53, 5/3.0 51.0/3
  • static_castltdoublegt(5)/3
  • 5/static_castltdoublegt(3)
  • static_castltdoublegt(5/3)
  • -- /
  • Result of an operator
  • int x3, y 4
  • double z 4.0
  • What is xy? What is xz?

12
Simple Flow of Control
  • gt lt ! equal or equivalent
  • !
  • (2 lt x) (x lt 7) !( x lt 2) (2 lt x)
    (x lt 7)
  • ( 2 lt y lt 7 )
  • If statement
  • Does if always paired with else? No
  • What is the output
  • x1
  • if(x1)
  • cout ltlt x1 ltlt endl
  • cout ltlt x!1 ltlt endl
  • What is the output
  • x2
  • if(x1)
  • cout ltlt x1 ltlt endl
  • else
  • cout ltlt x!1 ltlt endl

x1 x!1
Always true
x1
13
loop
  • While
  • Show the output of this code if x is of type
    int?x 10while ( x gt 0) cout ltlt x
    ltlt endl x x 3
  • What about this
  • x 0
  • while (x ! 12)
  • cout ltlt x ltlt endl
  • x x 2
  • A do-while loop is always executed at least once

10 7 4 1
Infinite loop
14
Function
  • Function header, body, declaration, definition?
  • Function declaration 2 ways
  • List formal parameter names
  • List types of formal parameters, but not names
  • A function can only return one value, but it can
    have many return statements.
  • A function call must be preceded by either
  • The functions declaration or
  • The functions definition
  • If the functions definition precedes the call,
    a declaration is not needed
  • In function call, the parameters and arguments
    are matched by there relative positions in the
    parameter lists.

15
Function
  • Call-by-value, call-by-reference
  • Call-by-reference parameters allow us to
    changethe variable used in the function call
  • Arguments for call-by-reference parameters must
    bevariables, not numbers
  • Call-by-value and call-by-reference parameters
    can be mixed in the same function
  • symbol (ampersand) identifies as a
    call-by-reference parameter
  • Used in both declaration and definition, but not
    in the function call
  • Void function
  • No return value
  • can still have return statement

16
Overloading function
  • Overloading a function providing more than one
    declaration and definition using the same
    function name
  • Overloaded functions must have
  • different numbers of formal parameters OR
  • at least one different type of parameter

17
Predefined Functions
  • Predefined function in cmath
  • sqrt(4.0), pow(2.0, 3), floor(6.99), ceil(1.01)
  • Must use
  • include ltcmathgt
  • using namespace std

18
Functions with Default Arguments
  • A default value can be specified in the parameter
    list
  • The default value is selected if no argument is
    available for the parameter
  • void default_args(int arg1, int arg2 -3)
    cout ltlt arg1 ltlt ' ' ltlt arg2 ltlt endl
  • default_args can be called with one or two
    parameters
  • default_args(5) //output is 5 -3
  • default_args(5, 6) //output is 5 6

19
Functions and name space
  • Different functions can have different name
    spaces
  • Using directives can be placed
  • Outside any function
  • At the beginning of a function definition

20
Using Constants
  • What is wrong with the following
  • const double PI 3.14159
  • PI3.14
  • Local variables variables declared in a
    function have the function as their scope and
    they cannot be used from outside the function
  • Global Named Constant
  • Declared outside any function body including main
  • Declared before any function that uses it

21
File input/output
  • Declare a file stream
  • ifstream in_stream // input-file stream
    variable ofstream out_stream // output-file
    stream variable
  • You must use
  • include ltfstreamgtusing namespace std
  • Connecting a stream to a file
  • in_stream.open("infile.dat")
  • out_stream.open("outfile.dat")
  • Using the streams
  • in_stream gtgt one_number gtgt another_number
  • out_stream ltlt "one number " ltlt
    one_number ltlt "another number
    " ltlt another_number

22
File input/output
  • Disconnecting a stream from a file
  • in_stream.close()
  • out_stream.close()
  • To append new output to the end an existing file
  • outStream.open("important.txt", iosapp)
  • If the file does not exist, a new file will be
    created
  • fail and exit in_stream.open("stuff.dat")
    if( in_stream.fail( ) ) cout ltlt
    "Input file opening failed.\n" exit(1)
  • // include ltcstdlibgt should be used
    for exit

23
File input/output
  • A manipulator is a function that is used after
    the insertion operator (ltlt)
  • cout ltlt "Start" ltlt setw(4) ltlt 10 ltlt setw(6) ltlt
    30
  • cout.setf(iosfixed)
  • cout.setf(iosshowpoint)
  • cout ltlt "" ltlt setprecision(2) ltlt 10.3 ltlt
    endl ltlt "" ltlt 20.5 ltlt endl
  • To use these manipulators, add include
    ltiomanipgt using namespace std
  • endl is a manipulator defined in ltiostreamgt

24
File input/output
  • Boolean expression (in_stream gtgt next)
  • Streams can be arguments to a function
  • The function's formal parameter for the stream
    must be call-by-reference
  • Function get
  • Member function of every input stream
  • Reads one character from an input stream
  • Does not skip blanks

25
File input/output
  • Function put
  • Member function of every output stream
  • Requires one argument of type char
  • Places its argument of type char in the output
    stream
  • Member function eof detects the end of a file
  • if ( ! in_stream.eof( ) )

26
Inheritance and Streams
  • cin object of the class istream (no 'f ')
  • Input-file streams are objects of the class
    ifstream, which is derived from istream.So
    input-file streams are also objects of istream
  • cout object of class ostream
  • Output-file streams are objects of class
    ofstream, which is derived from ostream
  • The following function can be called with
    ostream or ofstream arguments void
    say_hello(ostream any_out_stream)
    any_out_stream ltlt "Hello"

27
Character Functions
  • Several predefined functions
  • toUpper, toLower, isspace
  • The cctype library is required
  • include ltcctypegtusing namespace std

28
Structure
  • Define a structure
  • struct CDAccount double
    balance double interest_rate int term
    //months to maturity
  • Member of structure are public by default
  • To declare two variables
  • CDAccount my_account, your_account
  • A structure can be initialized
  • CDAccount my_account 100, 10, 10

29
Structure
  • Use the dot operator to access
  • member variable my_account.balance
  • member variable of hierarchical Structure
  • cout ltlt person1.birthday.year
  • Structures can be arguments in function calls
  • The formal parameter can be call-by-value
  • The formal parameter can be call-by-reference
  • Structures can be the type of a value returned
    bya function
  • The assignment operator can be used to
    assignvalues to structure types

30
Class
  • Define a class
  • Class className
  • public
  • // member functions private // member
    variables
  • // member functions
  • A member function can be public or private
  • Objects of the same class have
  • same member functions
  • different member variables

31
Member function
  • Define use to identify a member void
    DayOfYearoutput( )
    // function body
  • scope operator
  • Call use . to identify a member
    birthday.output( )
  • Call a public function from outside must include
    the object name
  • Call a private function can only be from another
    member function, object name is not used

32
The Assignment Operator
  • Objects and structures can be assigned values
    with the assignment operator ()

33
Constructors
  • A constructor
  • used to initialize member variables when an
    object is declared
  • Usually public
  • automatically called when an object of the class
    is declared
  • BankAccount account1
  • account1.BankAccount(10, 50, 2.0)
  • must be the same name as the class
  • cannot return a value
  • can be overloaded
  • default constructor no parameters
    BankAccount account1 // uses the default
    BankAccount constructor BankAccount account1()
    // illegal

34
Initialization Sections
  • An initialization section in a function
    definitionprovides an alternative way to
    initialize member variables
  • BankAccountBankAccount( ) balance(0),
    interest_rate(0.0)
  • // No code needed in this example
  • The values in parenthesis are the initial values
    for the member variables listed

35
Programming
  • Using if else, and while loop
  • Define a structure
  • Define a class, write member functions
  • Constructor
  • Accessor, mutator
  • Output member variables to file or cout
  • Other functions
Write a Comment
User Comments (0)
About PowerShow.com