When an object
memory is being utilized by multiple objects and references in such case if
NULL is assign to any of them still that memory can be utilized by others who
are pointing the memory.
To test this
rewrites the code under main method of class first as following.
{
First f1 = new First();
First f2 = f1;
f1 = null;
Console.WriteLine(f2.x);
Console.WriteLine(f1.x); //invalid
Console.ReadLine();
}
whenever the
object of a class is created internally following thing tack place.
- Reads the classes - in this process it identify each and every member i.e. defined in the class.
- Invokes the Constructors of classes.
- Allocate the required memory for execution.
Constructor
– It is a special method i.e. present in
every class responsible for initializing the variable of class.
A constructor
method will always have the same name of your class and it is non value
returning.
Every class
must and should required constructor in it, if it all, it has to execute. so a
programmer must define a constructor in the class if not defined , on behalf of
the programmer compiler tack the responsibility of defining constructor in the
class while compilation .
If a class
is defined as following:
Class Test
{
int x; string s; bool b;
}
After
compilation it will be looking like as –
class Test
{
x = 0; s = null; b = false;
}
Note – Implicitly defined constructor will
always be public.
We can also
define a constructor explicitly as following –
[<modifier>]<NAME>([<param def’s>])
{
- stmts
}
Add a class
ConDemo.cs and write the following :-
using System;
Namespace oopsProject
{
class ConDemo
{
ConDemo()
{
Console.WriteLine("constructor");
}
void Demo()
{
Console.WriteLine("method");
}
static void Main ()
{
ConDemo cd1 = new ConDemo();
ConDemo cd2 = new ConDemo();
ConDemo cd3 = cd2;
cd1.Demo(); cd2.Demo();
cd3.Demo();
Console.ReadLine();
}
}
}
While creating
the object of class we explicitly call the constructor of the class to execute
.
ConDemo.cd1 = new ConDemo(); in this we are making the constructor.
- Each time we
call the constructor of the class that many no. of times memory allocation is
also takes 3place.
Constructors is two types
- zero Argument / default constructor
- Parameterized constructor
If a constructor doesn’t
have any parameters to it. It is default or zero argument constructors. These
constructor can be defined either by a programmer or will be defined by
compiler implicitly, provided the class doesn’t contain any constructor in it.
If a constructor has any
parameter it is referred as parameterized constructor and these can be defined
only by programmer explicitly
If a constructor is
parameterized we need to send values to the parameters while creating object of
class. To test this rewrites the constructor in our program ConDemo as
following.
ConDemo(int x)
{
Console.WriteLine("constructor :" + x);
}
Now while creating the
object under main we need to send values to the constructor as following
ConDemo cd1 = new ConDemo(10);
ConDemo cd2 = new ConDemo(20);
- Add the
class Maths.cs and write the following .
using System;
namespace oopsProject
{
class Maths
{
int x, y;
Maths(int a, int b)
{
x = a;
y = b;
}
void Add()
{
Console.WriteLine(x + y);
}
void Sub()
{
Console.WriteLine(x - y);
}
void Mul()
{
Console.WriteLine(x * y);
}
void Div()
{
Console.WriteLine(x/y);
}
static void Main ()
{
Maths obj = new Maths (100,50);
obj.Add(); obj.Sub();
obj.Mul(); obj.Div();
Console.ReadLine();
}
}
}
Whenever a
class required any initialization values for its variable to execute we need to
pass value to the variables using constructor of the class. In our above
program the class required values to the variable x and y for execution of the
four methods on the class. So using constructor we have send values to the
variables, so that the method are getting executed.
No comments:
Post a Comment