C# 0 The Complete Reference


PART I C h a p t e r   9



Download 4,07 Mb.
Pdf ko'rish
bet308/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   304   305   306   307   308   309   310   311   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   9 :  

O p e r a t o r   O v e r l o a d i n g  

229


PART IPART I

4, 4, 4


3, 3, 3

2, 2, 2


1, 1, 1

Notice how the 



ThreeD

 objects are used to control 



if

 statements and a 



while

 loop. In the 

case of the 

if

 statements, the 



ThreeD

 object is evaluated using 



true

. If the result of this 

operation is true, then the 

if

 statement succeeds. In the case of the 



do

-

while

 loop, each 

iteration of the loop decrements 



b

. The loop repeats as long as 



b

 evaluates as true (that is, it 

contains at least one non-zero coordinate). When 

b

 contains all zero coordinates, it evaluates 

as false when the 

true

 operator is applied and the loop stops.



Overloading the Logical Operators

As you know, C# defines the following logical operators: 



&

,

|

,

!

,

&&

, and 

||

. Of these, only 

the

&

,

|

, and 

!

 can be overloaded. By following certain rules, however, the benefits of the 

short-circuit 

&&

 and 


||

 can still be obtained. Each situation is examined here.



A Simple Approach to Overloading the Logical Operators

Let’s begin with the simplest situation. If you will not be making use of the short-circuit 

logical operators, then you can overload 

&

 and 


|

 as you would intuitively think, with each 

returning a 

bool

 result. An overloaded 



!

 will also usually return a 



bool

 result.


Here is an example that overloads the 

!

,

&

, and 

|

 logical operators for objects of type 



ThreeD

. As before, each assumes that a 



ThreeD

 object is true if at least one coordinate is 

non-zero. If all three coordinates are zero, then the object is false.

// A simple way to overload !, |, and & for ThreeD.

using System;

// A three-dimensional coordinate class.

class ThreeD {

  int x, y, z; // 3-D coordinates

  public ThreeD() { x = y = z = 0; }

  public ThreeD(int i, int j, int k) { x = i; y = j; z = k; }

  // Overload |.

  public static bool operator |(ThreeD op1, ThreeD op2)

  {

    if( ((op1.x != 0) || (op1.y != 0) || (op1.z != 0)) |



       ((op2.x != 0) || (op2.y != 0) || (op2.z != 0)) )

      return true;

    else

      return false;

  }

  // Overload &.



  public static bool operator &(ThreeD op1, ThreeD op2)

  {


www.freepdf-books.com


230

 

P a r t   I :  



T h e   C #   L a n g u a g e

    if( ((op1.x != 0) && (op1.y != 0) && (op1.z != 0)) &

       ((op2.x != 0) && (op2.y != 0) && (op2.z != 0)) )

      return true;

    else

      return false;

  }

  // Overload !.



  public static bool operator !(ThreeD op)

  {


    if((op.x != 0) || (op.y != 0) || (op.z != 0))

      return false;

    else return true;

  }


  // Show X, Y, Z coordinates.

  public void Show()

  {

    Console.WriteLine(x + ", " + y + ", " + z);



  }

}

class TrueFalseDemo {



  static void Main() {

    ThreeD a = new ThreeD(5, 6, 7);

    ThreeD b = new ThreeD(10, 10, 10);

    ThreeD c = new ThreeD(0, 0, 0);

    Console.Write("Here is a: ");

    a.Show();

    Console.Write("Here is b: ");

    b.Show();

    Console.Write("Here is c: ");

    c.Show();

    Console.WriteLine();

    if(!a) Console.WriteLine("a is false.");

    if(!b) Console.WriteLine("b is false.");

    if(!c) Console.WriteLine("c is false.");

    Console.WriteLine();

    if(a & b) Console.WriteLine("a & b is true.");

    else Console.WriteLine("a & b is false.");

    if(a & c) Console.WriteLine("a & c is true.");

    else Console.WriteLine("a & c is false.");

    if(a | b) Console.WriteLine("a | b is true.");

    else Console.WriteLine("a | b is false.");

    if(a | c) Console.WriteLine("a | c is true.");

    else Console.WriteLine("a | c is false.");

  }


}

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   304   305   306   307   308   309   310   311   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish