C# 0 The Complete Reference


PART I C h a p t e r   9



Download 4,07 Mb.
Pdf ko'rish
bet307/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   303   304   305   306   307   308   309   310   ...   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  

227


PART IPART I

public static bool operator false(



param-type operand

)

{



   // return true or false

}

Notice that each returns a 



bool

 result.


The following example shows how 

true

 and 


false

 can be implemented for the 



ThreeD

class. 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. The decrement operator is also implemented 

for the purpose of illustration.

// Overload true and false 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 true.

  public static bool operator true(ThreeD op) {

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

      return true; // at least one coordinate is non-zero

    else


      return false;

  }


  // Overload false.

  public static bool operator false(ThreeD op) {

    if((op.x == 0) && (op.y == 0) && (op.z == 0))

      return true; // all coordinates are zero

    else

      return false;

  }

  // Overload unary --.



  public static ThreeD operator --(ThreeD op)

  {


    ThreeD result = new ThreeD();

    // Return the decremented result.

    result.x = op.x - 1;

    result.y = op.y - 1;

    result.z = op.z - 1;

    return result;

  }

  // Show X, Y, Z coordinates.



  public void Show()

  {


www.freepdf-books.com


228

 

P a r t   I :  



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

    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 true.");

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

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

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

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

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

    Console.WriteLine();

    Console.WriteLine("Control a loop using a ThreeD object.");

    do {


      b.Show();

      b--;

    } while(b);

  }


}

The output is shown here:

Here is a: 5, 6, 7

Here is b: 10, 10, 10

Here is c: 0, 0, 0

a is true.

b is true.

c is false.

Control a loop using a ThreeD object.

10, 10, 10

9, 9, 9

8, 8, 8


7, 7, 7

6, 6, 6


5, 5, 5

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   303   304   305   306   307   308   309   310   ...   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