Chapter 2
Chapter 2
Introduction to C++
Why do we learn C++ Programming?
• C has been used successfully for every type of programming
problem imaginable from operating systems to spreadsheets to
expert systems - and efficient compilers are available for machines
ranging in power from the Apple Macintosh to the Cray
supercomputers.
preprocessor directive
#include <iostream> Include the contents of a standard input/output
header
using namespace std; Inject all the names of entities that exist in the
std namespace into the global namespace. Like cout,
cin, endl input/output header.
int main() The main function of a program where the execution
will start, int means that main function "returns"
{ an integer value, void means that main function does
not take an input.
cout << "Hello, there!"; Entire line called a statement. All statements must
end with a semicolon (;).Prints the string of
return 0; characters within quotes (" ").
Standard Output:
The cout Object
The cout Object
• cout is our standard output function and the monitor of a computer
is the standard output device.
• You use the stream insertion operator << to send output to cout:
String
20 is an integer literal literals
2:5
Variable Initialization
Variable Assignments and Initialization
• Variables can be initialized (assigned an initial value) in their
definition or after definition
• The assignment statement uses the = operator to store a value in a
variable.
num1 = 12;
int num1 = 12, num2 = 5, area;
• This statement assigns the value 12 to the num variable.
• The variable receiving the value must appear on the left side of the
= operator.
• This will NOT work:
// ERROR!
12 = num1;
Declaring Variables With the auto Key Word
• C++ 11 introduces an alternative way to define variables, using the
auto key word and an initialization value. Here is an example:
auto amount = 100; int
• The auto key word tells the compiler to determine the variable’s
data type from the initialization value.
Identifiers
Identifiers
• An identifier is a programmer-defined name for some part of a
program: variables, functions, etc.
Identifier Rules
• The first character of an identifier must be an alphabetic character
or and underscore ( _ ),
• After the first character you may use alphabetic characters,
numbers, or underscore characters.
• Upper- and lowercase characters are distinct
•
Valid and Invalid Identifiers
H e l l o \0
2:9
double amount;
cout << "A double is stored in "
<< sizeof(double) << "bytes\n";
cout << "Variable amount is stored in "
<< sizeof(amount)
<< "bytes\n";
2 : 13
Scope
Scope
• The scope of a variable: the part of the program in which the
variable can be accessed
• A variable cannot be used before it is defined
Class Exercise
Practice the different data types discussed
2 : 14
Operators
Arithmetic Operators
• Used for performing numeric calculations
• C++ has unary, binary, and ternary operators:
• unary (1 operand) -5
• binary (2 operands) 13 - 7
• ternary (3 operands) exp1 ? exp2 : exp3
Arithmetic Operators
SYMBOL OPERATION EXAMPLE VALUE OF ans
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Arithmetic Operators in Program 2-21
Comparison Operators
• Expressions are combinations of operators and their operands
which are used to modify the values of variables
• Equality & relational operators
Comments
Comments
• Used to document parts of the program
• Intended for persons reading the source code of the program:
• Indicate the purpose of the program
• Describe the use of variables
• Explain complex sections of code
• Are ignored by the compiler
Comments
Single Line Comments
• Begin with // through to the end of line:
int length = 12; // length in inches
// calculate rectangle area
area = length * width;
Comments
Multi-line Comments
• Begin with /*, end with */
• Can span multiple lines:
/* this is a multi-line
comment
*/
• Can begin and end on the same line:
int area; /* calculated area */
2 : 16
Named Constants
Named Constants
• Named constant (constant variable): variable whose content
cannot be changed during program execution
• Used for representing constant values with descriptive names:
const double TAX_RATE = 0.0675;
const int NUM_STATES = 50;
• Often named in uppercase letters
Named Constants in Program 2-28
Exercise 1
What is the output
of the program?
Exercises
Exercise 2:
• Write a complete C program that calculates and displays the
average of two numbers.
Exercise 3:
• Write a complete C program that calculates and displays the
product (multiplication) of 3 numbers.
Exercise 4:
• Write a complete C program for that calculates and displays the
addition of the squares of that two numbers
Questions??
Thank you for your time.