Classes and Objects

What is a Class?

In the simplest terms, a Class defines an Object. A Class is used to define the characteristics of an Object. In other words, a Class describes what an object is made of and what it can do.

The characteristics of a class are defined by its properties. What it can do is defined by its methods. Consider the following Box class. It has the following three properties:

  • Length
  • Width
  • Height

This Class has one method called DecreaseBoxHeight. This means that it can do one thing right now, and that is Decrease the height of the box. More methods can easily be added to manipulate the width and length in a similar fashion.

namespace Shapes
{
        class Box
        {
            private int m_iLength; //member variable
            private int m_iWidth; //member variable
            private int m_iHeight; //member variable

            public int Length //property
            {
                get {return m_iLength;}
                set {m_iLength = value;}
            }

            public int Width //property
            {
                get {return m_iWidth;}
                set {m_iWidth = value;}
            }

            public int Height //property
            {
                get {return m_iHeight;}
                set {m_iHeight = value;}
            }   

            public Box()
            {
                //Default Constructor
            }

            public void DecreaseBoxHeight() // method
            {
                if(m_iHeight > 1)
                {
                    m_iHeight--; //decrease height
                }
            }
//End of method
        }
//End of class
    }//End of namespace

What’s the difference between a Class and an Object?

As stated before, a Class defines an Object. A Class is not an Object. Another way of looking at it is that a Class is a blueprint for an Object. A class cannot be used unless you instantiate it (create an instance of it). When you create an instance of a Class, you have an Object that you can use.

The one exception to the statement that I just made, “A class cannot be used unless you instantiate it“, is with static classes. You can not instantiate a static class. If you create a static class, you just use it. I will talk more about static classes in another post.

Don’t worry if this doesn’t make sense right now. We will work through an example.

Let’s Create a Box!

If we want to create a Box, we have to Instantiate it, or create a NEW Box. We do that using the new keyword like this.

Box cBox = new Box();
cBox.Length = 10;
cBox.Width = 10;
cBox.Height = 10;

We have created an Instance of a box called cBox. Notice that we used the new keyword to create an Instance of the Box Class. The Object that was created from the Box class is represented by the variable cBox. cBox is a variable that was created to represent the Box class. We could have used any name for the variable, but I chose cBox because it is easy to recognize in code. To me, it means class box, where the c means class.

Have a look at the full code for the Box class. Notice that the name of the class is Box. Since the Box class defines a Box, when we want new boxes, we have to create new boxes.

We went on to define the dimensions of cBox by setting the Length, Width, and Height to 10, so we have a Cube shape. Length, Width, and Height are public properties of the Box Class. When we Set those Properties, we are also setting values on m_iLength, m_iHeight, and m_iWidth. Notice the following properties in the Box class that Set the values of m_iLength, m_iHeight, and m_iWidth.

public int Length //property
{
   get {return m_iLength;}
   set {m_iLength = value;}
}

public int Width //property
{
   get {return m_iWidth;}
   set {m_iWidth = value;}
}

public int Height //property
{
   get {return m_iHeight;}
   set {m_iHeight = value;}
}   

We can decrease the height of cBox by calling its method DecreaseBoxHeight.

Manipulate cBox by Calling a Method.

cBox.DecreaseBoxHeight();

Since cBox represents a Box object, we can access it’s public properties and methods. Notice that DecreaseBoxHeight is a public method.

public void DecreaseBoxHeight() // method
{
   if(m_iHeight > 1)
   {
      m_iHeight--; //decrease height
   }
}
//End of method

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: