• Loading
    • Basic Object Oriented Concept In C Sharp (C#) .Net

      REPRESENTATION OF INFORMATION
      You could probably know, that to store and represent information, we use Variables in any programming languages. All the variables are declared with a specific Data Type. In C#, the data type could be classified into two types

      1. Primitive Data type
      2. User Defined Data type

      Primitive Data Type:
      Primitive Data Type includes the basic data types that we can declare directly such as int, float, double, string, char etc.
      User Defined Data type:
      User Defined Data type is a collection of information. Some examples of User Defined Data Types includes Structure, Class etc.
      Example:

      Code:
            class taxComponent
            {
                        int numberOfYears;
                        float taxRate;
            }
      Here the class name taxComponent is a User defined data type as it holds a collection of information i.e numberOfYears and taxRate



      DATA ABSTRACTION:
      Abstraction is a process of hiding unnecessary implementation information from user. The concept could be explained using a simple example.
      Example:
      Code:
      class Employee
      {
                  string name;
                  int age;
                 
                  public void getEmployeeDetails()
      {
      -------------
      -------------
      }
       
      public void setEmployeeDetails()
      {
      -------------
      -------------
      }
      }
      In the above example, the data members are string name and int age. The member functions are public void getEmployeeDetails() and public void setEmployeeDetails(). The data members are private by default and so they cannot be accessed in other classes. The member functions are public and so it could be accessible by other classes. Here, the data members are hidden from other classes, this concept is called as Data Abstraction.

      DATA ENCAPSULATION:
      Wrapping up of both Data Members and Member Functions in a template is called as Encapsulation. In the above example, the data members are string name and int age. The member functions are public void getEmployeeDetails() and public setEmployeeDetails().
      The data members and member functions are wrapped up in a single template called Employee. We call the class name Employee as a Template here.


      STRUCTURE OPERATIONS:
      A structure is a User Defined Data Type. A structure is used to store more than one information about an entity in a template. For example, we could say that the information regarding an employee could be his name, age, basic pay, and performance pay. Even though all the above information are of different values and different data types, they still belong to a single employee. So that we can create a structure for employee and have the information regarding the employee as Data Members.
      Consider the Following Example:

      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
       
      namespace Structure_Operations
      {
          class Program
          {
              struct employee
              {
                  public string name;
                  public int age;
                  public double basic;
                  public double performance;
              }
       
              static void Main(string[] args)
              {
                  employee emp;
       
                  emp.name = "Manoj";
                  emp.age = 24;
                  emp.basic = 10000;
                  emp.performance = 1000;
       
                  Console.WriteLine("Employee Details: Name: " + emp.name + " Basic Pay: " + emp.basic + " Performance Pay: " + emp.performance);
       
                  Console.ReadLine();
              }
          }
      }
      Output:
      Employee Details: Name: Manoj Basic Pay: 10000 Performance Pay: 1000


      PASSING STRUCTURE AS ARGUMENTS
      If a lot of information regarding an entity needs to be passed to a function, then it can be put in a structure and sent to the function as a structure variable. In the function definition, the structure is again unzipped and the contents can be retrieved.
      Given below is a simple example, which explains the way to pass structure as arguments to a function.
      Example:
      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
       
      namespace Structure_Operations
      {
          struct employee
          {
              public string name;
              public int age;
              public double basic;
              public double performance;       
          }   
          
          class Program
          {       
              static void Main(string[] args)
              {
                  employee emp;
       
                  emp.name = "Manoj";
                  emp.age = 24;
                  emp.basic = 10000;
                  emp.performance = 1000;          
       
                  dispEmployeeDetails(emp);
       
                  Console.ReadLine();
              }
       
              public static void dispEmployeeDetails(employee e1)
              {
                  Console.WriteLine("Employee Name: " + e1.name + " Employee Basic Salary: " + e1.basic);
              }
             
          }
      }
      Output:
      Employee Name: Manoj Employe Basic Salary: 10000

      OBJECT
      Object is an instance of a class. Object is an entity that has a well-defined structure and behavior. In real-world, everything that surround us can be considered as Objects.
      For Example:
      A Car
      A Computer
      A Book
      A Doctor etc.,
      Every object must have the following responsibilities.

      • Knowing Responsibility (State)
      • Doing Responsibility (Behaviour)


      Knowing Responsibility:
      To see about knowing responsibility let us consider A Doctor to be an object in our case. Every doctor has the following details.
      Name
      Profession
      Age
      Salary
      i.e the information about the object. These are called as Knowing Responsibilities. They are other wise called as Attributes or Data Members. They maintain the State Information about an object.

      Doing Responsibility:
      To see about Doing Responsibility, let us consider A Doctor to be an object in our case. Every doctor does the following operations.
      Consult Patients
      Treat Patients
      Do Surgery

      i.e the action to be performed by the object. These are called as Doing Responsibilities. They are otherwise called as Methods or Member Functions. They maintain the Behaviour Information about an object.

      Consider the following example, which explains the Knowing and Doing Responsibilites.


      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
       
      namespace Gross_Pay
      {
          class gp
          {
              double grosspay;
              double tax;
       
              public void dispGrossPay()
              {
                  tax=0.0;
       
                  Console.WriteLine("Enter the Gross Pay: ");
                  grosspay = Convert.ToDouble(Console.ReadLine());
       
                  if (grosspay <= 240)
                      tax = 0;
                  else if (grosspay <= 480)
                      tax = grosspay * (15.0 / 100);
                  else if (grosspay > 480)
                      tax = grosspay * (28.0 / 100);
       
                  Console.WriteLine("Tax Owed is = " + tax);
       
                  Console.ReadLine();
       
              }
          }
       
          class Program
          {
              static void Main(string[] args)
              {
                  gp g = new gp();
                  g.dispGrossPay();            
              }
          }
      }

      CLASS AND OBJECTS

      A class is a template for creating objects. Let us have a look at the figure given below which clearly explains what class and objects are.







      From the above figure, you could now get into a clear understanding of what class and object are.

      • A class is used as a template for the creation of similar objects
      • An object is said to be an instance of a class
      • A class is the blueprint from which individual objects are created.


      PROCEDURAL PARADIGM TO OBJECT ORIENTED PARADIGM
      We will compare the structure and class program to know the transition from Procedural Paradigm to Object Oriented Paradigm.

      ACCESS LEVELS

      • Public Part of the Class : This is the outside view of the class. The part visible by everybody (or at least any object who has the name of an object from this class).
      • Private Part of the class : All the instance fields and some behaviors which need not be visible to outside world




      Public vs Private Data
      Public data

      • Good, easy access
      • Bad, exposes implementation details


      • Private Data
        • Private fields and public access methods
        • Advantage
          • Access control (read only, write only, read write possible)



      Object Creation and Member Access
      Objects can be created for a class by using the new operator. For accessing the members of a class we use the dot operator as explained below.
      Example:
      Code:
      class Program
          {
              static void Main(string[] args)
              {
                  gp g = new gp();
                  g.dispGrossPay();            
              }
          }
      Referring to Current Instance of the Class
      Consider the following code snippet.
      Code:
      class GrossPay
          {
              double basic;
              double hra;
              double da;
       
              public void calcGrossPay(double basic, double hra, double da)
              {
                  this.basic = basic;
                  this.hra = hra;
                  this.da = da;
              }
          }
      In the above example there are two instances of basic, hra, da. One instance is defined as an argument in the function and another instance is the data members of the class. In such cases a conflict can arise, which can be resolved by using this operator. This operator is used to specify the data mebers of a class.


      Thanks for Reading My Article...



    Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.