Thursday, November 24, 2011

Learn C# Programming Tutorial Lesson 8 - Classes and Objects


What is a class?

A class is basically a single module of a program. All C# programs other than the most basic ones use many classes. Classes help to divide up a program into modules so that it is easier to work with the code.
Something you must understand about classes is that they are actually like an architect's plan for a house. You don't usually use the code in a class directly but rather you create objects from a class and use the objects. An object is like the house that is built from the architect's plan.

Creating a class

You have already used a class before when you created your first program. We are going to go one step further now and create another class that is used by the main program. Here is an example of a class called MyClass which you can create in a file called MyClass.cs:
using System;

class MyClass
{
}

It should look similar to the other programs you have written. You will see though that there are no methods in this class yet. Let's now add a simple method that just adds 2 numbers together and returns the result.
using System;

class MyClass
{
   public int Add(int a, int b)
   {
      int result = a + b;
      return result;
   }
}

You will see that I have made the method public so that it can be accessed from other classes. If you want to hide a method from other classes then you can use the private keyword.
You already know a little bit about local and global variables. We are now going to create a public global variable called sum which will store the sum of the result of all calls to the Add method to demonstrate how global variables work in classes.
using System;

class MyClass
{
   public int sum;

   public int Add(int a, int b)
   {
      int result = a + b;
      sum = sum + result;
      return result;
   }
}

Using a class

When you create an object from a class we say that you are instantiating an object of that class. We are now going to create the main program class, like you have done before in previous programs, which will instantiate an instance of MyClass. Here is the code for the main program class which will be called TestClass and will be saved in a file called TestClass.cs:
using System;

class TestClass
{
   public static void Main()
   {
   }
}

Now we use the new keyword to instantiate an instance of MyClass from the main program. When you instantiate a class you must give it a name just like when you declare a variable. This one will be called mc.
using System;

class TestClass
{
   public static void Main()
   {
      MyClass mc = new MyClass();
   }
}

Now we will use the Add method of MyClass to add the numbers 5 and 7 together and the numbers 3 and 2. We will then print out the 2 results as well as the sum from MyClass.
using System;

class TestClass
{
   public static void Main()
   {
      MyClass mc = new MyClass();
      int res1 = mc.Add(5, 7);
      Console.WriteLine(res1);
      int res2 = mc.Add(3, 2);
      Console.WriteLine(res2);
      Console.WriteLine(mc.sum);
   }
}

When you run the program you should see the numbers 12, 5 and 17 each on their own line.

Constructors

A constructor is a method that runs when a class is instantiated and is used to initialize the state of the object. A constructor looks very similar to any other method in a class except that it doesn't have a return value. A constructor must also have the exact same name as the name of the class. Here is an example using the MyClass example from above that initializes sum to 10:
using System;

class MyClass
{
   public MyClass()
   {
      sum = 10;
   }

   public int sum;

   public int Add(int a, int b)
   {
      int result = a + b;
      sum = sum + result;
      return result;
   }
}

A constructor can have parameters just like a normal method. You can also overload a constructor. We will now overload the MyClass constructor with another constructor that takes a parameter.
using System;

class MyClass
{
   public MyClass()
   {
      sum = 10;
   }

   public MyClass(int s)
   {
      sum = s;
   }

   public int sum;

   public int Add(int a, int b)
   {
      int result = a + b;
      sum = sum + result;
      return result;
   }
}

To use a constructor that has parameters you must put the values to be passed as parameters in the brackets after the class name when instantiating the class.
using System;

class TestClass
{
   public static void Main()
   {
      MyClass mc = new MyClass(25);
      Console.WriteLine(mc.sum);
   }
}

0 comments:

Post a Comment

Setec Institute || Group : SH6 | | Name : Kim Chanthy. Powered by Blogger.

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hot Sonakshi Sinha, Car Price in India