02-Overview of C++ (Part 1)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Data Structures

CT077-2-3-DSTR and Version VC1

Overview of C++ (part 1)


Topics structure

• Basic C++ function (input, output)


• Selection control
• Repetition control
• Function

CT077-3-2-DSTR Data Structures 2


Learning Objective

At the end of this lesson, you should know:


• the basic components of a C++ program
• how to write a simple C++ program and use
preprocessor directives
• how to use input\output in a program
• the selection control structures if, if...else, and
switch in a program
• the repetition (looping) control structures
• how to write and use a simple function

CT077-3-2-DSTR Data Structures 3


Key terms

• Input output (cin, cout)


• Selection (if … else, switch, case)
• Repetition (for, while, do … while)
• Function prototype
• Function repetition
• Parameter passing (by value, by reference)

CT077-3-2-DSTR Data Structures 4


Creating a C++ Program

• A C++ program has two parts:


– Preprocessor directives
– The program statements (functions, classes, and
types definitions)

• Preprocessor directives and program statements


constitute C++ source code (in “.cpp” and “.h” files)

• Compiler generates object code (“.obj”) and a process


called Linking generates executable file (“.exe” on
windows)

CT077-3-2-DSTR Data Structures 5


Creating a C++ Program

• A simple C++ program is a collection of functions


(and classes, etc.), one of which is the function
main
• The first line of the function main is called the
heading of the function: int main () {
//mandatory by some compilers
void main() …
return 0; //or other int value
}
• The statements enclosed between the curly
braces ({ and }) form the body of the function

CT077-3-2-DSTR Data Structures 6


Creating a C++ Program

• Many functions and symbols needed to run a C++


program are provided as collection of libraries
– Every library has a name and is referred to by a
header file, accessed using a preprocessor
directive
• Preprocessor directives are commands to the
preprocessor, which runs prior to the compiler
– Think of the preprocessor as a program that
“set up” your source code for the compiler
• All preprocessor commands begin with #
CT077-3-2-DSTR Data Structures 7
Creating a C++ Program

• Syntax to include a header file:

• For example:
#include <iostream>
– Causes the preprocessor to include the
header file iostream in the program

CT077-3-2-DSTR Data Structures 8


Creating a C++ Program

• cin and cout are declared in the header file


iostream, but within std namespace
• To use cin and cout in a program, use the
following two statements:
#include <iostream>
using namespace std;

Without using std namespace, you must use scope


operator “ :: ” to write fully qualified names (std::cin and
std::cout)

CT077-3-2-DSTR Data Structures 9


Creating a C++ Program

• To use the string type for example, you need to


access its definition from the header file string

• Include the following preprocessor directive:


#include <string>

CT077-3-2-DSTR Data Structures 10


Creating a C++ Program

CT077-3-2-DSTR Data Structures 11


Creating a C++ Program

Sample Run:
Line 9: firstNum = 18
Line 10: Enter an integer: 15

Line 13: secondNum = 15


Line 15: The new value of firstNum = 60

CT077-3-2-DSTR Data Structures 12


Programming Example

Write a program that takes a given length as input


expressed in feet and inches. Convert and output
the length in centimeters

• Convert the length in feet and inches to all


inches:
– Multiply the number of feet by 12
• Use the conversion formula (1 inch = 2.54
centimeters) to find the equivalent length in
centimeters
CT077-3-2-DSTR Data Structures 13
Programming Example

CT077-3-2-DSTR Data Structures 14


Programming Example

Sample Run:

Enter two integers, one for feet, one for inches: 15 7

The numbers you entered are 15 for feet and 7 for inches.
The total number of inches = 187
The number of centimeters = 474.98

CT077-3-2-DSTR Data Structures 15


Selection Control

• One-Way Selection
• Two-Way Selection
• Multiple Selections: Nested if
• Condition’s evaluation to true and false
• Multiple Selections: switch Statement

CT077-3-2-DSTR Data Structures 16


One-Way Selection

• The syntax of one-way selection is:

• The statement is executed if the value of the


expression evaluates to true
• The statement is bypassed if the value is false;
program goes to the next statement
• if is a reserved word

CT077-3-2-DSTR Data Structures 17


Two-Way Selection

• Two-way selection takes the form:

• If expression is true, statement1 is executed;


otherwise, statement2 is executed
– statement1 and statement2 are any C++
statements
• else is a reserved word

CT077-3-2-DSTR Data Structures 18


Multiple Selections: Nested if

• Nesting: one control statement in another


• To test more than one condition, an if statement
can be nested inside another if statement

CT077-3-2-DSTR Data Structures 19


Condition’s Evaluation

• Beware that in C and C++, any value evaluated


to zero is considered to be false, and any other
value is treated as true
• This can help write short code, but may lead to
bugs which are very difficult to detect:
This is an assignment operator, not checking equality
char c = 'Y'; operator. The code compiles without a problem because
while(c = 'Y') { any value is evaluated as a condition, and this
expression evaluates to true always ('Y' is not zero) !!
//do something here…
cout << "Continue? (Y/N)";
cin >> c;
} //why doesn't my loop ever end?
CT077-3-2-DSTR Data Structures 20
switch Structures

• switch structure: alternate to


if-else
• Value of the expression
determines which
corresponding action is taken
• Expression is sometimes
called the selector

CT077-3-2-DSTR Data Structures 21


switch Structures

• One or more statements may


follow a case label
• The break statement may or
may not appear after each
statement
• switch, case, break, and
default are reserved words

CT077-3-2-DSTR Data Structures 22


switch Structures

CT077-3-2-DSTR Data Structures 23


Repetition Control

• Repetition control structure causes a statement


or group of statements to be repeated
• Repetition allows you to efficiently use variables
• For example, to add five numbers:
– Declare a variable for each number, input the
numbers and add the variables together
– Create a loop that reads a number into a
variable and adds it to a variable that contains
the sum of the numbers

CT077-3-2-DSTR Data Structures 24


while Loop

• The general form of the while statement is:

• Statement can be simple or compound


• Expression acts as a decision maker and is
usually a logical expression
• while is a reserved word

CT077-3-2-DSTR Data Structures 25


while Loop

CT077-3-2-DSTR Data Structures 26


Case 1: Counter-Controlled Loops

• If you know exactly how many pieces of data


need to be read, the while loop becomes a
counter-controlled loop

CT077-3-2-DSTR Data Structures 27


Case 2: Sentinel-Controlled Loops

• Sentinel variable is tested in the condition


and loop ends when sentinel is encountered

CT077-3-2-DSTR Data Structures 28


Case 3: Flag-Controlled Loops

• A flag-controlled while loop uses a bool variable


to control the loop

CT077-3-2-DSTR Data Structures 29


for Loop

• The general form of the for statement is:

• The initial statement, loop condition, and


update statement are called for loop control
statements
• for is a reserved word

CT077-3-2-DSTR Data Structures 30


do…while Loop

• General form of a do...while:

• The statement executes first, and then the


expression is evaluated
• To avoid an infinite loop, body must contain a
statement that makes the expression false
• Loop always iterates at least once

CT077-3-2-DSTR Data Structures 31


do…while Loop

CT077-3-2-DSTR Data Structures 32


Choosing the Right Looping
Structure
• All three loops have their place in C++
– If you know or can determine in advance the
number of repetitions needed, the for loop is
the best choice
– If you do not know and cannot determine in
advance the number of repetitions needed,
and it could be zero, use a while loop
– If you do not know and cannot determine in
advance the number of repetitions needed,
and it is at least one, use a do...while loop
CT077-3-2-DSTR Data Structures 33
User-Defined Functions

• A function is defined in C++ in a very similar way


as in Java
– A function can exist outside a class definition
• General syntax:

functionType can be void, and a function may have no


parameter list at all
• A function prototype is useful, and may be
necessary at some cases
CT077-3-2-DSTR Data Structures 34
Function Prototype

• Function prototype: function heading without the


body of the function
• Syntax:

• It is not necessary to specify the variable name


in the parameter list
• The data type of each parameter must be
specified

CT077-3-2-DSTR Data Structures 35


Function Example

CT077-3-2-DSTR Data Structures 36


Function Example

CT077-3-2-DSTR Data Structures 37


User-Defined Functions

• By default, parameters are passed by value


– Values that are passed when you call a function get
copied, and any change to the parameters inside the
function are not carried out on original passed-in
parameters of the function call
– We will learn how to pass parameters by pointer and
by reference in order to change them, or avoid copying

• Write a void changeMe(int v) function and test it

CT077-3-2-DSTR Data Structures 38


Summary of Teaching Points

• There are three types of selection control, if…


else, nested if...else and switch
• There are three types of repetition control, for
loop, while loop and do while.
• Different situation require different type of loop
structure.
• Predefined functions have been include the
library.

CT077-3-2-DSTR Data Structures 39


Further Reading

• NTU’s C++ Programming Tutorial


The Process of Writing a C++ Program
www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_I
ntroduction.html

• Check this article on some common mistakes in


C\C++ programming
8 Common Programming Mistakes:
www.cprogramming.com/tutorial/common.html

CT077-3-2-DSTR Data Structures 40


Summary of main teaching

• Basic C++ function (input, output)


• Selection control
• Repetition control
• Function

CT077-3-2-DSTR Data Structures 41


CT077-3-2-DSTR Data Structures 42
What we will cover next

Overview of C++
• Simple Data Types
• Arrays
• Passing parameters to functions
• Passing arrays to functions

CT077-3-2-DSTR Data Structures 43

You might also like