Results 1 to 5 of 5

Thread: [RESOLVED] why two classes?

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    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.

  2. #2
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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.

  3. #3

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width