Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3330 Object-oriented Programming in C++

Similar presentations


Presentation on theme: "COP 3330 Object-oriented Programming in C++"— Presentation transcript:

1 COP 3330 Object-oriented Programming in C++
(Extremely incomplete) Review of C++ Fundamentals Spring 2019

2 From a C++ Program to an Executable
Create source code containing the program *.h (*.hpp), *.cpp (*.cc) Run preprocessor to convert source file directives to source code program statements #include, #ifndef, #define, #endif, #pragma …… Compile source code into machine instructions Link hardware-specific code to produce an executable If errors or bugs detected, go to step 1 g++ or make

3 From a C++ Program to an Executable

4 Primitive Types and Operators
Basic Types (unsigned) int, char, float, double, …… string (C++ STL): #include <string> auto (C++ 11): tells the complier to determine variable’s type from the initialization value: auto amount = 100; (int) Basic Operators Arithmetic: +, -, *, /, %, ++; = and == Pointer operations: *, & Comparisons: <, <=, >, …… Streams: << and >>

5 Input, Processing and Output
Three steps that a program typically performs Gather the data From keyboard: cin, …… From files (on hard drives): open, read, eof, close … Process the input data using algorithms Display results as output Send it to the screen: cout, …… Write to files: open, write, close

6 Type Casting Used for manual data type conversion C-style cast
cout << ch << “ is ” << (int) ch; Prestand C++ cast: value in (); cout << ch << “ is ” << int(ch); Static_cast (preferred) cout << ch << “ is ” << static_cast<int>(ch);

7 If-else Statement Short-circuit Evaluation
If(I have one more day till deadline) watch_Youtube(); write_my_C++_code(); If(I plan to fail COP 3330) watch_Youtube(); else write_my_C++_code(); Short-circuit Evaluation if(expression1 && expression2) expression1 is FALSE, so … if(expression1 || expression2) expression1 is TRUE, so … Applies to all the Boolean expressions!

8 Check Numeric Ranges Used to test to see if a value falls inside a range: if (grade >= 0 && grade <= 100) cout << "Valid grade"; Can also test to see if value falls outside of range: if (grade <= 0 || grade >= 100) cout << "Invalid grade"; Cannot use mathematical notation: if (0 <= grade <= 100) //doesn’t work! Not equal: if (grade != 100)

9 Flags Variables that signals a condition
Usually implemented as a bool variable In practice, can be used extremely effectively Example: linear search

10 Prefix vs. Postfix ++ and -- operators can be used in complex statements and expressions In prefix mode (++val, --val) the operator increments or decrements, then returns the value of the variable In postfix mode (val++, val--) the operator returns the value of the variable, then increments or decrements int num, val = 12; cout << val++; // displays 12, val is now 13; cout << ++val; // sets val to 14, then displays it num = --val; // sets val to 13, stores 13 in num num = val--; // stores 13 in num, sets val to 12

11 For Loop for(initialization; test; update)
statement; // or block in { } Perform initialization Evaluate test expression If true, execute statement If false, terminate loop execution Execute update, then re-evaluate test expression

12 Which Loop to Use? The while loop is a conditional pretest loop
Iterates as long as a certain condition exits Example: validating input Example: Reading lists of data terminated by a sentinel The do-while loop is a conditional posttest loop Always iterates at least once Example: Repeating a menu The for loop is a pretest loop Built-in expressions for initializing, testing, and updating Situations where the exact number of iterations is known

13 Let’s Change the Loop Behavior
Break Terminate execution of a loop When in an inner loop, terminates that loop and goes back to outer loop Continue Go to end of loop and prepare for next repetition while, do-while loops: go to test, repeat loop if test passes for loop: perform update step, then test, then repeat loop if test passes Return Go out of the function, no matter how deep I am in a nested loop

14 Function Definition Definition includes:
return type: data type of the value that function returns to the part of the program that called it name: name of the function. parameter list: variables containing values passed to the function body: statements that perform the function’s task, enclosed in {}

15 Passing Data to Functions
Pass by value: when an argument is passed to a function, its value is copied into the parameter Changes to the parameter in the function DO NOT affect the value of the argument Pass by reference: changes to a reference variable are made to the variable it refers to! A reference variable is an alias for another variable, defined with an ampersand (&) A mechanism that allows a function to work with the original argument from the function call, NOT a copy of the argument Provides a way for the function to ‘return’ more than one value Pass by pointer: C style, obsolete

16 Arrays Variable that store multiple values of the same type
Values are stored in adjacent memory locations Declared using [] operator: int tests[5]; Each element in an array is assigned a unique subscript Subscripts start at 0 ! No bounds checking for invalid subscripts! Arrays must be accessed via individual elements: cout << tests; // not legal Don’t try to assign one array to the other: newTests = tests; // Won't work

17 Vector (Almost Always a Better Solution)
Vector in Standard Template Library (STL) #include <vector> vector<int> scores; vector<int> scores(30); //initial size 30 vector<int> scores(30,0); //initialize to 0 scores.push_back(95); size_t vec_size = scores.size(); Automatically adds space as more is needed – no need to determine size at definition

18 Pointer Using a memory address to work with a piece of data
int *intptr = nullptr; // C++11 intptr = &num; if (ptr1 == ptr2) // compares addresses if (*ptr1 == *ptr2) // compares contents Constant pointer A constant pointer is a pointer initialized with an address, and cannot point to anything else int value = 22; int * const ptr = &value; num intptr 25 0x4a00

19 Const Pointer vs. Pointer to Const

20 Dynamic Memory Allocation
Uses new operator to allocate memory double *dptr = nullptr; dptr = new double; new returns address of memory location Use delete to free dynamic memory: delete dptr; Use [] to free dynamic array: delete [] arrayptr; Only use delete with dynamic memory!

21 Questions


Download ppt "COP 3330 Object-oriented Programming in C++"

Similar presentations


Ads by Google