Lecture 2 - C++ Basics
Lecture 2 - C++ Basics
Lecture 2
C++ Basics
C.F. Kwong
[email protected]
Department of Electrical and Electronic Engineering
Faculty of Science and Engineering
Room: PMB 325
Topics
Output in C++: cout
C++ Data Types
Arithmetic Operators
Input in C++: cin
if else, switch
Loops
Functions
Arrays
File Operations
Now you can understand a little bit why C++ is a higher level
language than C – the format of printing in C++ is not as “strict”
as C, but more humanized.
Lecture 2 – C++ Basics
Formatting Output
Can control how output displays for numeric, string data,
e.g. size, position, number of digits
Requires iomanip header file
Some affect just the next value displayed:
– setw(x): print in a field at least x spaces wide. Use more
spaces if field is not wide enough
Some affect values until changed again:
– fixed: use decimal notation for floating-point values
– setprecision(x): when used with fixed, print floating
point value using x digits after the decimal. Without fixed,
print floating-point value using x significant digits
– showpoint: always print decimal for floating-point values
Can be represented in
– Fixed point (decimal) notation:
31.4159 0.0000625
– E notation:
3.14159E1 6.25e-5
Are double by default
Lecture 2 – C++ Basics
The char Data Type
Used to hold characters or very small integer values
Usually 1 byte of memory
Numeric value of character from the character set is
stored in memory:
int x = 1;
do
{
cout << x << endl;
} while(x < 0);