C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet298/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   294   295   296   297   298   299   300   301   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

 and the 



++

 operator:

// More operator overloading.

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 binary +.

  public static ThreeD operator +(ThreeD op1, ThreeD op2)

  {


    ThreeD result = new ThreeD();

    /* This adds together the coordinates of the two points

       and returns the result. */

    result.x = op1.x + op2.x;

    result.y = op1.y + op2.y;

    result.z = op1.z + op2.z;

    return result;

  }


  // Overload binary -.

www.freepdf-books.com




218

 

P a r t   I :  



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

  public static ThreeD operator -(ThreeD op1, ThreeD op2)

  {

    ThreeD result = new ThreeD();



    /* Notice the order of the operands. op1 is the left

       operand and op2 is the right. */

    result.x = op1.x - op2.x;

    result.y = op1.y - op2.y;

    result.z = op1.z - op2.z;

    return result;

  }

  // Overload unary -.



  public static ThreeD operator -(ThreeD op)

  {


    ThreeD result = new ThreeD();

    result.x = -op.x;

    result.y = -op.y;

    result.z = -op.z;

    return result;

  }


  // Overload unary ++.

  public static ThreeD operator ++(ThreeD op)

  {

    ThreeD result = new ThreeD();



    // Return the incremented 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()

  {

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



  }

}

class ThreeDDemo {



  static void Main() {

    ThreeD a = new ThreeD(1, 2, 3);

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

    ThreeD c = new ThreeD();

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

    a.Show();

    Console.WriteLine();

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   294   295   296   297   298   299   300   301   ...   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