Learners Support Publications www.lsp4you.com Operators.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Numeric Types, Expressions, and Output ROBERT REAVES.
Informática II Prof. Dr. Gustavo Patiño MJ
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 14: Overloading and Templates
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT], MPhil (Comp. Sci), PGDCA, ADCA, Dc. Sc. & Engg.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Pointers Applications
Data Types.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
Operator Overloading and Type Conversions
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Stack and Heap Memory Stack resident variables include:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
STRUCTURE OF A C++ PROGRAM. It is a common practice to organize a program into three separate files. The class declarations are placed in a header file.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Learners Support Publications Constructors and Destructors.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Advanced Programming in C
Constructors and Destructors
EGR 2261 Unit 11 Pointers and Dynamic Variables
Standard Version of Starting Out with C++, 4th Edition
Operators in c++.
Introduction to Programming
Introduction to C++.
Dynamic Memory Allocation Reference Variables
Operators.
Constructors and Destructors
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Standard Version of Starting Out with C++, 4th Edition
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Learners Support Publications Operators

Objectives of this session Operators in C++ Operators in C++ Scope Resolution Operator Scope Resolution Operator Memory Management Operator Memory Management Operator Manipulators Manipulators Type Cast Operator Type Cast Operator

Learners Support Publications Operators in C++ New operators in C++ : o << Insertion Operator o >> Extraction Operator o : : Scope Resolution Operator o : : * Pointer-to-member declaration o ->* Pointer-to-member Operator o.* Pointer-to-member Operator o delete Memory Release Operator o endl Line Feed Operator o new Memory Allocation Operator o setw Field Width Operator All C operators are valid in C++

Learners Support Publications Scope Resolution Operator C++ is a block structured language. The scope of a variable extends from the point of its declaration till the end of the block containing the declaration. A variable declared inside a block is said to be local to that block. ……… { int x = 10; ……… } { int x = 1; ……… }

Learners Support Publications Scope Resolution Operator Blocks in C++ are often nested. In C, the global version of a variable can not be accessed from within the inner block. C++ resolved this problem with the use of the scope resolution operator ( :: ). ……… { int x = 10; ……… Block1 ……… { int x = 1; ……… Block 2 ……… } } continue…

Learners Support Publications Scope Resolution Operator The scope resolution operator ( :: ) can be used to uncover a hidden variable. : : variable-name This operator allows access to the global version of a variable. continue…

Learners Support Publications Scope Resolution Operator continue… #include<iostream.h>#include<conio.h> int m = 10; void main( ) { int m = 20; int m = 20; clrscr( ); clrscr( ); cout << "m_local = " << m << "\n"; cout << "m_local = " << m << "\n"; cout << "m_global = " <<::m << "\n"; cout << "m_global = " <<::m << "\n"; getch( ); getch( );} m_local = 20 m_global = 10 m_local = 20 m_global = 10

Learners Support Publications Memory Management Operators malloc( ) and calloc( ) functions are used to allocate memory dynamically at run time. The function free( ) to free dynamically the allocated memory. The unary operators new and delete perform the task of allocating and freeing the memory. C & C++

Learners Support Publications Memory Management Operators o new to create an object o delete to destroy an object A data object created inside a block with new, will remain in existence until it is explicitly destroyed by using delete. Thus the life time of an object is directly under our control and is unrelated to the block structure of the program. continue…

Learners Support Publications The data-type may be any valid data type Memory Management Operators o new to create an object continue… pointer-variable is a pointer of type data-type The new operator allocates sufficient memory to hold a data object of type data-type and returns the address of the object pointer-variable = new data-type; The pointer-variable holds the address of the memory space allocated

Learners Support Publications Memory Management Operators o pointer-variable = new data-type; p = new int; // p is a pointer of type int q = new float; // q is a pointer of type float Here p and q must have already been declared as pointers of appropriate types. Alternatively, we can combine the declaration of pointers and their assignments as: int *p = new int; float *q = new float; continue…

Learners Support Publications Memory Management Operators int *p = new int; float *q = new float; *p = 25; // assign 25 to the newly created int object *q = 7.5; // assign 7.5 to the newly created float object continue… pointer-variable = new data-type (value); int *p = new int ( 25 ); float *q = new float ( 7.5 );

Learners Support Publications Memory Management Operators new can be used to create a memory space for any data type including user-defined types such as arrays, structures and classes. continue… pointer-variable = new data-type [size]; int *p = new int [10]; When creating multi-dimensional arrays with new, all the array sizes must be supplied. array_ptr = new int[3][5][4]; //legal array_ptr = new int[m][5][4]; //legal array_ptr = new int[3][5][ ]; //illegal array_ptr = new int[ ][5][4]; //illegal

Learners Support Publications Memory Management Operators o delete to destroy an object continue… delete pointer-variable; When a data object is no longer needed, it is destroyed to release the memory space for reuse. delete p; delete q; delete [ size ] pointer-variable; The size specifies the number of elements in the array to be freed. delete [ ]p; // delete the entire array pointed to by p

Learners Support Publications Memory Management Operators If sufficient memory is not available for allocation, malloc( ) and new returns a null pointer. continue… …… p = new int; if (!p) { cout << “Allocation failed \n”; } …… p = new int; if (!p) { cout << “Allocation failed \n”; } ……

Learners Support Publications Memory Management Operators Advantages of new over malloc( ): o It automatically computes the size of the data object. No need to use sizeof operator. o It automatically returns the correct pointer type. No need to use type cast. o It is possible to initialize the object while creating the memory space. o Like any operator, new and delete can be overloaded. continue…

Learners Support Publications Manipulators Manipulators are operators that are used to format the data display. Commonly used manipulators are: o endl // causes a line feed when used in an // output statement o setw // to specify field width and force the // data to be printed right-justified // data to be printed right-justified

Learners Support Publications Manipulators continue… #include<iostream.h>#include<conio.h>#include<iomanip.h> void main( ) { int m, n, p; int m, n, p; m = 2597; m = 2597; n = 14; n = 14; p = 175; p = 175; clrscr( ); clrscr( ); cout <<setw(10) << "First = " <<setw(10) << m << endl cout <<setw(10) << "First = " <<setw(10) << m << endl <<setw(10) << "Second = " << setw(10) << n << endl <<setw(10) << "Second = " << setw(10) << n << endl <<setw(10) << "Third = " << setw(10) << p << endl; <<setw(10) << "Third = " << setw(10) << p << endl; getch( ); getch( );} First = 2597 First = 2597 Second = 14 Third = 175 Third = 175 First = 2597 First = 2597 Second = 14 Third = 175 Third = 175

Learners Support Publications Manipulators We can create our own manipulators as follows: # include # include ostream & symbol (ostream & output) { output << “\tRs. “; return output; return output;} continue…

Learners Support Publications Manipulators continue… #include<iostream.h>#include<conio.h> ostream & symbol (ostream & output) { output << "Rs. "; output << "Rs. "; return output; return output;} void main( ) { clrscr( ); clrscr( ); cout << symbol << "5,000/-" <<endl; cout << symbol << "5,000/-" <<endl; getch( ); getch( );}

Learners Support Publications Type Cast Operator C++ permit explicit type conversion of variables or expressions using the type cast operator. o (type-name) expression // C notation o type-name ( expression ) // C++ notation // like a function call // like a function call // notation // notation eg:- average = sum /(float) i; // C notation average = sum / float(i); // C++ notation average = sum / float(i); // C++ notation

Learners Support Publications Type Cast Operator p = int * ( q ); // is illegal p = ( int * ) q; // is legal Alternatively, we can use typedef to create an identifier of the required type. typedef int * int_pt; p = int_pt ( q ); continue… The type name should be an identifier

Learners Support Publications Thank You