C++ was created in the 1980s by Bjarne Stroustrup as an extension of the C language with object-oriented features. It combined features from C and Simula 67. The document provides an overview of the basic building blocks of C++ programs including characters, tokens, keywords, identifiers, literals, punctuators, operators, comments, streams, variables, and common errors. It describes the different data types and rules for writing identifiers, literals, and comments. It also explains common tokens like keywords, operators, and punctuators and how they are used in C++.
1. Getting started with C++
Prepared by:-
Ranjan Das & Akash deep baruah.
2. In 1980s bjarne Stroustrup decided to extend
the C language by adding some features from
his favourite language Simula 67. Simula 67
was one of the earliest object oriented
language. Bjarne Stroustrup called it “C with
classes”.
Later Rick Mascitti renamed as C++. Ever
since its birth, C++ evolved to cope with
problems encountered by users, and though
discussions.
3. Character set is a set of valid characters that a language
can recognize. A character represents any letter, digit,
or any other sign.
Letters: A-Z, a-z
Digits: 0-9
Special Symbols: Space + - * / ^ ( ) [ ] { } = != < >
. ‘ “ $ , ; : % ! & ? _(underscore) # <= >= @
White Spaces: Blank spaces, Horizontal tab, Carriage
return, New line, Form feed.
Other Characters: C++ can process any of the 256 ASCII
characters as data or as literals.
4. The smallest individual unit in a program is
known as a Token or lexical unit.
Types of Tokens
Keywords
Identifiers
Literals
Punctuators
Operators
5. Keywords are the words that convey a special
meaning to the language compiler. These are
reserved for special purpose and must not be
used as normal identifier names.
6. asm continue float new signed try
auto default for operator sizeof typedef
break delete friend private static union
case do goto protected struct unsigned
catch double if public switch virtual
char else inline register template void
class enum int return this volatile
const extern long short throw while
7. Identifiers are names of the program given by
user.
Rules to write identifiers
1. Do not start with digits.
2. No special symbols are used except
_(underscore).
3. No spaces are used.
Examples:- myfile , date9_2_7_6
8. Literals (constants) are data items that never
change their value during a program run.
Types of Literals:
1. Integer constant
2. Floating constants
3. Character constant
4. String literal
9. Integer constants are whole numbers without
any fractional part.
Three types of Integer constants
1. Decimal Integer constant
2. Octal Integer constant
3. Hexadecimal Integer constant
10. An integer constant consisting of a sequence of
digits is taken to be decimal integer constant unless
it begins with 0 (digit zero).
Example:- 1296, 5642, 12, +69,- 23,etc.,
11. A sequence of digits starting with0(digit zero) is
taken to be an octal integer.
Example:-123, 456, etc.,
12. A sequence of digits preceded by 0x or 0X is taken
to be an hexadecimal integer.
Example:-4B6, A43,etc.,
13. Floating constants are also called as Real
constants
Real constants are numbers having fractional
parts. These may be written in one of the two
forms called fractional form or the exponent form.
Examples:-2.0, 3.5, 8.6, etc.,
14. A Character constant is one character enclosed
in single quotes, as in ‘z’.
Examples:- ‘a’, ‘b’, etc.,
15. a Audible sound
b back space
f Formfeed
n Newline or Linefeed
r Carriage return
t Horizontal tab
v Vertical tab
Backslash
’ single quote
” double quote
? Question mark
on Octal number
xHn Hexadecimal number
0 Null
16. Multiple character constants are treated as string
literals.
Examples:-”a” , “ade”, etc.,
17. The following characters are used as
punctuators.
[ ] ( ) { } , ; : * … = #
Brackets [ ] opening and closing brackets
indicate single and multidimensional array
subscripts.
Parenthesis ( ) these indicate function calls
and function parameters.
18. Braces { } these indicates the start and end of a
compound statement.
Comma , it is used as separator in a function
argument list.
Semicolon ; it is used as statement
terminator.
Colon : it indicates a labeled statement.
Asterisk * it is used for pointer declaration.
19. Ellipsis … Ellipsis (...) are used in the formal
argument lists of the function prototype to
indicate a variable number of argument.
Equal to sign = It is used for variable
initialization and an assignment operator in
expressions.
Pound sign # this sign is used for
preprocessor directive.
20. Operators are tokens that trigger some
computation when applied to variables and
other objects in an expression.
Types of operators
1. Unary operators
2. Binary operators
3. Ternary operators
21. Unary operators are those operators that
require one operator to operate upon.
Examples :- +45, 5, -636,etc.,
23. Binary operators are those operators that
require two operands to operate upon.
Types of Binary operators
Arithmetic operators
+(addition) –(subtraction) *(multiplication)
/(division) %(reminder/modulus)
Logical operators
&& (Logical AND) || (Logical OR)
24. Relational operators
< (Less than)
<=(Less than or equal to)
>(Greater than)
>=(greater than or equal to)
== (equal to)
!= (not equal to)
25. Why include iostream.h ?
The header file iostream.h is included in every
C++ program to implement input/output
facilities. Input/output facilities are not
defined within C++ language, but rather are
implemented in a component of C++ standard
library, iostream.h which is I/O library.
26. A stream is simply a sequence of bytes.
The predefined stream objects for input, output,
error as follows:
1. Cin cin stands for console input.
2. Cout cout stands for console output.
3. Cerr cerr stands for console error.
27. Comments are pieces of codes that the
compiler discards or ignores or simply does
not execute.
Types of comments:
1. Single line comments
2. Multiline or block comments
28. These comments begins with // are single line
comments. The compiler simply ignores
everything following // in that same line
Example:-
#include<iostream.h>
Void main() // the program about addition.
29. The block comments, mark the beginning of
comment with /* and end with */. That means,
everything that falls between/* and*/ is
considered as comment.
Example:-
#include<iostream.h>
Void main() /*the program is about addition*/
30. Output operator “ << “
The output operator (“<<“), also called
stream insertion operator is used to direct a value
top standard output.
Input operator “ >> ”
The input operator(“>>“), also known as
stream extraction operator is used to read a value
from standard input.
31. Variable
A variable refers to a storage area whose
contents can vary during processing.
Cascading of I/O operators
The multiple use of input or output
operators(“>>”or”<<“) in one statement is
called cascading of I/O operators.
32. A part of the compiler’s job is to analyze the
program code for ‘correctness’. If the meaning
of the program is correct, then a compiler can
not detect errors.
Types of errors:
1. Syntax Errors
2. Semantic Errors
3. Type Errors
4. Run-time Errors
5. Logical Errors
33. Syntax Errors are occurred when rules of the
program is misused i.e., when grammatical rule
of C++ is violated.
Ex:- int a, b (semicolon missing)
Semantic Errors are occur when statements not
meaningful.
Ex:- x*y=z;
Type Errors are occurred when the data types
are misused.
Ex:-int a; a=123.56;
34. Run-time Errors are occurred at the time of
execution.
Logical Errors are occurred when the logic of
program is not proper.
Ex:- ctr=1;
While (ctr>10)
{
cout<<n*ctr;
ctr=ctr+1;
}