Programming Tutorial: Classes Part 1

From IAM MediaWiki Create Change
Jump to: navigation, search

Language: C#

Basics

Objects are not as complicated as you might think. You can almost think of them as mini-programs, because they have variables and methods, just like a program. However, they are modular and can be applied to many different situations. One of the many advantages of an object is that it can be written in one place and used in many, like a function.


The best way to understand an object in code is by relating it to a object in real life. The classic example is a dog. Say we have a dog. Dogs have a name, age, and a weight. A possible object for this in a C# console application would be:

class dog
{
    public string name;
    public int age, weight;
}


Dogs also do things, aside from existing. One of the things they do is bark. Our dog needs a bark method. To this, we add the bark method so that our class looks like this:

class dog
{
    public string name;
    public int age, weight;

    public void bark()
    {
        Console.WriteLine("Woof!");
    }
}

As you can see, a class function looks just like a regular function.

You may have noticed the word "public" in our class. This is called an "access modifier." Access modifiers serve to keep data and functionality of a class safe from other code that shouldn't be using it directly. This becomes more important later on. The two most common access modifiers are "public" and "private." Public offers no protection from outside code, but in many cases, protection is not needed. Private makes a member (such as name) or method (such as bark()) accessible only from inside the class itself. To learn more about C# access modifiers, go to http://msdn.microsoft.com/en-us/library/wxh6fsc7(VS.71).aspx.

Initialization

A class can't be used until you make an instance of it (unless it is static, which is outside of the scope of this tutorial). An instance creates a copy in memory of a class, but the difference is that you can alter and access the variables. To make an instance of a dog, we would do the following:

dog myDog = new dog();

This line declares myDog as type dog, and also creates a new dog object and sets myDog to it. Alternatively, you can also break this out into two lines that do the same thing:

dog myDog;
myDog = new dog();

So, like a variable, you can declare an object in one place and define it in another. This is useful in various situations for scoping reasons.

Method and Member Access

To access the different members and methods of a class, we use the dot operator. Say we wanted to name our dog, we would set the name string in dog to the desired name. Our dog also needs an age and weight. Using myDog, you would write:

myDog.name = "Snapple";
myDog.age = 4;
myDog.weight = 110;

The rule is that we first specify the object we are going to manipulate or use, and then we use the dot operator to denote the member we are accessing.

object.memberOrMethod;

This is also true for methods. If we want to access the bark() function inside of the dog class, we would write:

myDog.bark();

These are the basics of classes. Stuctures work much the same way, but structures do not have methods, only members (variables). Here's a program that uses the dog class, copy and run it see how it works:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace classTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            dog myDog = new dog();

            myDog.name = "Snapple";
            myDog.age = 4;
            myDog.weight = 110;

            Console.WriteLine(
                string.Format("Name:  {0}\r\nAge:  {1}\r\nWeight:  {2}\r\n", 
                myDog.name, myDog.age, myDog.weight));

            myDog.bark();

            Console.ReadKey();
        }
    }

    class dog
    {
        public string name;
        public int age, weight;

        public void bark()
        {
            Console.WriteLine("Woof!");
        }
    }
}