Constructor and De-constructor

Constructor and De-constructor

Constructor and De-constructor
Ganesh
Monday 10 April 2017

Constructor and De-constructor- 


Constructor-
Constructor เคเค• เคช्เคฐเค•ाเคฐ เค•े special member function เคนै เคœो เค•ि class เค•े object เค•ो initalize เค•เคฐเคจे เค•े เค•ाเคฎ เค†เคคा เคนै เคฏเคน special เค‡เคธเคฒिเค เคนै เค•्เคฏोंเค•ि เค‡เคธเค•ा name เคตเคนी เคนोเคคा เคนै เคœो เค•ि class เค•ा name เคนोเค—ा।
(1) Constructor เค•ो public part เคฎें declare เค•िเคฏा เคœाเคคा เคนै।
(2) เค‡เคจเค•ा เค•ोเคˆ return type เคจเคนी เคนोเคคा เคนै।
(3) เค‡เคจเค•ो inherit เคจเคนी เค•िเคฏा เคœा เคธเค•เคคा เคนै।

Types of Constructor-
Constructor เคฆो เคช्เคฐเค•ाเคฐ เค•े เคนोเคคे เคนै।
(I) Default constructor
(II) Parameterized constructor

(I) Default constructor- เค‡เคจเค•ा เค‰เคชเคฏोเค— object เค•े เคธाเคชेเค•्เคท scope resolution operator (::) เค•े เคฆ्เคตाเคฐा เค•िเคฏा เคœाเคคा เคนै।
เคฏเคน constructor เค…เคชเคจे เคชाเคธ เค•ोเคˆ argument เคจเคนी เคฐเค–เคคे เคนैं।
Example-
Class fruit
{
int x,y;
Public:
fruit();
-------------
-------------
};
fruit :: fruit()
{
x=5;
y=3;
----------
----------
}


(II) Parametrized constructor- เค‡เคธเคฎें constructor argument เคฒे เคธเค•เคคा เคนै। Argument เค•ो value เคช्เคฐเคฆाเคจ เค•เคฐเคจे เค•े เคฒिเค constructor เค•ो call เค•िเคฏा เคœाเคคा เคนै।
Example-
Class fruit
{
int x,y;
Public:
fruit(int a, int b);
-------------
-------------
};
fruit :: fruit(int a, int b)
{
x=a;
y=b;
----------
----------
}





De-Constructor- 
Constructor เคฆ्เคตाเคฐा เคฌเคจाเคฏे เค—เคฏे objects เค•ो destroy เค•เคฐเคจे เค•े เคฒिเค
De-constructor เค•ा use เคนोเคคा เคนै।
De-Constructor เคญी เคเค• member function เคนै, เคœिเคธเค•ा name class name เคนी เคนोเคคा เคนै เคคเคฅा constructor เค•เคญी เคญी เค•िเคธी เคญी เคช्เคฐเค•ाเคฐ เค•ा argument เคจเคนी เคฒेเคคा เคนै เคจ เคนी เค•ोเคˆ value return เค•เคฐเคคा เคนै।
◆De-Constructor เค•े name เค•े เค†เค—े tilde(~) เค•ा เคช्เคฐเคฏोเค— เค•िเคฏा เคœाเคคा เคนै।
Example-
Class fruit
{
int x, y;
Public:
fruit()
~ fruit();
Cout<<"destroyed";
}
fruit :: fruit(int a, int b)
{
x=a;
y=b;
-------------
-------------
}