Professional Documents
Culture Documents
Introduction To C++ Programming
Introduction To C++ Programming
Introduction To C++ Programming
Programming Language
LECTURE
What is C++
C++ is a high-level language and it
is evolved from C over a period of
several years starting in 1980.
The standard for C++ was jointly
int Integer;
Global Declaration
int main ( )
{
Local declaration
double radius, area;
radius = 7; Main
area = radius * radius * PI; Statements function
return 0;
}
Character Set
C++ is composed of character set :
Number : 0 to 9
Alphabetical : a to z and A to Z
Spacing
Special Character :
, . : ; ? ! ( ) {} “ ‘ + - * / = > < # % & ^ ~ | /
_
Token
Token : combination of the
characters in C++
Categorised into:
Identifiers
Reserved words/keywords
Constants
Literal String
Punctuators
Operators
Identifiers
Allows programmers to name data and other
objects in the program-variable, constant, function
etc.
Can use any capital letter A through Z, lowercase
letters a through z, digits 0 through 9 and also
underscore ( _ )
Rules for identifier
The first character must be alphabetic character
or underscore
It must consists only of alphabetic characters,
digits and underscores, cannot contain spaces
It cannot duplicate any reserved word
C++ is case-sensitive; this means that CASE,
Case, case, and CaSe are four completely different
Valid and Invalid Identifiers
Valid names Invalid names
A $sum // $ is illegal
student_name 2names // can’t start with
_aSystemNam 2
e stdnt Nmbr // can’t have
pi space
al int // reserved word
stdntNm
_anthrSysNm
PI
Reserved word/Keywords
A word that has special meaning in
C++.
Keywords cannot be used to name
identifiers.
Constant
Data values that cannot be changed
during the execution of a program
Types of constant:
Literals
constant
Defined constants
Declared constants
Literals Constant
If the data cannot be changed, we can
simply code the data value itself in a
statement 0.10 is a literal
constant
Eg: discount = 0.10 ;
Categorised into:
Integer Numerals ( eg: 178, -9, 0113, 0x4b)
Floating-Point Numerals (eg:
3.14159,6.02e23,1.6e-19 ,3.0
Characters ( eg: ‘A’, ‘p’)
Strings ( eg; “Hello World”)
int main()
{
……;
nett = salary – ( salary * EPF_RATE);
………;
}
Declared Constants
g: const float
#include <headerpi = 3.1416;
files>
using namespace std;
#define EPF_RATE 3.142
eclared inside a function
int main()
{
const double socso_rate = 0.05;
nett = salary – ( salary * EPF_RATE * socso_rate);
………;
}
Data Types in C++
Type defines a set of value and
operations that can be applied on
those values
double
long float
Stored in a form similar to scientific notation
Can be represented in
Fixed point (decimal) notation:
31.4159 0.0000625
E notation:
3.14159E1 6.25e-5
Data Type Float
Are double by default
Can be forced to be float (3.14159f) or
long double (0.0000625L)
All floating-point numbers are signed
Type Byte Size Precision Range
Global Variables
Local Variable
Local Variable
Variables Scope
Global scope
a global variable is a variable declared in
the main body of the source code, outside
all functions
Global variables can be referred from
anywhere in the code, even inside
functions, whenever it is after its
declaration.
Local Scope
alocal variable is one declared within the
body of a function or a block.
Operators
C ++ uses a set of built in operators ( Eg : +,
-, / etc).
Four classes of operators :
Arithmetic
Relational
Logical
Assignment
Arithmetic Operators
Assume int a=4, b= 5, d;
C++ Arithmetic C++ Value of d
Operation Operator Expression after
assignment
Addition + d=a+b 9
Substraction - d=b-2 3
Multiplication * d=a*b 20
Division / d = a/2 2
Modulus % d = b%3 2
Assignment Operators
Assume x=4, y=5, z=8;
Assignment Sample Similar Value of variable
Operator Expression Expression after assignment
+= x += 5 x=x+5 x=9
-= y -= x y=y-x y=1
*= x *= z x = x*z x=32
> y>x T
< y<2 F
>= x >= 3 T
<= y <= x F
== x == 5 T
!= y !=6 F
Logical Operators
Logical Operators Called Sample Operation
|| OR expression1 | | expression2
! NOT !expression
F T !(x == 60)
T F !(x != 60)
Logical Operators
Assume x=4, y=5, z=8
expression1 expression2 expression1 && Sample Expression
expression2
F T F ( z <= y) && ( x == 4)
T F F ( y != z) && ( z < x )
T T T ( z >= y ) && ( x != 3 )
Increment and Decrement
Operators
Operator Called Sample Similar Explanation
Expression Expression
int main()
{
//variables declaration
int no1;
int no2;
int value_div; Output :
int value_mod;
Enter two integral numbers : 10 6
cout << “Enter two integral numbers:”; 10 / 6 is 1 with a remainder of 4
cin >> no1 >> no2;
value_div= no1 / no2;
Octal (oct)
Values are displayed in octal numbering system
Hexadecimal (hex)
Values are in hexadecimal format
Formatting Output
Three Floating-point manipulators:
Fixed (fixed)
Displays
floating-point numbers (eg:
1.234568e+6) in the fixed-point format
(1234567.875)
Set precision (setprecision)
Usedto control the number of the decimal
places to be displayed
Showpoint (showpoint)
To show value with the decimal point
//demonstrate the output manipulator
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
char aChar;
int integer;
float dlrAmnt;
Output :
cout << “Please enter an integer ,\n”; Please enter an integer,