You are on page 1of 51

02.

C++
Language Basics
Oritented Object Programming C++: Chapter 02
Previous lessons

1. Introduction C++
C is (almost) a subset of C++
Learn the basics of many other languages by
yourself
Learn by doing
Do homeworks and project in group
 Use Code::blocks IDE

2 Duy Tan University


Contents

I. Structure of a program
II. Primitive Types and Literals
III. Variables
IV. Basic Input/Output
V. Operators
VI. Constants
VII.Statements and Flow Control

3 Duy Tan University


I. Structure of a program

#include <iostream>
using namespace std;
/*
My first program in C++
*/
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 4 Duy Tan University
I. Structure of a program

#include <iostream>
#include substitutes
using namespace std; whole file

/*
My first program in C++
*/
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 5 Duy Tan University
I. Structure of a program

#include <iostream>
using namespace std;
/*
My first program in C++
*/ Without this line, “cout”
has to be “std::cout”
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 6 Duy Tan University
I. Structure of a program

#include <iostream>
using namespace std;
/* Comments

My first program in C++


*/
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 7 Duy Tan University
I. Structure of a program

#include <iostream>
using namespace std;
/*
My first program in C++ main function
called when the
*/ program is run

int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 8 Duy Tan University
I. Structure of a program

#include <iostream>
using namespace std;
/*
My first program in C++ main returns an
int, 0 mean “no
*/ error”

int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 9 Duy Tan University
I. Structure of a program

#include <iostream>
using namespace std;
/*
My first program in C++
*/ Print “Hello world!” to
the console
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 10 Duy Tan University
II. Primitive Types and Literals

Integer types
Floating Point types
Character type
Boolean type

11 Duy Tan University


Integer types
Type names Short name Size Range (min size)
signed char char >=8 bits -128 to 127
unsigned char unsigned char >=8 bits 0 to 255
signed short int short >=16 bits -32,768 to 32,767
unsigned short int unsigned short >=16 bits 0 to 65,535
signed int int >=16 bits -32,768 to 32,767
unsigned int unsigned int >=16 bits 0 to 65,535
signed long int long >=32 bits -2^31 to 2^31 – 1
unsigned long int unsigned long >=32 bits 0 to 2^32 - 1
signed long long long long >=64 bits -2^63 to 2^63 – 1
int
unsigned long unsigned long >=64 bits 0 to 2^64 - 1
long int long

Size: char ≤ short ≤ int ≤ long ≤ long long


12 Duy Tan University
Integer Literals (constants)

int: (decimal, binary, octal, hex)


12, - 12 , 0b10, -0b10, 037, -037, 0xff ,-0xff
unsigned int:
12U, 037U, 0b10U, 0xffU
long:
12L, - 12L, 037L, -037L, 0xffL ,-0xffL
unsigned long:
12UL, 037UL, 0xffUL
Warning: unsigned arithmetic can be dangerous

13 Duy Tan University


Floating Point types

float
Almost always 32 bits, literals: 12.3F, -11.3F
Range: ±1.17549E-38 to ±3.40282E+38
Precision: approximately 7 decimal digits
double
Almost always 64 bits, literals: 13.2, -13.2, 3E-12
Range: ±2.22507E-308 to ±1.79769E+308
Precision: approximately 16 decimal digits
14 Duy Tan University
Character types

char
Almost always 8 bits
Character literals:
'a', 'B', '1', '#'
'\n', '\t', '\b', '\?', '\"', '\\'
'\142'
'\x61'
Others: char16_t, char32_t, wchar_t

15 Duy Tan University


Boolean type

bool
Almost always 8 bits
Boolean literals:
true
false
We can use true as 1 and false as 0

16 Duy Tan University


III. Variables

Likes C and Java:


type variableName;
type variableName = literal;
type variableName = declaredVariable;
Examples:
 int a;// Declare a to be an integer
 a = 99;
 int b = 10;// Declare and initialize b to 10
 int c = b;// Declare and initialize c from b
 double f = 1.23;
 char ch = 'A';
17 Duy Tan University
IV. Basic Input/Output

Use standard output cout to print values on screen:


 cout << value;
 cout << value1 << value2 << ... << valueN;
Examples:
int a = 10; Output
double f = 1.1; 10
cout << a;// print 10 on screen 1.1
1+1=2
cout << '\n'; // new line
cout << f << endl;// 1.1 and new line
cout << "1 + 1 = " << (1 + 1) << endl;
18 Duy Tan University
IV. Basic Input/Output (cont.)

Use standard input cin to get values from keyboard:


 cin >> variable;
 cin >> variable1 >> ... >> variableN;
Examples:
int i; float f; char ch; Output
3 stdin
cin >> i;//gets an integer i from
3 new line
cout << i << endl;//prints i and
2.1
cin >> f >> ch; p
cout << "f: " << f << endl; f: 2.1
ch: p
cout << "ch: " << ch << endl;
19 Duy Tan University
V. Operators

Arithmetic operators:
+ - * / % ++ --
Assignment operators:
= += -= *= /= %=
Relational and Comparison operators:
== != > < >= <=
Logical operators:
! && ||
Conditional ternary operator:
 condition ? result1 : result2
20 Duy Tan University
V. Operators (cont.)

Explicit type casting operator:


int i;
float f = 3.14;
i = (int)f;
i = int(f);
sizeof operator: sizeof(value)
 int a = 12;
 cout << sizeof(int) << endl; // 4 bytes
 cout << sizeof(a) << endl; // 4 bytes
 cout << sizeof('a') << endl; // 1 byte
 cout << sizeof(12.0) << endl; // 8 bytes
21 Duy Tan University
Contents

I. Structure of a program
II. Primitive Types and Literals
III. Variables
IV. Basic Input/Output
V. Operators
VI. Constants
VII.Statements and Flow Control

22 Duy Tan University


Practices by Your-Self in 5 minutes

23 Duy Tan University


Example 01

What is the output?


#include <iostream>
using namespace std;
Output
int main() {
2
int i; 16
i = 01 + 01;
cout << i << endl;
i = 010 + 010;
cout << i << endl;
return 0;
} 24 Duy Tan University
Example 02

What is the output?


#include <iostream>
using namespace std;
Output
int main()
1
{ 6
int a = 1, b = 5;
cout << a++ << endl;
cout << ++b << endl;
return 0;
}
25 Duy Tan University
Example 03

What is the output?


#include <iostream>
using namespace std;
Output
int main()
12
{ 2
float f1 = 12, f2 = 2.3;
int a = f1, b = f2;
cout << a << endl;
cout << b << endl;
return 0;
} 26 Duy Tan University
Example 04

What is the output?


#include <iostream>
using namespace std;
Output
int main() {
1.23457e+08
float f1 = 123456789; 123456789
int a = 123456789, b = f1; 123456792
cout << f1 << endl;
cout << a << endl;
cout << b << endl;
return 0;
} 27 Duy Tan University
Example 05

What is the output?


#include <iostream>
using namespace std;
Output
int main()
-13035
{ 123456789
short a = 123456789;
int b = 123456789;
cout << a << endl;
cout << b << endl;
return 0;
} 28 Duy Tan University
Y2K bug

A bug, that may have caused problems when dealing


with dates beyond December 31, 1999

29 Duy Tan University


Example 06

What is the output?


#include <iostream>
using namespace std; Output
int main() { A
char ch1 = 'A', ch2 = 97; a
67
int a = ch1 + 2;
cout << ch1 << endl;
cout << ch2 << endl;
cout << a << endl;
return 0;
} 30 Duy Tan University
Contents

I. Structure of a program
II. Primitive Types and Literals
III. Variables
IV. Basic Input/Output
V. Operators
VI. Constants
VII.Statements and Flow Control

31 Duy Tan University


VI. Constants

There are three ways to define constants:


Literals
• 1, -2, 1.5, 'a', "ABC", true, false
Preprocessor definitions (#define)
• #define PI 3.14159
• PI = 3.2;
Typed constant expressions
• const int a = 10;
• a = 5;
• const int b;

32 Duy Tan University


VI. Constants (cont.)

Example:
#include <iostream> Output
using namespace std; PI: 3.14159
#define PI 3.14159 a: 10

int main() {
const int a = 10;
cout << "PI: " << PI << endl;
cout << "a: " << a << endl;
return 0;
}

33 Duy Tan University


VII. Statements and flow control

Selection statements:
if…else
switch-case
Loops:
while and do-while
for

34 Duy Tan University


if statements

Syntax:
if (<condition>) <statement>
[else if (<condition>) <statement>]
[else <statement>]
<condition> can be anything return a value
If the returned value is false or 0, the condition is
false
Other values are make the condition mean true

35 Duy Tan University


if statements (cont.)

Examples: Output
int a = 5, b = 20; a <= b
if (a > b) 0 is false and 3 is true
cout << "a > b" << endl;
else
cout << "a <= b" << endl;
if (0)
cout << "0 is true" << endl;
else if (3)
cout << "0 is false and 3 is true" << endl;

36 Duy Tan University


switch statements

Syntax:
switch (<expression>) {
case <value 1>: <statements>
break;
case <value 2>: <statements>
case <value 3>: <statements>
...
[default: <statements>]
}
<expression> must be an integer

37 Duy Tan University


switch statements (cont.)

Examples:
int x = 2; Output
switch (x) { x is 2 or 3
case 1:
cout << "x is 1" << endl;
break;
case 2:
case 3:
cout << "x is 2 or 3" << endl;
break;
default:
cout << "x is not found";
break;
38 Duy Tan University
do and while loops

Syntax:
while (<condition>) <statements>
• Check the <condition> before execution of the
<statements> in each iteration
do <statements> while (<condition>);
• Check the <condition> after execution of
the <statements> in each iteration
We can use continue and break in the loops

39 Duy Tan University


do and while loops (cont.)

Examples:
int x = 0; Output
while (x < 3) { 0
cout << x << endl; 1
x++; 2
0
} 1
x = 0; 2
do {
cout << x << endl;
x++;
} while (x < 3);

40 Duy Tan University


for loops

Syntax:
for (<initialization>; <condition>; <increase>)
<statements>
Condition is checked before each iteration
Each expression can be ignored when unused
When <condition> ignored means it is true
We can use continue and break in the loops

41 Duy Tan University


for loops (cont.)

Examples:
Output
int sum = 0;
for (int i = 0; i <= 5; i++) { 1
3
if (i % 2 == 0)
5
continue; sum: 9
cout << i << endl;
sum = sum + i;
}
cout << "sum: " << sum << endl;

42 Duy Tan University


Example 01

What is the output?


#include <iostream>
using namespace std;
int main() { Output
int a = 2, b = 5;
#1
if ((a == 2) && (++b > 5)) a: 2
cout << "#1" << endl; b: 6
else
cout << "#2" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
43 Duy Tan University
Example 02

What is the output?


#include <iostream>
using namespace std;
int main() { Output
int a = 2, b = 5;
#2
if ((a == 1) && (++b > 5)) a: 2
cout << "#1" << endl; b: 5
else
cout << "#2" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
44 Duy Tan University
Example 03

What is the output?


#include <iostream>
using namespace std;
int main() { Output
int a = 2, b = 5;
#1
if ((a == 2) || (++b > 5)) a: 2
cout << "#1" << endl; b: 5
else
cout << "#2" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
45 Duy Tan University
Example 04

What is the output?


#include <iostream>
using namespace std;
int main() { Output
int a = 2, b = 5;
#1
if ((a == 1) || (++b > 5)) a: 2
cout << "#1" << endl; b: 6
else
cout << "#2" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
46 Duy Tan University
Example 05

What is the output?


#include <iostream>
using namespace std;
int k = 10; Output
int main() {
#1: 20
if (true) #2: 10
{
int k = 20;
cout << "#1: " << k << endl;
}
cout << "#2: " << k << endl;
return 0;
}
47 Duy Tan University
Summary

The basic C++ program contains: header, main


function, comments, staments.
Primitive types: char, unsigned char, short, unsigned
short, int, unsigned int, long, unsigned long, long
long, float, double.
cin to input, cout to output
if, if-else, else if…, switch…case
for, while, do..while
break, continues

48 Duy Tan University


Summary

I. Structure of a program
II. Primitive Types and Literals
III. Variables
IV. Basic Input/Output
V. Operators
VI. Constants
VII.Statements and Flow Control

49 Duy Tan University


Problems

1. Enter a month and a year, print number of days in


the month.
2. Write isPrime(…) function to check a number is a
prime or not.
3. Write input(…) function to enter n numbers to an
arrray, printLargestNumbers(…) to print k (k<=n)
largest number in the arrays.
4. Write add(…), insert(…) and remove(…) functions
to add, insert and remove an element of an array in a
position.
5. Write sort(…) function to sort elements in an array.
50 Duy Tan University
Problems (cont.)

6. Enter a charater:
 If the character is lowercase, print the uppercase
 If the character is uppercase, print the lowsercase
7. Enter an integer (>=0), print the integer by binary.
8. Enter an integer, print the number by binary in 16
bits.
9. Write inputMatrix(…) and displayMatrix(…) to
input and display an matrix 2D.
10. Write rotateMatrix(…) function to clockwise rotate
the matrix elements.

51 Duy Tan University

You might also like