Operators in C++: Scope Resolution, Member Dereferencing, and Memory Management

topic operators in c n.w
1 / 18
Embed
Share

"Explore the essential operators in C++ such as Scope Resolution, Member Dereferencing, and Memory Management. Learn how variables' scopes are resolved, accessing class members, and managing memory efficiently with new and delete operators." (298 characters)

  • C++
  • Operators
  • Scope Resolution
  • Memory Management
  • Member Dereferencing

Uploaded on | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Topic Operators in C++ Presented by Thatte R. B. Department of Comp. Sci. & IT. Deogiri College, Aurangabad

  2. Operators in C++

  3. Operators in C++ Operator :: ::* ->* .* delete Meaning Scope Resolution Pointer to member declarator Pointer to member operator Pointer to member operator Memory release operator endl Line feed operator new Memory allocation operator setw Field width operator

  4. Scope Resolution Operator The same variable name can be used to have different meanings in different blocks. The scope of variable extends from the points 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. Consider the following segment of a program.

  5. Scope Resolution Operator { } . .. { } . { int x=10; . . int x=10; .. .. { int x=1; . . Block 1 Block 2 int x=1; . . } }

  6. Scope Resolution Operator In C global version of a variable can not be accessed from within the inner block . C++ resolves this problem by introducing a new operator :: called the scope resolution operator This can be used to uncover a hidden variable . It takes the following form : :: variable-name This operator allows access to the global version of a variable.

  7. Scope Resolution Operator Let see Program using Scope Resolution operator

  8. Member Dereferencing Operators Operator Function ::* .* To declare a pointer to a member of a class To access a member using object name and a pointer to that member ->* To access a member using a pointer to the object and a pointer to that member

  9. Memory Management Operators C++ defines two unary operators new and delete that perform the task of allocating and freeing the memory in a better and easier way. Since these operators manipulate memory on the free store , they are known as free store operators. An object can be created by using new and destroyed by using delete operator as an when required. The new operator can be used to create objects of any type . It takes the following form. Pointer-variable = new data-type

  10. Memory Management Operators p=new int ; We can also initialize the memory using the new operator q=new float ; Pointer-variable = new data-type (value); Combine declaration of pointer and their assignment as follows int * p=new int (25); int * q=new float (7.5); int * p=new int ; new can be used to create a memory space for any data type including user defined data types such as arrays , structures and classes int * q=new float ; int * p=new int[10]; * p= 25 ; * q= 7.5 ;

  11. Memory Management Operators When creating multi-dimensional arrays with new , all the array sizes must be supplied. array_ptr=new int[3][5][4]; // legal // legal array_ptr=new int[m][5][4]; array_ptr=new int[3][5][]; // illegal array_ptr=new int[][5][4]; // illegal

  12. Memory Management Operators When a data object is no longer needed , it is destroyed to release the memory space for reuse. The general form is delete pointer-variable; delete p; delete q; If we want to free a dynamically allocated array, we must use the following form of delete Delete[size] pointer-variable; delete [ ]p;

  13. Manipulators Manipulators are operators that are used to format the data display. The most commonly used manipulators are endl and setw. endl manipulator causes a linefeed to be inserted. It has the same effect as \n . It should appear rather as follows . . cout<< m= <<m<<endl << n= <<n<<endl << p= <<p<<endl; m= 1 2 3 4 m= 1 2 3 4 n= 1 2 1 2 n= p= 1 2 3 1 2 3 p= This form is not the ideal output

  14. setw The setw manipulator does this job. It is used as follows cout<<setw(5)<<sum<<endl ; 1 2 3

  15. Manipulators Let see Program using manipulators

  16. Type cast Operators C++ permits explicit type conversions using the type cast operator. Following two conversions are equivalent: (type-name)expression type-name(expression) // C notation // C++ notation Example Average = sum/(float)n ; Average = sum/ float(n); // C notation // C notation

  17. Expressions and Implicit conversions Expressions may be of four types, namely, constant expressions, integral expressions, float expressions and pointer expressions. Constant expressions Constant expressions Constant expressions Constant expressions Consist of only constant values Produce integer results after implementing all the automatic & explicit type conversions. After all conversions, produce floating point result. Produce address values. Examples: Examples: Examples: Examples: 15 34+4/2.0 x m m*n-5 5+int(2.0) x+y x*y/8 5+float(4) &m ptr ptr+1

  18. Water-fall model of type conversion short char int unsigned long int unsigned long int float double long double

Related


More Related Content