Monday, May 27, 2013

Constructors

Constructors are methods of a class which get executed automatically when an instance of a class is created. The constructor method name should be same as the class name.

The following example defines a simple constructor for the class CEmployee

Sunday, May 26, 2013

What is a Struct?

Structs can be used to define custom types, which are build using a combination of the primitive types like int, string etc. For example we can define an Employee Struct which can hold details of the employee id, name, address etc in individual types like int, string etc.

Structs are value types while classes are reference types, which means that every instance of the Struct is completely independent and have their own set of values. If a struct instance A is assigned to struct instance B then both A & B have their own memory and have an independent set of value, hence any change to instance A will not affect instance B.

The below example defines a simple Struct which contains an integer id and a string name representing an employee details.

Saturday, May 25, 2013

What is an Object?

An object is an instance or representation of a class. Objects are the building blocks of Object Oriented Programming, objects give life to classes and structs. Classes are mere definition of a model,  the model gets activated only when an object is created out of the class.

We can create multiple instances of objects from a single class definition; each object is unique and can hold its own set of values. The fields/properties of an object are used to set/get values to the object while the methods of the object are used to perform some operation on the object.


Let us consider the same Calculator class which we defined in the post What is a Class?, here we shall create an instance of the class, set values to the fields and invoke the methods of the object to see how it operates.

    Calculator objCalculator = new Calculator();
    int nResult = objCalculator.Add(5, 6);
    txtResult.Text = nResult.ToString();

Here objCalculator is the object which represents the class Calculator, the object objCalculator invokes the Add() method of the class and passes values to the method. The object performs the calculation which is defined in its base class definition and returns the result of the calculation, which is captured and displayed in the textbox txtResult in the client application.

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#

What is OOP?

OOP stands for Object Oriented Programming, as the name suggests OOP is a type of programming pattern which is governed by OOP principles.

Prior to the introduction of OOP, programming was done using structural programming languages like Fortran, Pascal etc. In structural programming languages the order of execution of the program is sequential, i.e execution happens line by line starting from the first line till the last line in the program is completed.