KEMBAR78
Cpp tokens (2) | PPT
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

Cpp tokens (2)

  • 1.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 3 Department of Information Technology C++ Timeline
  • 4.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 5 Department of Information Technology Keywords  Implement specific C++ language features  They are explicitly reserved identifiers
  • 6.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 11 Department of Information Technology Difference in C and C++
  • 12.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 12 Department of Information Technology Difference in C and C++
  • 13.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 13 Department of Information Technology Difference in C and C++
  • 14.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 14 Department of Information Technology Anonymous enums
  • 15.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 22 Department of Information Technology Property of Reference //Address of a & b are same
  • 23.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 23 Department of Information Technology Property of Reference
  • 24.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 25 Department of Information Technology Property of Reference
  • 26.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 28 Department of Information Technology Exercise 30
  • 29.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 29 Department of Information Technology Exercise Error
  • 30.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 30 Department of Information Technology Exercise
  • 31.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 31 Department of Information Technology Exercise Error
  • 32.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 32 Department of Information Technology Exercise Address of x
  • 33.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 33 Department of Information Technology Exercise Runtime Error.
  • 34.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 34 Department of Information Technology Exercise 10
  • 35.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 35 Department of Information Technology Exercise 30
  • 36.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 37 Department of Information Technology :: Scope Resolution operator  C++ is block structured language
  • 38.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 43 Department of Information Technology
  • 44.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 44 Department of Information Technology Home Work  Difference between delete and free.
  • 45.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 48 Department of Information Technology Exercise
  • 49.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 53 Department of Information Technology What about left alignment?
  • 54.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar Institute of Technology, Changa 56 Department of Information Technology cout ignores 0 after decimal places
  • 57.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 57 Department of Information Technology cout ignores 0 after decimal places
  • 58.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 58 Department of Information Technology Default no. of digits in float is 6.
  • 59.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 59 Department of Information Technology setprecision: specifies digits in floating point value
  • 60.
    Classified e-Material ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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 ©CopyrightsCharotar 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