|
|
#1 |
|
Hyperactive Member
Join Date: Nov 04
Location: Inescapable Void
Posts: 442
![]() |
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();
}
}
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();
}
}
|
|
|
|
|
|
#2 |
|
Old Member
Join Date: Nov 04
Location: In Hiding.... Weather: sizzzzlin'........ Code: Secret
Posts: 2,701
![]() ![]() ![]() ![]() ![]() |
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 |
|
Hyperactive Member
Join Date: Nov 04
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 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 61,542
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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-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 (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW* |
|
|
|
|
|
#5 |
|
Hyperactive Member
Join Date: Nov 04
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. |
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|