انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Operators in C Plus plus

الكلية كلية العلوم للبنات     القسم قسم الحاسبات     المرحلة 2
أستاذ المادة مهدي عبد سلمان المسلماوي       3/29/2011 9:58:17 AM

operators in c++

 

in addition of the known operators, we have already seen two operators, namely, the insertion operator < < , and the extraction operator > > . other new operators are:

 

: :               scope resolution operator

 

::*              pointer-to-member declarator

 

-> *            pointer-to-member operator

 

.*                pointer-to-member operator

 

deleting memory release operator

 

endl        line feed operator

 

new       memory allocation operator

 

setw        field width operator

 

in addition, c++ also allows us to provide new definitions to some of the built in operators. that is, we can give several meanings to an operator, depending upon the types of arguments used. this process is known as operator overloading.

 

 

scope resolution operator

 

c++ is a block-structured language. blocks and scopes can be used in constructing programs. we know that the same variables name can be used to have different meanings in different blocks. the scope of the 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. example:

 

…….

 

……

 

{

 

                      int x = 10

 

                      ……….

 

                      ………

 

}

 

………

 

………

 

{                 

 

                      int  x = 1

 

                      ……….

 

                      ………

 

}

 

the two declaration of  x refer to two different memory locations containing different values. another example:

 

                      ……..

 

                      ……..

 

                        {

 

                                              int x = 10

 

                                              …….

 

                                              ……

 

                                                {

 

                                                                      int x = 1

 

                                                                      ……….              block2      block1

 

                                                                      ………

 

                                              }

 

                      ………

 

                      }

 

 

block2 is contained in block1. none that a declaration in an inner block hides a declaration of the same variable in an outer block and therefore, each declaration of x causes it to refer to a different data object. within the inner block, the variable x will refer to the data object declared therein.

 

scope-resolution operator used to uncover a hidden variable. it takes the following form:

 

 

                                                                                              : : variable-name

 

this operator allows access to the global version of variable. program bellow illustrates this feature:

 

/////////////////////[ scope resolution operator]////////////////////

 

#include < iostream.h>

 

int m = 10                                                         //global  m

 

main( )

 

{

 

                    int m = 20                                   //m redeclared, local to main

 

                    {

 

                                                      int k = m

 

                                                      int m = 30 //m again, local to inner block

 

                                                      cout  < < "we are in inner block to \n"

 

                                                      cout  < < "k=" < < k < < "\n"

 

                                                      cout  < < "m=" < < m < < "\n"

 

                                                      cout  < < ": :m = " < < : :m  < < "\n"

 

                        }

 

                          cout < < "\nwe are in outer block \n"

 

                        cout  < < "m = " < < m < < "\n"

 

                        cout  < < ": : m = " < < : :m < < "\n"

 

}

 

/////////////////////////< program 3.1 > /////////////////////////////

 

the output of the program would be:

 

                      we are in inner block

 

                      k = 20

 

                      m = 30

 

                      : : m = 10

 

 

                      we are in outer block

 

                      m = 20

 

                      : :m=10

 

memory management operators

 

the new operator can be used to create objects of any type. it takes the following form:

 

pointer-variable = new data-type

 

here, 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. example:

 

                      int *p= new int

 

                      float *q = new float

 

when a data object is no longer needed, it is destroyed to release the memory space for reuse.

 

the general form of its use is

 

                                                                                              deleting pointer-variable

 

the pointer variable is the pointer that points to a data object created with new. example:

 

                                                                                              deleting p

 

                                                                                              deleting q

 

 


manipulators 

 

manipulators are operators are used to format the data display. the most commonly used manipulators are endl and setw.

 

endl has same effect as using the newline character "\n". for example:

 

 

                                              ………

 

                                              cout          < < "m = " < < m < < endl

 

                                                                      < < "n = "   < < n < < endl

 

                                                                      < < "p = "  < < p < < endl

 

                                              ………

 

                                              ………

 

would cause the three lines of output.

 

m =

 

2

 

5

 

9

 

7

 

n =

 

1

 

4

 

 

 

p =

 

1

 

7

 

5

 

 

note that this form is not the ideal output. it should appear as follow:

 

                                              m = 2597

 

                                              n  =        14

 

                                              p  =    175

 

here, the numbers are right justified. its is used as follows:

 

                                              cout < < setw(5) < < sum < < endl

 

program bellow illustrates theuse of endl and setw:

 

//////////////////////////< use of manipulators > /////////////////////

 

#include < iostream.h>

 

#include < iomanip.h>

 

main ( )

 

{             

 

                  int basic = 950, allowance = 95 , total = 1045

 

                    cout  < < setw(10) < < "basic" < < setw(10)< < basic < < endl

 

                                      < < setw(10) < < "allowance " < < setw(10)< < allowance< < endl

 

                                      < < setw(10) < < "total"    < < setw(10) < < total < < endl

 

}

 

//////////////////////////< program 3.2 > //////////////////////////////

 

output of this program is given below:

 

                                                                      basic            950

 

                                                    allowance                95

 

                                                                      total        1045

 

type cast operator

 

c++ permits explicit type conversion of variables or expressions using the type cast operator. 

 

                                                type-name (expression)

 

example:

 

                                              average = sum/flout(i)

 

a type-name behaves as if it is a function for converting values to a designated type.

 

expressions and implicit conversions

 

·    an expression is a combination of operators, constants and variables arranged as per the rules of the language.

 

·    it may be also include function calls which return values.

 

·    an expression may consist of one or more operands and zero or more operators to produce a value.

 

·    expression may be of four types, constant expressions, integral expressions, float expressions and pointer expressions.

 

  • constant expressions consist of only constant values. example:

     

                                              15

 

                                              20+5/2.0

 

                                              x

 

  • integral expressions are those which produce integer results after implementing all the automatic and explicit type conversion. example:

     

m

 

m*n-5

 

m- x

 

5+int(2.0)

 

                      when m and n are integer variables.

 

  • float expressions are those which , after all conversions, produce floating-point results.

     

example:

 

                      x + y

 

                      x * y/10

 

                      5 + float(10)

 

                      10.75

 

where x and y are floating –point variables.

 

  • pointer expressions produce address values. example:

     

& m

 

ptr

 

ptr++1

 

"xyz"

 

                      where m is variable and ptr is a pointer.

 

                      we can mix data types in expressions. for example:

 

                                              m=5+2.75

 

whenever data types are mixed in an expression, c++ performs the conversions automatically. this process is known as implicit or automatic conversion.

 

 

operators overloading

 

overloading means assigning different meanings to an operation depending on the context. the number and type of operands decide the nature of operation to follow. example: the statement

 

                                                                      cout < < 75.86

 

invokes the definition for displaying a double type value, and

 

                                                                      cout < < "well done"

 

invokes the definition for displaying a char value.

 

we can define + operator to add two structures or objects.

 

question:

 

  1. what is the application of scope resolution operator : : in c++.

     

  2. write a program to print the following output using setw:

     

1

 

11

 

111

 

1111

 

11111

 

3. cast the following variables one to another:

 

long int  x

 

float  y

 


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .