C# 0 The Complete Reference


PART I C h a p t e r   6



Download 4,07 Mb.
Pdf ko'rish
bet193/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   189   190   191   192   193   194   195   196   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   6 :  

I n t r o d u c i n g   C l a s s e s   a n d   O b j e c t s  

129


PART IPART I

Add a Constructor to the Building Class

We can improve the 



Building

 class by adding a constructor that automatically initializes the 



Floors

,

Area

, and 

Occupants

 fields when an object is constructed. Pay special attention to 

how

Building

 objects are created.

// Add a constructor to Building.

using System;

class Building {

  public int Floors;    // number of floors

  public int Area;      // total square footage of building

  public int Occupants; // number of occupants

  // A parameterized constructor for Building.

  public Building(int f, int a, int o) {

    Floors = f;

    Area = a;

    Occupants = o;

  }


  // Display the area per person.

  public int AreaPerPerson() {

    return Area / Occupants;

  }


  // Return the maximum number of occupants if each

  // is to have at least the specified minimum area.

  public int MaxOccupant(int minArea) {

    return Area / minArea;

  }

}

// Use the parameterized Building constructor.



class BuildingDemo {

  static void Main() {

    Building house = new Building(2, 2500, 4);

    Building office = new Building(3, 4200, 25);

    Console.WriteLine("Maximum occupants for house if each has " +

                      300 + " square feet: " +

                      house.MaxOccupant(300));

    Console.WriteLine("Maximum occupants for office if each has " +

                      300 + " square feet: " +

                      office.MaxOccupant(300));

  }

}

The output from this program is the same as for the previous version.



www.freepdf-books.com


130

 

P a r t   I :  



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

Both



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   189   190   191   192   193   194   195   196   ...   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