SlideShare uma empresa Scribd logo
1 de 68
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

Mais conteúdo relacionado

Mais procurados

Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++BalajiGovindan5
 
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-v3Simon Ritter
 
Lambdas And Streams Hands On Lab
Lambdas And Streams Hands On LabLambdas And Streams Hands On Lab
Lambdas And Streams Hands On LabSimon 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
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training reportRaushan Pandey
 
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.SivakumarSivakumar R D .
 
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 ontologyPieter Pauwels
 
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
 

Mais procurados (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...
 

Semelhante a Cpp tokens (2)

Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET FrameworkKamlesh Makvana
 
Computer programming and utilization 2110003
Computer programming and utilization 2110003Computer programming and utilization 2110003
Computer programming and utilization 2110003Juhi 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 Sguestd436758
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016Ankit Dubey
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016Ankit Dubey
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016Ankit Dubey
 
Python Basis Tutorial
Python Basis TutorialPython Basis Tutorial
Python Basis Tutorialmd sathees
 
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 intromarklaloo
 
PRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptxPRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptxSajalKesharwani2
 
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 CENTREjatin 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 Patilwidespreadpromotion
 

Semelhante a 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
 

Último

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 

Último (20)

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 

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