A class is a blue print or a template. One or more instances of the template is/are known as object(s).
You can declare a class like below:
A Simple form of Declaration-
public class phone
{
//Some Code here
}
A Class is a combination of member functions (Methods) and member variables (fields). A class can also have properties. And, a property is like member variable but can decide what value to take and what value not to take. Similarly, it can decide how a value can be retrieved.
The real world representation considers methods as the object's behavior and fields as the state of the object.
Let us consider the below example:
using System;
class Phone
{
private string model;
private string make;
private int cost;
static void Main(string[] args)
{
}
}
The phone class has three states now. These member variables will say model, make and cost of the phone. Static void main is the entry point of the console program execution. To create an object based on the above template that is to create an object of the above class, use the following statement:
Phone myphone = new Phone();
Here,
myphone - The actual object
Phone - the template
new - Keyword that tells we want to create a real object and allocate space required for it.
To initialize the class fields we can use a special method called constructor. The constructor is called when the object is created. If there is no parameters in the constructor, then we can say it is a default constructor. When translating your class the compiler makes sure to add a default constructor (Parameter less constructor) when it is not available; that is, when it is not implemented by you.
class Phone
{
//001: Adding fields
private string model;
private string make;
private int cost;
//002: Adding a default constructor
public Phone()
{
model = "823CK";

Nimesh PatelPosted Apr 8, 2011, 4:58 PM
Can you show this class diagram? Its nice article. I think its necessary to code from uml diagram whenever we code class? Do you agree? I want to know the developer process when he faced with real world problem? Excellent article!!! Thanks, Nimesh
Satyapriya NayakPosted Oct 22, 2010, 4:52 AM
Hi friend, Nice work,Can you add some more Oops Conceps. Thanks