Class Defination
A class is a construct that enables you to create your own
custom types by grouping together variables of other types, methods and events.
A class is like a blueprint or plan, or template, that describes the details of
an object. It defines the data and behavior of a type.
Class
Description
Classes are declared by using the keyword class followed by the
class name and a set of class members surrounded by curly braces. Every class
has a constructor,
which is called automatically any time an instance of a class is
created. The purpose of constructors is to initialize class members when an
instance of the
class is created. Constructors do not have return values and
always have the same name as the class. one class may contain fields and
methods(Function)
So far, the only class members you've seen are Fields, Methods,
Constructors, and Destructors. Here is a complete list of the types of members
you can have
in your classes:
• Constructors
• Destructors
• Fields
• Methods
• Properties
• Indexers
• Delegates
• Events
• Nested Classes
Real life
Example of a Class
suppose you want to build a house then fast you have to make a
plan of your number of bedroom,bathroom,kitchen and their position with in your
land.That means
you have to make a blue print of your house which is nothing but
class and your house is the Object of this class.
.Net code
of a Class
///Class declaration
public class employee
{
///Fields
private string _name;
private int _salary;
///Constants
private const int anualBonus = 1000;
///Constructor
public employee()
{
}
///Properties
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public int Salary
{
get
{
return _salary;
}
set
{
_salary = value;
}
}
/// Event handlers
public event EventHandler OnPromotion
{
add
{
}
remove
{
}
}
/// Methods
public void DuplicateSalary()
{
_salary = _salary*2;
}
}
// Program start class
class ExampleClass
{
// Main begins program execution.
public static void Main()
{
// Instance of employee class
employee EmCl = new employee ();
//Set Property value
EmCl.Salary =21000;
EmCl.Name
="Mithun"
// Call employee class' method
EmCl.DuplicateSalary();
}
}
Object
Objects are the building blocks
of OOP and are commonly defined as variables or data structures that
encapsulate behavior and data in a programmed unit. Objects are items that can
be individually created, manipulated, and represent real world things in an
abstract way.
Object
composition
Every object is composed by:
• Object identity: Means
that every object is unique and can be differentiated from other objects. Each
time and object is created (instantiated) the object identity is defined.
• Object behavior: What
the object can do. In OOP, methods work as functions that define the set of
actions that the object can do.
• Object state: The data
stored within the object at any given moment. In OOP, fields, constants, and
properties define the state of an object.