Monday, October 3, 2011

Create the OBJECT of a class


As per encapsulation method has to be defined inside a class and to invoke them we need to create object of that class.
We create the OBJECT of a class as following –
<class><obj.> = new <class> ([<list of values > ])
Program p = new Program ();
Or
Program p;
p = new Program();
Object of a class can be created anywhere within the class it can also be created in other classes also, but while creating in a class we create under main method because it is the main entry point of your program.
Open visual Studio  click new Project  select Console application Template  name it as oops project and click ok . Now write following code under the default class program.

using System;
namespace oopsProject
{
class Program
{
//No input and no return value
void test1()
{
Console.WriteLine("First Method ");
}
// No Return value but has input
void test2(int x, int max)
{
for (int i = 1; i <= max; i++)
Console.WriteLine("{0}* {1} = {2}", x, i, x * i);
}
// NO input but has a return value
string test3()
{
return "Third method";
}
// has input and return value
string test4(string name)
{
return "hello" + name;
}
// named and optional parameter (C# 4.0)
void Addnums(int x, int y = 50, int z = 25)
{
Console.WriteLine(x + y + z);
}
// Returns Multiple values as output
void Math1(int x, int y, ref int a, ref int b)
{
a = x + y;
b = x * y;
}
void Math2(int x, int y, out int a, out int b)
{
a = x - y;
b = x / y;
}
static void Main(string[] args)
{
Program p = new Program();
p.test1();
p.test2(5, 5);
Console.WriteLine(p.test3());
Console.WriteLine(p.test4("Prabodh"));
// Invoking method with default value can
//be done in any of the following way :
p.Addnums(100);
p.Addnums(100, 100);
p.Addnums(100, z: 100);
p.Addnums(100, 100, 100);
// Invoking method with input & output params :
int m = 0, n = 0; //initializing is mandatory
p.Math1(100, 50, ref m, ref n);
Console.WriteLine(m + " " + n);
int q, r; //Initialization is optional
p.Math2(100,50, out q, out r);
Console.WriteLine(q + " " + r);
Console.ReadLine();
}
}
}
- In C# 4.0 we are given with the feature named and optional parameters, which allows passing default values to any parameter of your method. The advantage in this case is while invoking the method it will only be optional to pass values to those parameters.

The execution of a method with output parameters tacks place as following –
A variable i.e. declared using ref or out keyword is a pointer variable, which doesn’t contain any values in it , what it contains is address of another variable but any manipulation we perform onref or out variables will be affected to the variable whose address they contain in them.

Note – A variable declared as ‘ref’ required initialization while invoking the method where as in case of ‘out’ it is optional.
Add a class.cs under the project and write the following code
class First
{
int x=100;
static void Main ()
{
First F; //F is variable
F = new First(); // f is an object
Console.WriteLine(F.x);
Console.ReadLine();
}
}
- A variable or a method which is declared in a class is referred as member of the class where to invoke a member we must create the Object of the class (NOT required for static members)
- The object of a class can be created only with the use of new operator. If new is not used there will not be any memory allocation for the object and more over it is only consider as variable of the class.
We can destroy the object which got created at any particular point of time by assigning NULL to it so that object gets marked as unused and cannot be used any more to invoke the members.

- To test this rewrite the following code under you privies class first main method.
First F = new First();
Console.WriteLine(F.x);
F = null; //destroying the object
Console.WriteLine(F.x); //Invalid
Console.ReadLine();
- We can create any number of objects to a class where each object will create will allocate the memory separately for each members and manipulations performed on members of one object will not be reflected to members of other object
- To test this rewrites the code under main method of class first as following.
First F1 = new First();
First F2 = new First();
Console.WriteLine(F1.x + “ ” +F2.x );
F1.x = 500;
Console.WriteLine(F1.x + “ ” +F2.x );
Console.ReadLine();
- We can assign object of a class to the variable of some class. So that the variable will be consuming the memory of the object which was assign to it. Once the assignment is performed the variable is now referred as a reference.
- Because the object and reference are consuming the same memory manipulation performed on the members through object reflect to reference and Vis...
- To test this rewrites the code under main method of class first as following.
First F1 = new First();
First F2 = F1;
Console.WriteLine(F1.x + “ ” +F2.x );
F1.x = 500;
Console.WriteLine(F1.x + “ ” +F2.x );
Console.ReadLine();

No comments: