Objects

=Objects=

Objects and classes allow for programmers to break code down even further than functions would allow. They also make it easy to refer to a function or variable that is used often in a program. Objects are all around us, people are objects, cats are objects. These are divided into classes or categories. For example, Ryan is an object from the person class. My dog, bailey, is an object from the dog class.

Each class or category has features and functions that they perform each day. A person will have features such as hair color, height, weight, or Grade Point Average. And functions such as go to school, read, write, or draw. A dog will have similar features and functions but will be set up a little differently than people. You would want to separate dogs from people because it is not very often that you see a person panting or drinking water from a bowl on the ground. Another example class would be a computer class A computer has several features such as processor type, memory size, and screen size.

Now lets look at setting this up in the programming world. In order to get started setting up a class in C# you would click on Project...Add Class...Select the class icon and type person for the name of the class at the bottom of the screen. You should see a new tab at the top. This is now your person class in C#. We can define properties,or features, of a person. I am using age and hair color to start with.

code format="csharp" using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace WindowsFormsApplication1 {   class Person {       public int age; public string hair_color;

public Person {       }

public void GoToSchool {            //Go straight on Ewing: //Turn Right on Fellows: //Stop at stop sign: //Turn into student lot: }   } }

code notice there is code format="csharp" class Person code

we will concern ourselves with this point down. I declare a few variables to use as our features of a person.

code format="csharp" public int age; public int hair_color; code

Next is something called a constructor. When we create an actual instance of a person which might be named Fred we will call on a function to make a new person. The function called is below.

code format="csharp" public Person { } code

This would create an empty person until we specify what color hair and how old the person is later on in the code.

Below is how we would create Fred in the main program.

code format="csharp" Person fred = new Person; code This sets Fred up in the program but he still has no age or hair color. To do that the code would look like below.

code fred.hair_color = "Brown"; fred.age = 30;

code notice we use periods to access the variables that fred has inherited from the person class.

You can also use a function that every person would use such as go to school. Then I could have our person named fred go to school. code fred.GoToSchool; code This would have fred go to school which is really helpful when we start creating a lot of people. We can create a few people called wilma, barney, and betty to go to school without the need to rewrite a lot of code.

code fred.GoToSchool; wilma.GoToSchool; barney.GoToSchool; betty.GoToSchool; code Isn't this neat! This makes your code easy to read and very easy to expand upon. Let's say the school moves. All we would need to do is change the directions in the person class and it would not mess with the main code!