1.1 Introduction
In this course you will learn
C and C++
Structured programming and object oriented programming
High-level languages
Similar to everyday English, use mathematical notations
(translated via compilers)
Example:
grossPay = basePay + overTimePay
Linkerlinkstheobject
4. Link
Primary
Memory
Loader
5. Load Disk
Loaderputsprogram
inmemory.
..
..
6. Execute ..
Primary
Memory
CPU CPUtakeseach
instructionand
executesit,possibly
storingnewdata
valuesastheprogram
.. executes.
..
..
6 { 3. main
preprocessor directive
7 std::cout << "Welcome to C++!\n";
Message to the C++ preprocessor.3.1 Print "Welcome to
8 C++\n"
Lines beginning with # are preprocessor directives.
9 return 0; // indicate that program ended successfully
#include <iostream> tells3.2 theexit
preprocessor
(returnto 0)
10 } include
C++ the contents
programs containofone
theor
filemore
<iostream>, which
functions, one of
includes input/output
which must be main operations (such as printing to
the screen). Program Output
Welcome to C++! Parenthesis are used to indicate a function
int means
Prints the string of characters that main
contained "returns"
between the an integer value.
quotation marks. More in Chapter 3.
return is a way to exit a function
from a function. A left brace { begins
The entire line, including std::cout, the the
<< body of every function
return 0, in this case, means the
operator, and a right to
thatstring "Welcome braceC++!\n"
} ends it. and
the program terminatedthe
normally.
semicolon (;), is called a statement.
Program Output
Welcome to C++!
integer1 45
1.22 Arithmetic
Arithmetic calculations
Use * for multiplication and / for division
Integer division truncates remainder
7 / 5 evaluates to 1
Modulus operator returns the remainder
7 % 5 evaluates to 2
Operator precedence
Some arithmetic operators act before others (i.e.,
multiplication before addition)
Be sure to use parenthesis when needed
Example: Find the average of three variables a, b and c
Do not use: a + b + c / 3
Use: (a + b + c ) / 3
1.22 Arithmetic
Arithmetic operators:
C++ operation Arithmetic Algebraic C++ expression
operator expression
Addition + f+7 f + 7
Subtraction pc p - c
Multiplication * bm b * m
Division / x/y x / y
Modulus % rmods r % s
Equalityoperators
= == x==y xisequaltoy
!= x!=y xisnotequaltoy
37
2.3 exit (return 0)
38 return 0; // indicate that program ended successfully
39 }
Object orientation
Natural way to think about the world and to write computer
programs
Attributes - properties of objects
Size, shape, color, weight, etc.
Behaviors - actions
A ball rolls, bounces, inflates and deflates
Objects can perform actions as well
Inheritance
New classes of objects absorb characteristics from existing classes
Information hiding
Objects usually do not know how other objects are implemented