SlideShare a Scribd company logo
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 1
Department of Information Technology
Tokens, Expressions and
Control Structure
CE 142: Object Oriented
Programming with C++
Kamlesh Makvana
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 2
Department of Information Technology
Course Outline
Sr
No.
Title of the unit
1 Tokens
2 Type Compatibility
3 Dynamic Initialization
4 Reference Variable
5 Operators in C++
6 Scope Resolution Operator
7 Expression and Control Structure
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 3
Department of Information Technology
C++ Timeline
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 4
Department of Information Technology
Tokens
 Tokens
 The Smallest Individual units in a program
 Types of Tokens
 Keywords
 Identifiers
 Constants
 String
 Operators
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 5
Department of Information Technology
Keywords
 Implement specific C++ language
features
 They are explicitly reserved identifiers
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 6
Department of Information Technology
Identifiers and Constants
 Name of Variable, function, array,
structure, Class etc.
 Rules to specify Identifiers
 Only alphabetic characters, digits,
underscores are permitted
 The name cannot start with a digit
 Uppercase and Lowercase letters are
distinct
 A declared keyword can not be used as a
variable name.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 7
Department of Information Technology
Identifiers and Constants
 The major difference between C and C++is the
limit on the length of variable.
 C-first 32 characters are recognized.
 C++- No Limit on length.
 Constants refers to fixed values.
 123 // Integer constant
 12.34 // floating point integer
 o37 // Octal integer
 0x2 // Hexadecimal integer
 “C++” // string constant
 ‘A’ // Character constant
 L ‘ab’ // Wide –character constant
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 8
Department of Information Technology
Basic Data Types
 Built-in Type
 Integral Type
 int, char
 Floating Type
 float, double
 Void
 Derived Type
 array, function, pointer, reference
 User defined type
 Structure, union, enumeration, class
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 9
Department of Information Technology
Void Data Type
 Where you are using void type?
 To specify return type of function.
 void function1()
 To represent empty argument list.
 int function2(Void)
 Declaration of Generic pointers.
 void *p
 But it can not be dereferenced!!!
 Assigning any pointer to void pointer is
allowed in both C and C++
 Can not Assign a void pointer to a non
void pointer without a type casting.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 10
Department of Information Technology
Enumerated Data Type
 Provides a way of attaching names to numbers.
 Automatically enumerates by assigning values
0,1,2, and so on.
 Syntax
 enum shape{circle, square, triangle};
 enum colour{red, blue, green, yellow}
 Difference in C and C++
 shape ellipse; //ellipse is of type shape
 couour background; //background is of type colour
 Anonymous enums
 enum{off,on};
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 11
Department of Information Technology
Difference in C and C++
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 12
Department of Information Technology
Difference in C and C++
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 13
Department of Information Technology
Difference in C and C++
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 14
Department of Information Technology
Anonymous enums
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 15
Department of Information Technology
Storage Classes
 Automatic (Local Variable)
 Lifetime: Function Block, Visibility: Local, Initial value:
Garbage, Keyword: auto
 External (Global Variable)
 Lifetime: Entire Program , Visibility: Global, Initial
value: 0, Keyword: extern
 Static
 Lifetime: Entire Program , Visibility: Local, Initial
value: 0, Keyword: static
 Register
 Lifetime: Function Block , Visibility: Local, Initial value:
Garbage, Keyword: Register
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 16
Department of Information Technology
Derived Data Type
 Array
 Group of Similar Data type.
 Char string[3]=“xyz”; // Valid in C
 Char string[3]=“xyz”; // Not Valid in C++
 Char string[4]=“xyz”; // OK;
 Pointer
 Example
 int *p,x;
 ip=&x;
 *ip=10;
 Constant Pointer and Pointer to constant
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 17
Department of Information Technology
Constant Pointer and Pointer to
constant
 Constant pointer
 int a=10,b=20;
 int * const p1=&a;
 p1=&b; //Error
 Pointer to constant
 int a=10,b=20;
 int const *p1=&a;
 p1=&b; //Valid
 b++; //Valid
 (*p1)++; //Error
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 18
Department of Information Technology
Symbolic Constant
 Two ways of creating symbolic constant
in C++
 Using the qualifier const
 const int size=10;
 const size=10;
 Defining a set of integer constant using
enum keyword.
 enum{x,y,z};=>const x=0;const y=1; const
z=2
 enum{x=100,y=50,z=200};
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 19
Department of Information Technology
Type Compatibility
 C++ is very strict with regard to type
compatibility to C.
 short int, int, long int
 Unsigned char, char, signed char
 int is not compactible with char.
 Function overloading.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 20
Department of Information Technology
Variable
 Variable Declaration
 Declare a variable as needed
 Dynamic initialization
 Int m=10;
 Int a=m*10;
 Lambda Expression (C++ 11).
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 21
Department of Information Technology
Reference Variable
 Alias for previously defined variable
 Syntax
 Data-type & reference-name=var-name
 Example:
 float total=100;
 float & sum=total;
 cout<<total<<sum;
 Total=total+10;
 cout<<total<<sum;
 Reference variable must initialize at time of
declaration.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 22
Department of Information Technology
Property of Reference
//Address of a & b are same
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 23
Department of Information Technology
Property of Reference
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 24
Department of Information Technology
Property of Reference
//Invalid Initialization of constant variable to non-constant reference
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 25
Department of Information Technology
Property of Reference
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 26
Department of Information Technology
Some Other Properties
 Reference variable can never be void
 void &ar = a; // it is not valid
 Once a reference is created, it cannot
be later made to reference another
object
 References cannot be NULL (Expect
Constant Reference.).
 A reference must be initialized when
declared
 Can not be declared pointer to
reference.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 27
Department of Information Technology
Reference Variable
 Following statements are also allowed.
 int x; int *p=&x; int & m=*p;
 const int & n=50;
 Application of Reference variable
 Passing an argument to functions.
 Manipulation of object without copying object
arguments back and forth.
 Creating reference variable for user defined
data types.
 Creating Delegates in C++.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 28
Department of Information Technology
Exercise
30
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 29
Department of Information Technology
Exercise
Error
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 30
Department of Information Technology
Exercise
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 31
Department of Information Technology
Exercise
Error
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 32
Department of Information Technology
Exercise
Address of x
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 33
Department of Information Technology
Exercise
Runtime Error.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 34
Department of Information Technology
Exercise
10
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 35
Department of Information Technology
Exercise
30
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 36
Department of Information Technology
Operators in C++
 :: Scope resolution operator
 ::* Pointer-to-member declaration
 ->* Pointer-to-member operator
 .* Pointer-to-member operator
 delete Memory release operator
 new Memory allocation operator
 setw Field width operators
 endl Line feed operator
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 37
Department of Information Technology
:: Scope Resolution operator
 C++ is block structured language
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 38
Department of Information Technology
:: Scope Resolution operator
 C++ is block structured language
 Two declaration of x refers to two
memory location containing different
values.
 In C global variable can not be accessed
from within the inner block
 :: operator solves this problem.
 Syntax:
 :: variable-name
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 39
Department of Information Technology
Memory Management Operator
 Memory allocation in C:
 malloc()
 calloc()
 realloc()
 Deallocated using the free() function.
 Memory allocation in C++
 using the new operator.
 Deallocated using the delete operator.
 Syntax:
 Pointer-variable=new data-type
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 40
Department of Information Technology
Memory Management Operator
 Example:
 int *p=NULL;
 p=new int;
 int *p=new int;
 Int *p=new int(5);
 Allocating memory to array
 Syntax:
 Variable-name=new data-type[size];
 Example
 int *p = new int[10]
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 41
Department of Information Technology
Memory Management Operator
 What happens if sufficient memory is
not available?
 exception of type std::bad_alloc
 new operator returns a null pointer
 Example:
int *p
p=new int;
If(!p)
{
cout<<“Allocation failed n”;
}
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 42
Department of Information Technology
Memory Management Operator
 Advantages
 Automatically computes size of data
 It automatically returns the correct pointer
type
 Possible to initialize the object while
creating the memory space
 New and delete operator can be
overloaded!!!
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 43
Department of Information Technology
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 44
Department of Information Technology
Home Work
 Difference between delete and free.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 45
Department of Information Technology
Exercise
How to create a dynamic array of pointers (to integers)
of size 10 using new in C++?
Hint: We can create a non-dynamic array using
int *arr[10]
int **arr = new int *[10];
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 46
Department of Information Technology
Exercise
Which of the following is true about new when
compared with malloc.
1)new is an operator, malloc is a function
2)new calls constructor, malloc doesn't
3)new returns appropriate pointer, malloc
returns void * and pointer needs to typecast to
appropriate type.
All
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 47
Department of Information Technology
Exercise
No Effect
What happens when delete is used for a
NULL pointer?
int *ptr = NULL;
delete ptr;
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 48
Department of Information Technology
Exercise
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 49
Department of Information Technology
Manipulators
 Stream I/O Library Header Files
 iostream
 contains basic information required for all stream I/O
operations
 iomanip
 contains information useful for performing formatted
I/O with parameterized stream manipulators
 fstream
 contains information for performing file I/O operations
 strstream
 contains information for performing in-memory I/O
operations.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 50
Department of Information Technology
C++ Stream I/O -- Stream Manipulators
 C++ provides various stream manipulators
that perform formatting tasks.
 Stream manipulators are defined in
<iomanip>
 These manipulators provide capabilities for
 setting field widths,
 setting precision,
 setting and unsetting format flags,
 flushing streams,
 inserting a "newline" and flushing output
stream,
 skipping whitespace in input stream
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 51
Department of Information Technology
setprecision (n)
 Select output precision, i.e., number of significant
digits to be printed.
 Example:
cout << setprecision (2) ; // two significant digits
setw (n)
 Specify the field width (Can be used on input or
output, but only applies to next insertion or
extraction).
 Example:
cout << setw (4) ; // field is four positions wide
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 52
Department of Information Technology
Does setw set width of all variables specified
in cout?
No, Default alignment is
right
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 53
Department of Information Technology
What about left alignment?
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 54
Department of Information Technology
Does left alignment aligns all the
variables specified in cout?
alignment: yes
setw: No
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 55
Department of Information Technology
Can more than one alignment is possible in
one cout cascading statement?
Yes
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 56
Department of Information Technology
cout ignores 0 after decimal places
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 57
Department of Information Technology
cout ignores 0 after decimal places
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 58
Department of Information Technology
Default no. of digits in float is 6.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 59
Department of Information Technology
setprecision: specifies digits in floating point value
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 60
Department of Information Technology
 Which manipulators is used to set only
decimal places digits?
 setprecision prefixed with fixed
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 61
Department of Information Technology
 showpoint
 when set, show trailing decimal point and zeros
 showpos
 when set, show the + sign before positive numbers
 fixed
 use fixed number of digits
 scientific
 use "scientific" notation
 adjustfield
 left
 use left justification
 right
 use right justification
 Internal
 left justify the sign, but right justify the value
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 62
Department of Information Technology
Manipulators as member function
.precision ( ) ;
 Select output precision, i.e., number of significant
digits to be printed.
 Example:
cout.precision (2) ; // two significant digits
.width ( ) ;
 Specify field width. (Can be used on input or
output, but only applies to next insertion or
extraction).
 Example:
cout.width (4) ; // field is four positions wide
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 63
Department of Information Technology
More Input Stream Member Functions
.get ( ) ;
Example:
char ch ;
ch = cin.get ( ) ; // gets one character from
keyboard
// & assigns it to the variable "ch"
.get (character) ;
Example:
char ch ;
cin.get (ch) ; // gets one character from
// keyboard & assigns to "ch"
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 64
Department of Information Technology
More Input Stream Member Functions
.get (array_name, max_size, delimiter) ;
Example:
char name[40] ;
cin.get (name, 40,’ ’) ; // Gets up to 39
characters
// and inserts a null at the end of the
// string "name". If a delimiter is
// found, the read terminates. The
// delimiter is not stored in the array,
// but it is left in the stream.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 65
Department of Information Technology
More Input Stream Member Functions
.getline (array_name, max_size, delimiter) ;
Example:
char name[40] ;
cin.getline (name, 40,’ ’) ; // Gets up to 39
characters
// and inserts a null at the end of the
// string "name". If a delimiter is
// found, the read terminates. The
// delimiter is not stored in the array,
// but it remove delimter from stream.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 66
Department of Information Technology
More Input Stream Member Functions
.ignore ( ) ;
Ex:
cin.ignore ( ) ; // gets and discards 1 character
cin.ignore (2) ; // gets and discards 2 characters
cin.ignore (80, 'n') ; // gets and discards up to 80
// characters or until "newline"
// character, whichever comes
// first
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 67
Department of Information Technology
Type Cast Operator
 C++ permits explicit type conversation of
variable or expression.
 Function call notation
 (type-name) expression // c notation
 Type-name (expression) //C++ notation
 Example:
 Avg=sum/(float)n; // c notation
 Avg=sum/float(n); // C++ notation
 Only use if expression is an simple identifiers
 Example:
 P=int * (q) //illegal
 P=(int *) q;
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 68
Department of Information Technology
Type Cast Operator
Alternatively use typedef to
create identifier
Example
 typedef int * int_pt;
 p=int_pt(q);
 ANSI C++ add following new cast
operators:
 const_cast
 static_cast
 dynamic_cast
 reinterpret_cast

More Related Content

What's hot

Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
BalajiGovindan5
 
Apouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-futureApouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-future
OUGTH Oracle User Group in Thailand
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3
Simon Ritter
 
C++ Course module
C++ Course moduleC++ Course module
C++ Course module
amandeep0224
 
Lec4
Lec4Lec4
Lec4
Saad Gabr
 
Handout#10
Handout#10Handout#10
Handout#10
Sunita Milind Dol
 
Lambdas And Streams Hands On Lab
Lambdas And Streams Hands On LabLambdas And Streams Hands On Lab
Lambdas And Streams Hands On Lab
Simon Ritter
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Akhil Mittal
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
shammi mehra
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training report
Raushan Pandey
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
Salahaddin University-Erbil
 
Android training in Nagpur
Android training in Nagpur Android training in Nagpur
Android training in Nagpur
letsleadsand
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Introduction to programming languages part 1
Introduction to programming languages   part 1Introduction to programming languages   part 1
Introduction to programming languages part 1
university of education,Lahore
 
A08
A08A08
A08
lksoo
 
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontologyEG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
Pieter Pauwels
 
Java chapter 3
Java   chapter 3Java   chapter 3
Java chapter 3
Mukesh Tekwani
 
Software Technologies for the Interoperability, Reusability and Adaptability...
Software Technologies for the Interoperability,  Reusability and Adaptability...Software Technologies for the Interoperability,  Reusability and Adaptability...
Software Technologies for the Interoperability, Reusability and Adaptability...
Daniele Gianni
 

What's hot (20)

Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Apouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-futureApouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-future
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3
 
C++ Course module
C++ Course moduleC++ Course module
C++ Course module
 
Lec4
Lec4Lec4
Lec4
 
Handout#10
Handout#10Handout#10
Handout#10
 
Lambdas And Streams Hands On Lab
Lambdas And Streams Hands On LabLambdas And Streams Hands On Lab
Lambdas And Streams Hands On Lab
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training report
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 
Android training in Nagpur
Android training in Nagpur Android training in Nagpur
Android training in Nagpur
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
Introduction to programming languages part 1
Introduction to programming languages   part 1Introduction to programming languages   part 1
Introduction to programming languages part 1
 
A08
A08A08
A08
 
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontologyEG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
 
Java chapter 3
Java   chapter 3Java   chapter 3
Java chapter 3
 
Software Technologies for the Interoperability, Reusability and Adaptability...
Software Technologies for the Interoperability,  Reusability and Adaptability...Software Technologies for the Interoperability,  Reusability and Adaptability...
Software Technologies for the Interoperability, Reusability and Adaptability...
 

Similar to Cpp tokens (2)

Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
Kamlesh Makvana
 
BIT211_2.pdf
BIT211_2.pdfBIT211_2.pdf
BIT211_2.pdf
Sameer607695
 
Computer programming and utilization 2110003
Computer programming and utilization 2110003Computer programming and utilization 2110003
Computer programming and utilization 2110003
Juhi Shah
 
R05010106 C P R O G R A M M I N G A N D D A T A S T R U C T U R E S
R05010106  C  P R O G R A M M I N G   A N D   D A T A  S T R U C T U R E SR05010106  C  P R O G R A M M I N G   A N D   D A T A  S T R U C T U R E S
R05010106 C P R O G R A M M I N G A N D D A T A S T R U C T U R E S
guestd436758
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
Ankit Dubey
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
Ankit Dubey
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
Ankit Dubey
 
Python basics
Python basicsPython basics
Python basics
Jyoti shukla
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
emailharmeet
 
APS PGT Computer Science SylIabus
APS PGT Computer Science SylIabusAPS PGT Computer Science SylIabus
APS PGT Computer Science SylIabus
Knowledge Center Computer
 
Python Basis Tutorial
Python Basis TutorialPython Basis Tutorial
Python Basis Tutorial
md sathees
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
aptechsravan
 
Learning c++
Learning c++Learning c++
Learning c++
Bala Lavanya
 
c.ppt
c.pptc.ppt
devLink - What's New in C# 4?
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?
Kevin Pilch
 
C++ programming intro
C++ programming introC++ programming intro
C++ programming intro
marklaloo
 
PRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptxPRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptx
SajalKesharwani2
 
day3.pdf
day3.pdfday3.pdf
day3.pdf
VikasPs6
 
C Programming Training In Ambala ! BATRA COMPUTER CENTRE
C Programming Training In Ambala ! BATRA COMPUTER CENTREC Programming Training In Ambala ! BATRA COMPUTER CENTRE
C Programming Training In Ambala ! BATRA COMPUTER CENTRE
jatin batra
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
widespreadpromotion
 

Similar to Cpp tokens (2) (20)

Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
BIT211_2.pdf
BIT211_2.pdfBIT211_2.pdf
BIT211_2.pdf
 
Computer programming and utilization 2110003
Computer programming and utilization 2110003Computer programming and utilization 2110003
Computer programming and utilization 2110003
 
R05010106 C P R O G R A M M I N G A N D D A T A S T R U C T U R E S
R05010106  C  P R O G R A M M I N G   A N D   D A T A  S T R U C T U R E SR05010106  C  P R O G R A M M I N G   A N D   D A T A  S T R U C T U R E S
R05010106 C P R O G R A M M I N G A N D D A T A S T R U C T U R E S
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
 
Python basics
Python basicsPython basics
Python basics
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
APS PGT Computer Science SylIabus
APS PGT Computer Science SylIabusAPS PGT Computer Science SylIabus
APS PGT Computer Science SylIabus
 
Python Basis Tutorial
Python Basis TutorialPython Basis Tutorial
Python Basis Tutorial
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
Learning c++
Learning c++Learning c++
Learning c++
 
c.ppt
c.pptc.ppt
c.ppt
 
devLink - What's New in C# 4?
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?
 
C++ programming intro
C++ programming introC++ programming intro
C++ programming intro
 
PRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptxPRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptx
 
day3.pdf
day3.pdfday3.pdf
day3.pdf
 
C Programming Training In Ambala ! BATRA COMPUTER CENTRE
C Programming Training In Ambala ! BATRA COMPUTER CENTREC Programming Training In Ambala ! BATRA COMPUTER CENTRE
C Programming Training In Ambala ! BATRA COMPUTER CENTRE
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
 

Recently uploaded

Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
Aditya Rajan Patra
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 

Recently uploaded (20)

Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 

Cpp tokens (2)

  • 1. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 1 Department of Information Technology Tokens, Expressions and Control Structure CE 142: Object Oriented Programming with C++ Kamlesh Makvana
  • 2. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 2 Department of Information Technology Course Outline Sr No. Title of the unit 1 Tokens 2 Type Compatibility 3 Dynamic Initialization 4 Reference Variable 5 Operators in C++ 6 Scope Resolution Operator 7 Expression and Control Structure
  • 3. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 3 Department of Information Technology C++ Timeline
  • 4. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 4 Department of Information Technology Tokens  Tokens  The Smallest Individual units in a program  Types of Tokens  Keywords  Identifiers  Constants  String  Operators
  • 5. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 5 Department of Information Technology Keywords  Implement specific C++ language features  They are explicitly reserved identifiers
  • 6. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 6 Department of Information Technology Identifiers and Constants  Name of Variable, function, array, structure, Class etc.  Rules to specify Identifiers  Only alphabetic characters, digits, underscores are permitted  The name cannot start with a digit  Uppercase and Lowercase letters are distinct  A declared keyword can not be used as a variable name.
  • 7. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 7 Department of Information Technology Identifiers and Constants  The major difference between C and C++is the limit on the length of variable.  C-first 32 characters are recognized.  C++- No Limit on length.  Constants refers to fixed values.  123 // Integer constant  12.34 // floating point integer  o37 // Octal integer  0x2 // Hexadecimal integer  “C++” // string constant  ‘A’ // Character constant  L ‘ab’ // Wide –character constant
  • 8. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 8 Department of Information Technology Basic Data Types  Built-in Type  Integral Type  int, char  Floating Type  float, double  Void  Derived Type  array, function, pointer, reference  User defined type  Structure, union, enumeration, class
  • 9. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 9 Department of Information Technology Void Data Type  Where you are using void type?  To specify return type of function.  void function1()  To represent empty argument list.  int function2(Void)  Declaration of Generic pointers.  void *p  But it can not be dereferenced!!!  Assigning any pointer to void pointer is allowed in both C and C++  Can not Assign a void pointer to a non void pointer without a type casting.
  • 10. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 10 Department of Information Technology Enumerated Data Type  Provides a way of attaching names to numbers.  Automatically enumerates by assigning values 0,1,2, and so on.  Syntax  enum shape{circle, square, triangle};  enum colour{red, blue, green, yellow}  Difference in C and C++  shape ellipse; //ellipse is of type shape  couour background; //background is of type colour  Anonymous enums  enum{off,on};
  • 11. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 11 Department of Information Technology Difference in C and C++
  • 12. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 12 Department of Information Technology Difference in C and C++
  • 13. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 13 Department of Information Technology Difference in C and C++
  • 14. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 14 Department of Information Technology Anonymous enums
  • 15. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 15 Department of Information Technology Storage Classes  Automatic (Local Variable)  Lifetime: Function Block, Visibility: Local, Initial value: Garbage, Keyword: auto  External (Global Variable)  Lifetime: Entire Program , Visibility: Global, Initial value: 0, Keyword: extern  Static  Lifetime: Entire Program , Visibility: Local, Initial value: 0, Keyword: static  Register  Lifetime: Function Block , Visibility: Local, Initial value: Garbage, Keyword: Register
  • 16. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 16 Department of Information Technology Derived Data Type  Array  Group of Similar Data type.  Char string[3]=“xyz”; // Valid in C  Char string[3]=“xyz”; // Not Valid in C++  Char string[4]=“xyz”; // OK;  Pointer  Example  int *p,x;  ip=&x;  *ip=10;  Constant Pointer and Pointer to constant
  • 17. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 17 Department of Information Technology Constant Pointer and Pointer to constant  Constant pointer  int a=10,b=20;  int * const p1=&a;  p1=&b; //Error  Pointer to constant  int a=10,b=20;  int const *p1=&a;  p1=&b; //Valid  b++; //Valid  (*p1)++; //Error
  • 18. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 18 Department of Information Technology Symbolic Constant  Two ways of creating symbolic constant in C++  Using the qualifier const  const int size=10;  const size=10;  Defining a set of integer constant using enum keyword.  enum{x,y,z};=>const x=0;const y=1; const z=2  enum{x=100,y=50,z=200};
  • 19. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 19 Department of Information Technology Type Compatibility  C++ is very strict with regard to type compatibility to C.  short int, int, long int  Unsigned char, char, signed char  int is not compactible with char.  Function overloading.
  • 20. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 20 Department of Information Technology Variable  Variable Declaration  Declare a variable as needed  Dynamic initialization  Int m=10;  Int a=m*10;  Lambda Expression (C++ 11).
  • 21. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 21 Department of Information Technology Reference Variable  Alias for previously defined variable  Syntax  Data-type & reference-name=var-name  Example:  float total=100;  float & sum=total;  cout<<total<<sum;  Total=total+10;  cout<<total<<sum;  Reference variable must initialize at time of declaration.
  • 22. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 22 Department of Information Technology Property of Reference //Address of a & b are same
  • 23. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 23 Department of Information Technology Property of Reference
  • 24. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 24 Department of Information Technology Property of Reference //Invalid Initialization of constant variable to non-constant reference
  • 25. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 25 Department of Information Technology Property of Reference
  • 26. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 26 Department of Information Technology Some Other Properties  Reference variable can never be void  void &ar = a; // it is not valid  Once a reference is created, it cannot be later made to reference another object  References cannot be NULL (Expect Constant Reference.).  A reference must be initialized when declared  Can not be declared pointer to reference.
  • 27. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 27 Department of Information Technology Reference Variable  Following statements are also allowed.  int x; int *p=&x; int & m=*p;  const int & n=50;  Application of Reference variable  Passing an argument to functions.  Manipulation of object without copying object arguments back and forth.  Creating reference variable for user defined data types.  Creating Delegates in C++.
  • 28. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 28 Department of Information Technology Exercise 30
  • 29. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 29 Department of Information Technology Exercise Error
  • 30. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 30 Department of Information Technology Exercise
  • 31. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 31 Department of Information Technology Exercise Error
  • 32. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 32 Department of Information Technology Exercise Address of x
  • 33. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 33 Department of Information Technology Exercise Runtime Error.
  • 34. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 34 Department of Information Technology Exercise 10
  • 35. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 35 Department of Information Technology Exercise 30
  • 36. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 36 Department of Information Technology Operators in C++  :: Scope resolution operator  ::* Pointer-to-member declaration  ->* Pointer-to-member operator  .* Pointer-to-member operator  delete Memory release operator  new Memory allocation operator  setw Field width operators  endl Line feed operator
  • 37. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 37 Department of Information Technology :: Scope Resolution operator  C++ is block structured language
  • 38. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 38 Department of Information Technology :: Scope Resolution operator  C++ is block structured language  Two declaration of x refers to two memory location containing different values.  In C global variable can not be accessed from within the inner block  :: operator solves this problem.  Syntax:  :: variable-name
  • 39. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 39 Department of Information Technology Memory Management Operator  Memory allocation in C:  malloc()  calloc()  realloc()  Deallocated using the free() function.  Memory allocation in C++  using the new operator.  Deallocated using the delete operator.  Syntax:  Pointer-variable=new data-type
  • 40. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 40 Department of Information Technology Memory Management Operator  Example:  int *p=NULL;  p=new int;  int *p=new int;  Int *p=new int(5);  Allocating memory to array  Syntax:  Variable-name=new data-type[size];  Example  int *p = new int[10]
  • 41. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 41 Department of Information Technology Memory Management Operator  What happens if sufficient memory is not available?  exception of type std::bad_alloc  new operator returns a null pointer  Example: int *p p=new int; If(!p) { cout<<“Allocation failed n”; }
  • 42. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 42 Department of Information Technology Memory Management Operator  Advantages  Automatically computes size of data  It automatically returns the correct pointer type  Possible to initialize the object while creating the memory space  New and delete operator can be overloaded!!!
  • 43. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 43 Department of Information Technology
  • 44. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 44 Department of Information Technology Home Work  Difference between delete and free.
  • 45. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 45 Department of Information Technology Exercise How to create a dynamic array of pointers (to integers) of size 10 using new in C++? Hint: We can create a non-dynamic array using int *arr[10] int **arr = new int *[10];
  • 46. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 46 Department of Information Technology Exercise Which of the following is true about new when compared with malloc. 1)new is an operator, malloc is a function 2)new calls constructor, malloc doesn't 3)new returns appropriate pointer, malloc returns void * and pointer needs to typecast to appropriate type. All
  • 47. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 47 Department of Information Technology Exercise No Effect What happens when delete is used for a NULL pointer? int *ptr = NULL; delete ptr;
  • 48. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 48 Department of Information Technology Exercise
  • 49. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 49 Department of Information Technology Manipulators  Stream I/O Library Header Files  iostream  contains basic information required for all stream I/O operations  iomanip  contains information useful for performing formatted I/O with parameterized stream manipulators  fstream  contains information for performing file I/O operations  strstream  contains information for performing in-memory I/O operations.
  • 50. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 50 Department of Information Technology C++ Stream I/O -- Stream Manipulators  C++ provides various stream manipulators that perform formatting tasks.  Stream manipulators are defined in <iomanip>  These manipulators provide capabilities for  setting field widths,  setting precision,  setting and unsetting format flags,  flushing streams,  inserting a "newline" and flushing output stream,  skipping whitespace in input stream
  • 51. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 51 Department of Information Technology setprecision (n)  Select output precision, i.e., number of significant digits to be printed.  Example: cout << setprecision (2) ; // two significant digits setw (n)  Specify the field width (Can be used on input or output, but only applies to next insertion or extraction).  Example: cout << setw (4) ; // field is four positions wide
  • 52. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 52 Department of Information Technology Does setw set width of all variables specified in cout? No, Default alignment is right
  • 53. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 53 Department of Information Technology What about left alignment?
  • 54. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 54 Department of Information Technology Does left alignment aligns all the variables specified in cout? alignment: yes setw: No
  • 55. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 55 Department of Information Technology Can more than one alignment is possible in one cout cascading statement? Yes
  • 56. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 56 Department of Information Technology cout ignores 0 after decimal places
  • 57. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 57 Department of Information Technology cout ignores 0 after decimal places
  • 58. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 58 Department of Information Technology Default no. of digits in float is 6.
  • 59. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 59 Department of Information Technology setprecision: specifies digits in floating point value
  • 60. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 60 Department of Information Technology  Which manipulators is used to set only decimal places digits?  setprecision prefixed with fixed
  • 61. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 61 Department of Information Technology  showpoint  when set, show trailing decimal point and zeros  showpos  when set, show the + sign before positive numbers  fixed  use fixed number of digits  scientific  use "scientific" notation  adjustfield  left  use left justification  right  use right justification  Internal  left justify the sign, but right justify the value
  • 62. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 62 Department of Information Technology Manipulators as member function .precision ( ) ;  Select output precision, i.e., number of significant digits to be printed.  Example: cout.precision (2) ; // two significant digits .width ( ) ;  Specify field width. (Can be used on input or output, but only applies to next insertion or extraction).  Example: cout.width (4) ; // field is four positions wide
  • 63. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 63 Department of Information Technology More Input Stream Member Functions .get ( ) ; Example: char ch ; ch = cin.get ( ) ; // gets one character from keyboard // & assigns it to the variable "ch" .get (character) ; Example: char ch ; cin.get (ch) ; // gets one character from // keyboard & assigns to "ch"
  • 64. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 64 Department of Information Technology More Input Stream Member Functions .get (array_name, max_size, delimiter) ; Example: char name[40] ; cin.get (name, 40,’ ’) ; // Gets up to 39 characters // and inserts a null at the end of the // string "name". If a delimiter is // found, the read terminates. The // delimiter is not stored in the array, // but it is left in the stream.
  • 65. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 65 Department of Information Technology More Input Stream Member Functions .getline (array_name, max_size, delimiter) ; Example: char name[40] ; cin.getline (name, 40,’ ’) ; // Gets up to 39 characters // and inserts a null at the end of the // string "name". If a delimiter is // found, the read terminates. The // delimiter is not stored in the array, // but it remove delimter from stream.
  • 66. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 66 Department of Information Technology More Input Stream Member Functions .ignore ( ) ; Ex: cin.ignore ( ) ; // gets and discards 1 character cin.ignore (2) ; // gets and discards 2 characters cin.ignore (80, 'n') ; // gets and discards up to 80 // characters or until "newline" // character, whichever comes // first
  • 67. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 67 Department of Information Technology Type Cast Operator  C++ permits explicit type conversation of variable or expression.  Function call notation  (type-name) expression // c notation  Type-name (expression) //C++ notation  Example:  Avg=sum/(float)n; // c notation  Avg=sum/float(n); // C++ notation  Only use if expression is an simple identifiers  Example:  P=int * (q) //illegal  P=(int *) q;
  • 68. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 68 Department of Information Technology Type Cast Operator Alternatively use typedef to create identifier Example  typedef int * int_pt;  p=int_pt(q);  ANSI C++ add following new cast operators:  const_cast  static_cast  dynamic_cast  reinterpret_cast