You are on page 1of 32

Chapter 2

Basic Elements of C++

Outline
1.

The Basics of a C++ Program

2.

Data Types

3.

Arithmetic Operators and Operator Precedence

4.

Expressions

5.

Type Conversion (Casting)

6.

string type

7.

Input

8.

Increment and Decrement Operators

9.

Output

10.

Processor Directives

11.

Creating a C++ Program

1. The Basics of a C++ Program


// This is a C++ program. It prints the sentence :
// Welcome to C++ Programming
#include <iostream>
using namespace std;
int main()
{
cout << Welcome to C++ Programming. << endl;
return 0;
}

1. The Basics of a C++ Program

C++ Program
Editor
Preprocessor (#)
Library

Compiler
Linker
Loader
Execution (Running)

Syntax Error

1. The Basics of a C++ Program: Comments

Comments are added to the program to explain the


meaning of some statements.

Comments are for the reader not for the compiler.

Two common types of comments:

// for one-line comments

/*

*/ for multiple-line comments

1. The Basics of a C++ Program: Special Symbols

Mathematical symbols

Punctuation marks

=<

==

=!

=>

Tokens

1. The Basics of a C++ Program: Reserved Words

C++ Reserved words = Keywords.

Example: int, float, double, char, const, void, return,


class,

Lower case letters for the keywords.

Cant be defined.

1. The Basics of a C++ Program: Identifiers

C++ variable names defined by the user or predefined

Identifiers can be a combination of letters, digits, and


the underscore character (_)

Identifiers must begin with a letter or _

Identifiers can be of any length

Examples: first, conversion, payRate, counter1

1. The Basics of a C++ Program: Identifiers

Illegal Identifier

Description

Employee Salary

Spaces are not allowed

!Hello

Is not allowed!

One+two

+ Is not allowed

2nd

Cant begin with a digit

2. Data Types
C++ Data Types

Integral

Simple

Structured

Floating-Point

Enumeration

char, short, int, long, bool,


unsigned char
unsigned short
unsigned int
unsigned long

float
double
long double

Pointers

2. Data Types

Data Type

Values

Storage (in bytes)

int

to 2147483647 2147483648-

bool

true and false

()char

to 127 128-

3. Arithmetic Operators and Operator Precedence

Standard arithmetic operators: +, -, *, /, and %

% has to be used with only integers to find the reminder.

/ when used with integers it gives an integer quotient.

Operators can be binary (+, -, *, /, and % ) or unary (!)

3. Arithmetic Operators and Operator Precedence


#include <iostream>
using namespace std;
int main()

2 + 5 = 10
20 34 = -14

{
cout << 2 + 5 = << 2 + 5 << endl;
cout << 20 34 = << 20 - 34 << endl;
cout << 2 * 10 = << 2 * 10 << endl;
cout << 8 / 3 = << 8 / 3 << endl;
cout << 11 % 3 = << 11 % 3 << endl;
return 0;
}

Sample run (Output):

2 * 10 = 20
8/3=2
11 % 3 = 2

3. Arithmetic Operators and Operator Precedence


#include <iostream>
using namespace std;
int main()

Sample run (Output):


5.0 + 3.5 = 8.5
4.2 * 2.5 = 10.5

{
cout << 5.0 + 3.5 = << 5.0 + 3.5 << endl;
cout << 4.2 * 2.5 = << 4.2 * 2.5 << endl;
cout << 5.0 / 2.0 = << 5.0 / 2.0 << endl;
return 0;
{

5.0 / 2.0 = 2.5

3. Arithmetic Operators and Operator Precedence

C++ uses the operator precedence rules to evaluate a


mixed-operator expressions.

*, /, and % has higher level than + and

*, /, and % have the same level of precedence.

Operations are performed from left to right for samelevel operators.

3. Arithmetic Operators and Operator Precedence


6+4/5*2+67*3

Initial expression

6 + ((4 / (5 * 2)) + (6 (7 * 3))) =

Equivalent expression

6 + ((4 / 10) + (6 - 21)) =

*)Evaluate(

6 + (2 + (6 21)) =

)Evaluate /, Integer Division(

6+ (2 + 15) =

-)Evaluate(

=6 + 17

+)Evaluate first(

23 =

+)Evaluate(

4. Expressions

An expressions is a combination between operators (+, *, /, -, %),


numbers (2, 4.5, ), and variables (x, y, )

Integer expressions:

2+3*5

3 + x y / 7 (if x and y are integers)

x + 2 * (y - z) + 18 (if x, y, and x are integers)

Floating-point expressions:

12.8 * 17.5 34.5

x * 10.5 + y 16.2 (x, y, and x are float)

Mixed expressions:

2 + 3.5 (= 5.5)

6 / 4 + 3.9 (= 4.9)

5. Type Conversion (Casting)

The cast operator, also called type conversion or type casting takes the
following form: static_cast<dataTypeName>(expression)

Expression

Evaluates to

static_cast<int>(7.9)

static_cast<double>(25)

25.0

static_cast<double>(5+3)

8.0

static_cast<double>(15)/2

15.0/2 = 7.5

static_cast<double>(15/2)

7.0

6. string Type

A string is sequence of 0 or more characters.

Strings in C++ are enclosed in double quotation marks.

A string containing no characters is called a null or empty string.

Example of strings: , ITCS 101 103, Hello

Every character in a string has a relative position in a string. The


relative positions start from 0.

The length of string is the number of its characters.

To use a string in C++ program, you have to add the directive


#include<string> at the top of the program.

7. Input

To handle data and perform calculations, we need to allocate


memory and include statements in the program to put data into
the allocated memory.

Some data must stay constant throughout a program, some other


is variable.

const dataType identifier = value; // for constants

const double CONVERSION = 2.54;

const int NO_OF_STUDENTS = 30;

const char BLANK = ;

const double PAY_RATE = 15.75;

const float PAY_RATE = 15.75f;

7. Input

A variable: A memory location whose content may change during


program execution.

Syntax to declare a variable


dataType identifier1, identifier2, ;
Examples
double amountDue;
int counter;
int x, y;
string name;

7. Input: Putting Data into Variables

You can place data into variables in two ways:

Use C++s assignment statement.

Use input (read) statements.

Assignment: variable = expression;


int num1, num2;
double sale;
char first;
string str;
num1 = 4;
num2 = 4*5-11;
sale = 0.02*1000;
first = 'D';
str = "It's a sunny day.";

int num1 = 4, num2 = 4*5-11;


double sale = 0.02*1000;
char first = 'D;
string str = "It's a sunny day.";

7. Input: walk-through
Statement

Values of the variables

Explanation

int num1, num2, num3;

?
num1

?
num2

?
num3

num1 = 18;

18
num1

?
num2

?
num3

num1 = num1 + 27;

45
num1

?
num2

?
num3

num1 + 27 = 18 + 27 = 45
45 will be assigned to num1.

num2 = num1;

45
num1

45
num2

?
num3

Copy the value of num1 into


num2.

num3 = num2 / 5;

45
num1

45
num2

9
num3

num2 / 5 = 45 / 5 = 9. This
value is assigned to num3.

num3 = num3 / 4

45
num1

45
num2

2
num3

num3 / 4 = 9 / 4 = 2. This
value is assigned to num3.

7. Input: Putting Data into Variables

Read Statement: cin>>variable1>>variable2;

#include<iostream>
using namespace std;
int main()
{
int feet, inches;
cout << "Enter two integers separated by spaces: ";
cin >> feet >> inches;
cout << endl;
cout << "Feet = " << feet << endl;
cout << "Inches = " <<inches << endl;
return 0;
Output:
}
Enter two integers separated by spaces: 23 7
Feet = 23
Inches 7

7. Input: cin: Example 2


#include<iostream>
using namespace std;
int main()
{
int x, y;
cout << "Enter two integers separated by spaces: ";
cin >> x >> y;
cout << endl;
cout << x + y = " << x + y << endl;
cout << x - y = " << x - y << endl;
cout << x * y = " << x * y << endl;
cout << x / y = " << x / y << endl;
return 0;
}

8. Increment and Decrement Operators

To simplify statements like var = var + 1; or var = var 1; C++


provides two operators: increment and decrement

Increment:

Pre-increment: ++var;

Post-increment: var++;

Decrement:

Pre-decrement: --var;

Post-decrement: var--;

8. Increment and Decrement Operators: Example

C++ Expressions
x = 5;
y = ++x;

// x = 5 and y = 6

x = 5;
y = x++;

// x = 6 and y = 5

a = 5;
b = 2 + (++a);

// a = 6 and b = 8

a = 5;
b = 2 + (a++);

// a = 6 and b = 7

9. Output

Write Statement: cout<< expression or manipulator << ;

<< is called the stream insertion operator (>> stream extraction


operator).

The expression is evaluated and its value is printed at the


current insertion point on the output device.

A manipulator is used to format the output (endl is the simplest).

The standard output devise is the screen.

9. Output: Examples
Statement
cout << 29 / 4 << endl;
cout << Hello there. << endl;

Output
7
Hello there.

cout << Hello \n there. << endl;

Hello
there

cout << 4 + 7 << endl;

4+7

cout << A << endl;


cout << 4 + 7 = << 4 + 7 << endl;

A
4 + 7 = 11

9. Output: Escape Sequences

Symbol

Escape Sequence

Description

\n

Newline

Cursor moves to the beginning of the


next line

\t

Tab

Cursor moves to the next tab stop

\b

Backspace

Cursor moves one space to the left

\r

Return

Cursor moves to the beginning of the


current line

\\

Backslash

Backslash is printed

Single Quotation

Single Quotation is printed

Double Quotation

Double Quotation is printed

10. Preprocessor Directives

Preprocessor

directives

are

commands

supplied

to

the

preprocessor to modify the text of a C++ program before it is


compiled.

They are placed at the top of the program.

Syntax: #include <HeaderFileName>

Example: #include<iostream> // to allow the program using the


C++ identifiers cin and cout.

11. Creating a C++ Program


preprocessor directives to include header files
using statement
declare named constants, if necessary
int main()
{
statement 1
.
.
.
stement n
return 0;
}

You might also like