Lecture - 02-Basics of C++
Lecture - 02-Basics of C++
AN OVERVIEW OF C++
C++
PROGRAMS
C++ programs are made of global
Preprocessor Directives
#include <iostream.h>
3
THE PROGRAM PART: THE HEADING
The heading part has the following form
Every C++ program has a function
main. typeOfFunction main(argument list)
The basic parts of the function main
are:
1. The heading int main()
2. Body of the function {
/* print out the message */
cout<<"Welcome to C++"<<endl;
return 0;
}
The statement
6
THE
IndicatesHELLO WORLD PROGRAM
the beginning of
< iostream.h > is
the functions body
included in this program
#include <iostream.h> because it provides
information to the compiler
int main (void) about cout function
{
cout<<hello world\n; Block
cout is a function from a
return 0; standard library that prints
} on the screen
The string hello world\n is sent to the cout function and the
function will display the message on the screen
The \n that appears in the cout function call will cause the message
to be printed on the screen then moves the printing to the next line
7
SYNTAX
A programming language is a set of rules, symbols, and special words used
to construct a program.
There are rules for both syntax (grammar) and semantics (meaning).
Syntax: The formal rules governing how one writes valid
instructions in a language
The syntax rules tell us which statements (instructions) are legal, that is,
accepted by the programming language and which are not.
8
IDENTIFIERS
Identifiers allow us to name variables, constant names, functions and
objects in the program
Each piece of data in the computer is stored at a unique memory address
Identifier names allow us to symbolically deal with the memory locations so
that we dont have to deal directly with these addresses
Different programming languages use different syntactic rules to form
identifiers
A C++ identifier are formed using the following symbols
Capital letters A to Z
Small letters a to z
Digits 0 to 9
Underscore _ shift key with minus key in the keyboard
9
IDENTIFIERS
Rules to form and Identifier
First character must be an alphabet or an underscore
A _a a123
ABcDf99_ sum average 1a_a a+2 a$1
Mark1 Mark_2 xy_1_4
Abc -abc aa.bb
Sum_of_values avg_plus_sum
A abc a9!c
these are not the same
Why they are invalid ?.
Aa aa aA AA aa
10
KEYWORDS
Keywords are words (like identifiers) that have a special meaning to
the compiler and cannot be used in your programs for other
purposes.
11
DATA TYPE
A set of values together with a set of operations is called a data
type.
Pointers.
14
INTEGERS TYPES AND RANGES
Type Sign Byte No. of Minimum value Maximum value
Size Bits
Signed -32768 32767
short int 2 16
Unsigned 0 65535
int Signed -32768 32767
2 16
16 bits Unsigned 0 65535
int Signed -2147483648 2147483647
4 32
32 bits Unsigned 0 4294967295
16
FLOATING POINT DATA TYPES
Floating point number is a number with a
Real Number C++ Floating
fraction part Point-Notation
Example for a floating point number is 75.924 7.592400E1
45.356
0.18 1.800000E-1
The C language supports three different
data types of floating point 0.0000453 4.530000E-5
-1.482 -1.482000E0
Float
7800.0 7.800000E3
Double
Long double
18
THE CHAR DATA TYPE
Character constants are enclosed
between two single quotes 'a'
ASCII char Symbolic
Name
Some of the values belonging to Null character \0
char data type are:
Alert (bell) \a
'A', '0', '+', '$', '&'
Backspace \b
Horizontal tab \t
Blank space is a character and is
New line \n
written ' ', with a space left
between the single quotes. Vertical tab \v
Form feed \f
In addition to the character there Carriage return \r
can be a \ Single quote \
The \ is known as the escape Double quote \
character and used when the backslash \\
character cannot be printed (the
first 32 characters of the ASCII table
are not printable)
19
THE STRING TYPE
The data type string is a programmer-defined type and is not part of the C++
language. The C++ standard library supplies it.
A string is a sequence of zero or more characters.
Strings in C++ are enclosed in double quote marks.
A string with no characters is called a null or empty string.
21
DECLARING
VARIABLES
The following Syntax diagram shows how The following are valid
to declare a variable: declaration statements
dataType identifier;
= Literal
type identifier ;
int sum = 0 ;
23
EXAMPLES OF VARIABLE DECLARATION AND
INITIALIZATION
int a = 10, b, c = 20; char a = a,b = 65;
cin>>
cout<<
25
INPUT (READ) STATEMENT: CIN
Syntax of cin together with >>:
cin>>variable>>variable. . .;
26
INPUT (READ) STATEMENT: CIN
By using more than one variable in cin, more than one value
can be read at a time.
27
OUTPUT : COUT
The syntax of cout together with << is
cout<<expression or manipulator<<expression or manipulator...;
28
OUTPUT : COUT
Statement Output
1. cout<<29/4; 7
2. cout<<"Hello there. "; Hello there.
3. cout<<12; 12
4. cout<<"4+7"; 4+7
5. cout<<4+7; 11
6. cout<<"A"; A
7. cout<<"4 + 7 = "<<4 + 7; 4 + 7 = 11
8. cout<<2+3*5; 17
9. cout<<"Hello \nthere. "; Hello
there.
\n is called new line escape sequence.
\ (back slash) is called the escape character.
29
NEW LINE CHARACTER
The new line character, '\n'.
cout<<"Hello there.";
cout<<It is sunny day.";
Output
Hello there.It is sunny day.
30
ESCAPE SEQUENCES
The following statement is illegal in C++.
The return (or Enter) key on your keyboard cannot be part of the string.
Statement 1
cout<<"The new line \\n"<<endl;
Output 1
The new line \n
Statement 2
cout<<"The tab character \'\\t\'"<<endl;
Output 2
The tab character '\t'
Statement 3
cout<<"The string \"Sunny\" contains 5 characters\n";
Output 3
The string "Sunny" contains 5 characters
32
PROMPT LINES
cout<<"Please enter a number between 1 and 10 and"
<<" press ENTER"<<endl;
cin>>num;
When these two statements execute in the order given, first the cout statement causes
the following line of text to appear on the screen:
After seeing this line, users know that they must enter a number and press the return
key.
33