Class and Object Tutorial UserPageVisits:316 active 80 80 DaniWeb 561 60 2009-12-11T23:21:42+00:00 https://www.daniweb.com/programming/software-development/code/245270/class-and-object-tutorial

Class and Object Tutorial

Geekitygeek

In the simplest of terms, a Class is like a cookie cutter. It describes what members and methods an object should have. You use the cookie cutter (class) to make (instantiate) one or more cookies that are all the same shape. The cookies are called objects.
The same way that you can decorate each cookie a different way, you can set the member values of each object to different values.
For instance, if you had a Class like this:

public class Cookie
{
   public string Topping;
}

You can create two cookies:

Cookie firstCookie = new Cookie();
Cookie secondCookie = new Cookie();

At this point each cookie is the same because they are fresh from the cutter. Now we can decorate them:

firstCookie.Topping = "Chocolate";
secondCookie.Topping = "Icing";

At this point we have two cookies that are different. Each has the same members but the values stored in them are different.

Now we need to be able to eat the cookies, so lets add a method. A method outlines a Classes behaviour.

public class Cookie
{
   public string Topping;

   public void TakeBite()
   {
       //crunch..nom nom nom
   }
}

SO, now we have two objects of the cookie class (firstCookie and secondCookie). To enjoy a chocolate flavour mouthful we need to bite into the right cookie. In our case, the chocolate topping went on firstCookie so thats the one we need to bite:

firstCookie.TakeBite();

A static class behaves a little differently. A static class is used to describe something you will only ever have one of. Lets expand our example. Before we can eat our cookies we need to cook them. Unless you have an industrial kitchen you will only have one oven. You don't want to have to re-build it every time you use it so you build it once and have it ready to use whenever you need it. Thats what static classes are:

public static class Oven
    {
        public static int Temperature;

        //the method is static so we don't need to instantiate an Oven object to use it. 
        //It accepts a Cookie as an input parameter (rawCookie)
        //It has a return type of Cookie which means it will return a Cookie when it finishes (return finishedCookie).
        public static Cookie CookCookie(Cookie rawCookie)
        {
            Cookie finishedCookie = rawCookie; //plus temperature and time to cook
            return finishedCookie;
        }
    }

We have made our oven and its ready to use. The static methods can be called any time we need them. So to cook our cookies we put them in the oven:

//the CookCookie has a return value of the type Cookie.
//So we send the cookie into the Cook method and get a cooked cookie back.
firstCookie = Oven.CookCookie(firstCookie);
secondCookie = Oven.CookCookie(secondCookie);

We didn't need to create an oven Oven myOven = new Oven(); because the method we need is static.

Hope that helps to clear up some of the confusion :)

ddanbe commented: Nice! +5
sknake commented: very nice +6
316 Views
About the Author

After many years of aimlessly wondering the job market in a daze I stumbled across a study from home course in VB.NET. Since then I have discovered a passion for coding and subsequently graduated from Canterbury Christchurch University with a 2:1 Bsc in Computing.
I have spent the last 2 years working for a local firm developing bespoke inhouse software.
More recently I have secured a position with RDDS Avionics where I will be training to write embedded C and C++ software.
My new job also gives me the perfect excuse to get startup (Celescribe Systems) off the ground as I will be doing contract work outside of my day to day employment.

code snippet
using System;
using System.Collections.Generic;
using System.Text;

namespace ObjectExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //create two instances of the Cookie Class
            Cookie firstCookie = new Cookie();
            Cookie secondCookie = new Cookie();

            //pass our cookies to our method
            Oven.CookCookie(firstCookie);
            Oven.CookCookie(secondCookie);
            
            //Now lets decorate one or our cookies
            secondCookie.Topping = "Chocolate";

            Console.WriteLine("This cookie is {0}", firstCookie.Topping);
            Console.WriteLine("This cookie is {0}", secondCookie.Topping);
            Console.WriteLine();

            //now to sample the cookies
            firstCookie.TakeBite();
            secondCookie.TakeBite();
            Console.ReadKey();

        }
    }

    public class Cookie
    {
        public string Topping;

        public void TakeBite()
        {
            Console.WriteLine("crunch...nom nom nom");
        }

    }

    public static class Oven
    {
        public static int Temperature;

        //Method is static so no instance of Oven Class is needed to call it
        //classes are reference type so rawCookie will contain a pointer to our class in memory
        public static void CookCookie(Cookie rawCookie)
        {
            //once they are cooked we have plain cookies
            rawCookie.Topping = "Plain";
        }
    }
}
ddanbe 2,724

Makes one get hungry and wanting for more!!!:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.