Tuesday, 21 May 2013

PROGRAMMING PART OF C# --- OBJECTS AND VARIABLES CONTINUED

Submit ExpressSEO Services & Tools
PROGRAMMING PART OF C#  --- OBJECTS AND VARIABLES CONTINUED

             A C# program can be written by using the Windows Notepad application.  After creating a C # program in the Notepad application,  you need to compile and execute the program to get the desired output.  The compiler coverts the program's source code to machine code, such that the computer source code to machine code, such that the computer can understands the instructions in the program.

CREATING A SAMPLE C# PROGRAM 
         

using System;
class Car
               {
                 //Member variables
                 string Engine;
                 int NoOfWheels;
                 //Member functions
                 public void InputDetails()
                  {
                      Console.WriteLine("Enter the Engine Model");
                      Engine = Console.ReadLine();
                      Console.WriteLine("Enter the number of Wheels");
                      NoOfWheels = Convert.ToInt32(Console.ReadLine());
                      }

  public void DisplayDetails()
                  {
                      Console.WriteLine("The Engine Model is :{0}", Engine);
                      Console.WriteLine("The number of Wheels are: {0}", NoOfWheels);
                      NoOfWheels = Convert.ToInt32(Console.ReadLine());
                    }
}
//Class used to instantiate the Car class
class MainClass
{
              public static void Main(string[] args)
             {
                 Car MyCar = new Car();
                  MyCar.InputDetails();
                  MyCar.DisplayDetails();
              }
}

Typical building blocks of a C# program are:
  • The using keyword
  • The class keyword
  • The comment entry
  • The member variables
  • The member functions
  • The instantiating class
The using Keyword
    The using keyword is used to include the namespaces in the program.  Keywords are reserved words that have a special meaning.  The statement, using System, declares that you can refer to the classes defined in the namespace without using the fully qualified name.  You can write multiple using statements in your program to include more than one namespace in the program.

The class Keyword
      The class keyword is used to declare a class.  In the preceding code, the class keyword defines the class, Car.  The braces, known as delimiters, are used to indicate the start and end of a class body.

The Comment Entry
       Comments are used to explain the code in a program.  Compilers ignore the comment entries included in a program.  The symbol '//' treats the rest of code within the same line as a comment.  If a comment entry spans more than one line, it has to be enclosed within '/*' and '*/'.

The following examples show the usage of comment entries in C#:

  • // Single Line Comment
  • /* This is the sample program to display addition of two numbers                                                                                                                    
  •    Multiple line comment */
Member Variables
       Variables are used to store data in the memory.  Variables are also called the data members of a class.  In the code, the Car class has two member variables, Engine and NoofWheels.  These variables are used to store the data provided by the user.

Member Functions
        A function is a set of statements that perform a specific task in response to a message.  The  functions of a class are called member functions in C# and are also referred to as methods.  Member functions are declared within a class.  The function declaration declares the name of the function and its attributes in the class and the function definition contains the code of the function.  A program calls a function by using the function name.  However , what will the function do when it is called, is represented by the function definition.
        The Car class code contains two member functions, InputDetails and DisplayDetails.  These are declared and defined within the class.  Notice that the function definition contains code, which is written inside a block by using braces '{}'.  These braces represent the start and end of the function body.
         The InputDetails() mehod in the Car class code snippet will prompt the user to enter the engine model and number of wheels in the vehicle.  It will then store these details in the respective variables.  Further, the DisplayDetails() method will display the values stored in the member variables.

Instantiating Class
    In the Car class code, the MainClass class contains the Main() method.  This class is used to instantiate the Car class.  The first line of code that a C# compiler looks for is the Main() method.
    To implement the functionality of a class, you need to create an object of the class.  Objects interact with each other by passing messages and by responding to the received messages.  In C#, the task of passing messages can be done by using the member functions.
     All the objects of a class share the same copy of the member functions.  However, they maintain a separate copy of each member variable in memory.  The sharing of member functions and non sharing of member variables by the objects is shown in the following .
     Suppose there is Class: Car.  It has two variables: Member Variable1 and Member Variable2 .  And it has two functions : Member function 1 and Member function2.  Now two objects are created from it.  Let me briefly explain it.  In the previously example.  Car class.  To create an object named Mycar of the Car class , the following code snippet is given in the Main() method.
   
      Car Mycar = new Car();
    The member functions of the class are accessed through an object of the class by using the "." operator, as shown in the following code snippet:
    Mycar.InputDetails();
    Mycar.DisplayDetails();
      In the preceding example, the object name and the "." operator are used to access the two member functions, InputDetails() and DisplayDetails().  Inaa other words, the object Mycar of the class Car invoked the InputDetails() and DisplayDetails() methods.  These functions accept the values for the engine type and the number of wheels from the user and display the same, respectively.

Note
     The preceding code invokes the member functions from within the Main() method.  Usually, member functions are declared and defined under the public access specifier.  Access specifiers are used to determine if any other class can access the variables or function of a class.  You will learn more about access specifiers in subsequent posts.

No comments:

Post a Comment