Saturday, May 25, 2013

What is a Class?


A Class is the basic definition or template of an object, a class defined the properties, methods, events etc of an object. A class is used to represent a real world object/model. For example a calculator can be represented using a Calculator class, the Calculator class defines the properties of a Calculator like input Numbers, operations etc.

A Calculator class just defines the properties and methods of an object, a class does not hold any values of these properties, and to assign values to these we should create an object which represents an instance of the class.

The following example shows a simple Calculator class defined in C#


    class Calculator
    {
        // Private Members
        private int _inputNumber1;
        private int _inputNumber2;
        //
        // Methods
        public int Add(int nNumber1, int nNumber2)
        {
            return nNumber1 + nNumber2;
        }
        //
        public int Subract(int nNumber1, int nNumber2)
        {
            return nNumber1 - nNumber2;
        }
        //
        public int Multiply(int nNumber1, int nNumber2)
        {
            return nNumber1 * nNumber2;
        }
        //
        public int Divide(int nNumber1, int nNumber2)
        {
            return nNumber1 / nNumber2;
        }
    }

The above class contains 2 member variables/fields _inputNumber1 & _inputNumber2. The class also contains 4 methods Add, Subtract Multiply and Divide.

Search Flipkart Products:
Flipkart.com

No comments: