To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > .NET > C#

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Apr 16th, 2007, 01:38 PM   #1
gjon
Hyperactive Member
 
gjon's Avatar
 
Join Date: Nov 04
Location: Inescapable Void
Posts: 440
gjon is an unknown quantity at this point (<10)
Resolved [RESOLVED] why two classes?

The book uses two classes. One class has main in it and is being used to create objects from another class. Or I believe this is what is happening. My question is why is the example using two? Could not one class have main and create objects of that class? The book does not explain why it does this. Maybe someone has a moment to look this over and break it down for me.
Code:
using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants

  // Display the area per person.
  public void areaPerPerson() {
    Console.WriteLine("  " + area / occupants +
                      " area per person");
  }
}

// Use the areaPerPerson() method.
class BuildingDemo {
  public static void Main() {
    Building house = new Building();
    Building office = new Building();

    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area");
    house.areaPerPerson();

    Console.WriteLine();

    Console.WriteLine("office has:\n  " +
                      office.floors + " floors\n  " +
                      office.occupants + " occupants\n  " +
                      office.area + " total area");
    office.areaPerPerson();
  }
}
Why couldn't we just do it this way?
Code:
using System;
// Use the areaPerPerson() method.
class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants

  // Display the area per person.
  public void areaPerPerson() {
    Console.WriteLine("  " + area / occupants +
                      " area per person");
  }
  public static void Main() {
    Building house = new Building();
    Building office = new Building();

    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area");
    house.areaPerPerson();

    Console.WriteLine();

    Console.WriteLine("office has:\n  " +
                      office.floors + " floors\n  " +
                      office.occupants + " occupants\n  " +
                      office.area + " total area");
    office.areaPerPerson();
  }
}
Thank you in advance.
gjon is offline   Reply With Quote
Old Apr 16th, 2007, 06:26 PM   #2
moeur
Old Member
 
moeur's Avatar
 
Join Date: Nov 04
Location: In Hiding.... Weather: sizzzzlin'........ Code: Secret
Posts: 2,681
moeur is a glorious beacon of light (400+)moeur is a glorious beacon of light (400+)moeur is a glorious beacon of light (400+)moeur is a glorious beacon of light (400+)moeur is a glorious beacon of light (400+)
Re: why two classes?

I think you are missing the point of having classes.

The BuildingDemo class is the program that is going to be run. The Building class is an object that can be used and reused in your buildingDemo program and other programs if you wish.
moeur is offline   Reply With Quote
Old Apr 16th, 2007, 06:54 PM   #3
gjon
Hyperactive Member
 
gjon's Avatar
 
Join Date: Nov 04
Location: Inescapable Void
Posts: 440
gjon is an unknown quantity at this point (<10)
Re: why two classes?

Ok so we have to have this starting point. The program. We have the classes that we are going to make objects out of to use in our program. It just confuses me because the program itself begins with class. The entry point of all programs is Main. But this entry point is also a class right? So, can this entry point class/program be used like a class to create objects from some other program? I am confused as to why the program is a class but it's relying on other classes to create objects. How does this entry program differ from another class, err why is it a class?
I hope you understand what I am asking. Thanks in advance.
gjon is offline   Reply With Quote
Old Apr 16th, 2007, 07:22 PM   #4
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 54,913
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: why two classes?

The whole point of OOP is encapsulation, as I said in your last thread. You have two classes that encapsulate the data and behaviour of two types. One type is your application and the other type is a building. Your application is not a building and a building is not an application. You have one class that represents an instance of your application. You only have one application so you only have one instance of that class. Your application may create hundreds of buildings, so you may have hundreds of instances of the Building class.

Like we said in your other thread, you could put all the code into one class but what if you wanted to manipulate buildings in another application? You'd have to recreate all the code for a building in each application. By having a Building class you encapsulate all the data and behaviour for a building into that type.

The point of classes is that they are supposed to represent a type of object, whether physical or abstract. Each type of object you want to represent in your application should have its own class. The application itself is a type of object so there should be a class to represent it. A building is also a type of object so there should be a class to represent that too.
__________________

2007, 2008, 2009, 2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET
jmcilhinney is offline   Reply With Quote
Old Apr 16th, 2007, 07:58 PM   #5
gjon
Hyperactive Member
 
gjon's Avatar
 
Join Date: Nov 04
Location: Inescapable Void
Posts: 440
gjon is an unknown quantity at this point (<10)
Re: why two classes?

As I read this and the book and mull it over, it's beginning to click better.
Thank you so much for your time.
gjon is offline   Reply With Quote
Reply

Go Back   VBForums > .NET > C#


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:20 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.