Module 3 - Intro To C++
Module 3 - Intro To C++
Module 3
Brief History | Turbo C++ Editor Environment | Elements of C++ | Structure of C++ Program
Brief History
C++ is an extension of C
Developed by Bjarne Stroustrup
Early 1980’s at Bell Laboratories
It provides capabilities for object-
oriented programming.
TURBO C++ EDITOR ENVIRONMENT
2. Identifiers
• Variables
• Constants
3. Data Types
4. Operators
• Assignment
• Arithmetic
• Increment / Decrement
• Relational
• Logical
1. CHARACTER SET
Characters that can be used in a program are:
Lowercase letters (a-z)
Uppercase letters (A-Z)
Digits (0-9)
Special Characters (+,-,*,/,=,(,),{,}……)
2. IDENTIFIERS
These are a sequence of one or more
letters, digits, or underline symbols( _ ) that
are used to identify functions, classes, or
other elements of C++ language.
TYPES OF IDENTIFIERS
• Predefined Identifier – Reserved Words / Keywords
• User-defined Identifier – Variable Names
PREDEFINED IDENTIFIERS / C++ RESERVED WORDS
What is a variable?
A variable is a data storage location that has a value,
which can change during program execution.
By using a variable’s name in your program, you are, in
effect referring to the data stored there.
You must initialize a variable - probably giving it a value
of zero - before you use it in a calculation or you need to
make sure that it is given a value in some other way
All variables in C++ must be declared prior to their use.
How to create a variable name?
Variables can be given any name up to 31 characters in
length which is composed of any of the following
characters:
Characters a to z and A to Z
(NOTE: upper case and lower case are distinct)
The first character must be a letter.
Only letters, digits or underscores may follow the
initial letter.
Blank spaces and special characters are not allowed .
How to create a variable name?
Use the underscore to separate words in a
name consisting of multiple words or
capitalize the first letter of one or more
words.
Do not use underscore as the first character
of a name.
Must not be any of the 32 keywords.
Variable names must be meaningful.
Tell whether the following variables
are valid or invalid:
velocity @discount
#sam
unit_1
break
day_month_year
first-variable
temp_data
%_rate
pay_period
40_birthday
2percent
Tell whether the following variables
are valid or invalid:
velocity @discount
#sam
unit_1
break
day_month_year
first-variable
temp_data
%_rate
pay_period
40_birthday
2percent Red means invalid
How to declare variables?
Syntax:
datatype variable_name;
datatype var1, var2, varn;
Examples:
int value;
char letter; - - > l e t t e r v a r i a b l e ca n o n l y s t o r e s i n g l e ch a r a c t e r i n p u t
float num;
int a, size, x;
double interest, profit;
char name[20]; - - > n am e v ar i able can s to re u p to 20 characters i n pu t
Declaring variables with initial values
int value1 = 0;
int value2 = 27;
T H ER E A R E T HR EE P L A C ES IN A C+ + P R O GR A M WH ER E
VA R IA B L ES CA N B E D E C L A R ED :
double Double Precision 8 bytes +/- 1.7e +/- 308 (~15 digits)
Types of Operators
Assignment Operator
Arithmetic Operators
Compound Operators
Relational Operators
Logical Operators
a. Assignment Operator
++, --
*, /, %
+, -
=
c. Compound Operators
Example:
x += y; --> this means x=x+y
compound operator
d. Increment/Decrement Operator
Example:
a++
b--
d. Relational Operators
These are operators that allow the
comparison of two or more numerical values,
yielding a result based on whatever the
comparison is true(1) or false(0).
d. Relational Operators (cont.)
OPERATORS ACTION
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
== Equal to
!= Not equal
Example:
Expression Evaluates As
5 == 1 0 (false)
5 >= 1 1 (true)
5 != 1 1 (true)
(5+10) == (3*5) 1 (true)
e. Logical Operators
These operators work with logical values
true(1) and false(0) allowing to combine
relational operators.
OPERATORS ACTION
&& and
|| or
! not
The truth table of basic logical operations
p q p && q p || q !p
false false false false true
false true false true true
true false false true false
true true true true false
Examples:
Expression Evaluates As
(5 == 5) && (6 !=2) True
(5 > 1) || (6 < 1) True
STRUCTURE
OF A
C++ PROGRAM
GENERAL FORM OF A C++ PROGRAM
f u n c t i o n1 ( )
p reprocessor directives {
gl ob al decl arations; l o c a l d e c l a ra t io n ;
rem arks statement 1;
:
m ain ( ) statement n;
}
{
:
l ocal decl arations;
:
statem ent 1; f u n c t i o n2 ( )
: {
: l o c a l d e c l a ra t io n ;
statem ent n ; Statement 1;
g etch (); :
Statement n;
return val ue;
}
}
1. PREPROCESSOR DIRECTIVE
Example:
#define pi 3.14159
#define taxrate 0.0725
#define g gotoxy --> r e n a m i n g g o t o x y c o m m a n d t o l e t t e r g
The #define Directive
// defined constants: calculate circumference
#include < iostream.h>
#include< conio.h>
#define pi 3.14159
#define NEWLINE ' \n'
int main ()
{
double r = 5.0; // radius double circle;
circle = 2 * pi * r;
cout << circle; cout << NEWLINE;
getch();
return 0;
}
2. VARIABLE DEFINITION
Example:
void area(int l, int w);
int hypotenuse(int a, int b);
8. RETURN STATEMENT