Sunday, November 14, 2010

C# - Introduction to Class and Objects

A class is a blue print or a template. One or Many outcome of the template is known as object(s).

You can declare a class like below:
A Simple form of Declaration-

public class phone
{
            //Some Code here
}


A Class is combination of member functions (Methods) and member variables (fields). A class can also have properties. And, a property is like member variable but can decide what value to take and what value not to take. Similarly, it can decide how a value can be retrieved.

The real world representation considers the methods as object’s behavior and fields as state of the object.

Let us consider the below example:

using System;
class Phone
{
private string model;
      private string make;
      private int cost;
      static void Main(string[] args)
      {
      }
}

The phone class has three states now. These member variables will say model, make and cost of the phone. Static void main is the entry point of the console program execution. To create an object based on the above template that is to create an object of the above class, use the following statement:

Phone myphone = new Phone();

Here,
myphone – The actual object
Phone – the template
new – Keyword that tells we want to create a real object and allocate space required for it.

To initialize the class fields we can go for special method called constructor. The constructor is called when the object is created. If there is no parameters in the constructor, then we can say it is default constructor. The compiler when translating your class makes sure to add a default constructor (Parameter less constructor) when it is not available that is when it is not implemented by you.

class Phone
{
      //001: Adding fields
      private string model;
      private string make;
      private int cost;

      //002: Adding a default constructor
      public Phone()
      {
            model = "823CK";
            make = "Samsung";
            cost = 2000;
      }

      static void Main(string[] args)
      {
            Phone myphone = new Phone();
      }
}

Like function overloading, a constructor can also be overloaded. A constructor can accept parameters, but do not return any value back. Below is the overloaded version of constructor:

//003: Overloaded constructor
public Phone(string model, string make_param, int price)
{
      this.model = model;
      make = make_param;
      cost = price;
}

In the above overloaded constructor note the usage of this.model. The “this” keyword refers the object that is being constructed here. As the parameter variable name model is having the same name of member variable model, this keyword is used to differentiate.

Methods or member functions defines the behavior of the class. It will accept parameters and return value. Let us add some function to the phone class:

//004: Function: Get Discount
float getDiscount(int percent)
{
      if (percent<0)
            percent = 0;
      if (percent>100)
            percent = 100;

      return (float) (this.cost * percent / 100);
}

//Function: Display current states
void display()
{
      Console.WriteLine("Make:{0}, Model:{1}, Cost:{2}", make, model, cost );
}

To access the members of the class use the dot (.) operator. Below is the code that uses the class created above:
 //005: Create an object using default constructor
Phone myfirstphone = new Phone();

//006: Make a call to the member function to display the state
myfirstphone.display();

//007: Construct another object using overloaded constructor
Phone mySecondPhone = new Phone("70CF", "Samsung", 7000);
Console.WriteLine("I got a Discount of: {0}", mySecondPhone.getDiscount(10));

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Firend :)