Class on Classes and Objects

In every OOP(Object oriented programming), one thing that is a must, that is, the OBJECT modeling. What is an object? Well in a layman’s language, any tangible item is an object. Quite true! In programming, what do we do? We take up a problem and sort out an appropriate algorithm to reach to our objective. Let’s take an everyday example of the busy world. You need to get to the office,meet Eva at lunch, after office you need to buy some household chores. So, you plan your day on a sticky note as follows:
i. Get ready for office.
ii. Meet Eva at lunch.
iii. Buy orange juice, lemon grass, eggplants and sunscreen.
Now, this is the algorithm. You will not definitely go to lunch first or to the store first, so in short, there is sequential flow. Now, in order to reach the office, meet Eva at lunch, and buying all items in the store, take a moment, mark these things: OFFICE,BUS(to office),EVA,RESTAURANT(for lunch),STORE,ORANGE JUICE,LEMON GRASS, etc. are all tangible items and are treated as objects in the world of programming.
Well, now lets get a deeper look at objects, in order to do so, we have to know about classes. What’s a class? Well, class is a definite space that holds all the methods(functions) and properties for a particular part or task to be completed by it. Lets take an example of a class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace tut1
{
    class office
    {
        int desks,count=0,deskadded;
        int[] appointments=new int[10];
        int meetings;
        public office(int y)       /*Constructor*/
        {
            desks = y;
        }
        public void desk_check()   /*method to count desks after addition*/
        {
            Console.WriteLine("HOW MANY DESKS TO BE ADDED?");
            deskadded = Convert.ToInt32(Console.ReadLine());
            count=desks+deskadded;
            Console.WriteLine("{0} no of desks",count);
             }
        public void appo()        /*method for appointments*/
        {
          
            Console.WriteLine("enter appointments:");
            for (int i = 0; i < appointments.Length; i++) {
                appointments[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("APPOINTMENTS SERIALS ARE:");
            foreach (int x in appointments) {
                Console.WriteLine("{0}",x);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            office office_1 = new office(10); /*new object created as office_1*/
            office_1.desk_check();            /*desk_check method is called*/
            office_1.appo();                  /*appo method is called*/
        }
    }
}

Go through this example properly and then try to understand what’s the utility of an object. Well, in a multi-paradigm program needs to access a lot of codes where code redundancy as well as complexness increases. This was substantially reduced with the advent of OOP model.
Now, we shall look at an object as an instance of a class.
office_1=new office(10);
Here we create a new example of the class office as office_1 with a constructor office(10). This is how we relate the object with class. Now statements: office_1.desk_check(); and office_1.appo(); calls methods from the class office used for counting desks and appointments list respectively. The format being:
OBJECT.METHOD(parameters);
Well, this is enough about basics about classes and objects using C#. Here’s the output:

Output of the above code.

 

Advertisements

One thought on “Class on Classes and Objects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s