Showing posts with label Programming in C Plus Plus. Show all posts
Showing posts with label Programming in C Plus Plus. Show all posts
Constructor and De-constructor

Constructor and De-constructor

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;
-------------
-------------
}





Inline Function in C++

Inline Function in C++

Inline Function-



C++ เคฎें เคœเคฌ เคเค• function เค•ो เค•เคˆ เคฌाเคฐ call เค•िเคฏा เคœाเคฏे เคคो เค•เคˆ เคธाเคฐा memory space เคคเคฅा time เคฒเค—เคคा เคนै เคชเคฐंเคคु function เค›ोเคŸा เคนो เคคो execution time เค•ो เค•เคฎ เค•เคฐเคจे เค•ा เคคเคฐीเค•ा เคนै macros เค•ा เค‡เคธ्เคคेเคฎाเคฒ।

            C เคฎें macros เค•ा เค‡เคธ्เคคेเคฎाเคฒ เค•िเคฏा เคœाเคคा เคฅा เคชเคฐंเคคु เค‡เคธเค•ा เคฏเคน disadvantage เคนै เค•ि macros real function เคจเคนी เคนोเคคे เคนै เคคเคฅा compile เค•เคฐเคคे เคธเคฎเคฏ เค‡เคจเคฎे error check เคจเคนी เคนोเคคी เคนै।
            Inline Function C++ เคฎें function เค•ो call เค•เคฐเคจे เค•ी process เค•ो เค†เคธाเคจ เค•เคฐเคคे เคนुเค เคŠเคชเคฐ เคฆी เค—เคฏी เคธเคฎเคธ्เคฏा เค•े เคธเคฎाเคงाเคจ เค•ो เคช्เคฐเคฆाเคจ เค•เคฐเคคा เคนै, เค•िเคธी เคญी function เค•ो inline เคฌเคจाเคจे เค•े เคฒिเค function เค•े เค†เค—े invoke เคถเคฌ्เคฆ เค•ो เคฒिเค– เคฆिเคฏा เคœाเคคा เคนै।

Inline function เค•ो เคœिเคธ line เคฎें เคฒिเค–ा เคœाเคคा เคนै เคตเคน invoke เคนुเค† เคนो เคตเคนी เคชเคฐ expand เค•เคฐเค•े define เค•िเคฏा เคœा เคธเค•เคคा เคนै।

Inline Function เค•ी calling direct เคนै เคคเคฅा inline keyword compiler เค•ो command เคจเคนी requst เคญेเคœเคคा เคนै เค•ि เค‰เคธे เค•ाเคฐ्เคฏ เค•เคฐเคจे เคฆिเคฏा เคœाเคฏे เคชเคฐंเคคु เค•ुเค› situation เคชเคฐ inline expansion เค•ाเคฎ เคจเคนी เค•เคฐเคคा เคนै เคœैเคธे เค•ि-
(i) เค…เค—เคฐ inline function Recursive เคนो।
(ii) function เค•े เค…ंเคฆเคฐ static เคฎाเคจ เคนो।
(iii) เค…เค—เคฐ function เค•े เค…ंเคฆเคฐ value return เคนो เคฐเคนी เคนो เคคเคฅा เค‰เคธเคจे switch เคฏा goto เค•ा เคช्เคฐเคฏोเค— เค•िเคฏा เคนो।




Example- 
#include<iostream.h>
Inline float add (float a, float b)
{
return(a+b);
}
int main()
{
float a=5.4
float b=7.4
Cout<<add(a,b)
return 0;
}



เคœเคพเคจเฅ‡ เคธเฅ€++ เคฎเฅ‡เค‚ เคซเฅเคฐเฅ‡เค‚เคก เคซเค‚เค•เฅเคถเคจ เค•เฅเคฏเคพ เคนเฅ‹เคคเคพ เคนเฅˆ เคคเคฅเคพ เค‡เคธเค•เฅ‡ เค•เฅเคฏเคพ เค•เฅเคฏเคพ เคฒเคพเคญ เคนเฅ‹เคคเฅ‡ เคนเฅˆเฅค

เคœाเคจे เคธी++ เคฎें เคซ्เคฐेंเคก เคซंเค•्เคถเคจ เค•्เคฏा เคนोเคคा เคนै เคคเคฅा เค‡เคธเค•े เค•्เคฏा เค•्เคฏा เคฒाเคญ เคนोเคคे เคนै।

Friend Function -


C++ เคฎें friend function เค•ा เค‰เคชเคฏोเค— เค•िเคฏा เคœा เคธเค•เคคा เคนै, Friend function เค•ी เคจिเคฎ्เคจเคฒिเค–िเคค characterstics เคนोเคคी เคนै।



★ friend function เค•ो class เค•े object เค•े เคœเคฐिเคฏे access เคจเคนी เค•िเคฏा เคœा เคธเค•เคคा เคนै।
★ เค‡เคธเค•ो เค•िเคธी เคญी normal function เค•ी เคคเคฐเคน เคฌिเคจा object เค•े invoke เค•िเคฏा เคœा เคธเค•เคคा เคนै।
★ เค•िเคธी function เค•ो เค•िเคคเคจी เคญी class เคฎें friend define เค•िเคฏा เคœा เคธเค•เคคा เคนै।
★ friend function เค•ो class เค•े public เคฏा private เค•िเคธी เคญी part เคฎें define เค•िเคฏा เคœा เคธเค•เคคा เคนै।
★friend function เค•ो เค•िเคคเคจी เคญी class เคฎें friend define เค•िเคฏा เคœा เคธเค•เคคा เคนै।

■ เค•िเคธी เคญी function เค•ो friend function เคฌเคจाเคจे เค•े เคฒिเค function เค•े เค†เค—े Friend เคฒिเค–ा เคœाเคคा เคนै।

Example- Friend void display(N);
เคœเคนा N เค•ोเคˆ object เคนोเค—ा।




Pure Virtual Function

Pure Virtual Function

Pure Virtual Function -

เค…เค—เคฐ เคนเคฎ base class เคฎें เค•िเคธी function เค•ो define เค•เคฐे เคคเคฅा เค‰เคธเค•ी เคชुเคจः definition derived class เคฎें เคฆी เคœाเคฏे เคคो base class เค•ा เคตเคน function do-nothing เคช्เคฐเค•ाเคฐ เค•ा เคนै, เค‡เคธ เคช्เคฐเค•ाเคฐ เค•े function เค•ो Pure Virtual Function เค•เคนा เคœाเคคा เคนै เคคเคฅा เคเคธी classes เคœिเคจเคฎें Pure Virtual Function เคฌเคจा เคนो Abstract Class เค•เคนเคฒाเคคी เคนै।

//virtual void display()=0;


Example-
class fruit      //base class
{
//virtual void display()=0;
}
class apple : public fruit  // abstract class
{
void display()
{
Cout<<"this is a Pure Virtual Function";
}
}



เคตเคฐเฅเคšเฅเค…เคฒ เคซเค‚เค•เฅเคถเคจ เค•เฅเคฏเคพ เคนเฅ‹เคคเคพ เคนเฅˆ เคเคตเค‚ เค‡เคธเค•เฅ‡ เค•เฅเคฏเคพ เค‰เคชเคฏเฅ‹เค— เคนเฅ‹เคคเฅ‡ เคนเฅˆเฅค

เคตเคฐ्เคšुเค…เคฒ เคซंเค•्เคถเคจ เค•्เคฏा เคนोเคคा เคนै เคเคตं เค‡เคธเค•े เค•्เคฏा เค‰เคชเคฏोเค— เคนोเคคे เคนै।

Virtual Function-


Virtual Function เค•ा use run time polymorphism เค•ो เคช्เคฐाเคช्เคค เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।
เค…เค—เคฐ เคนเคฎाเคฐे เคชाเคธ เคฆो class เคนो เคเค• base class เคคเคฅा เคฆूเคธเคฐी derived class เคนो เค”เคฐ เคนเคฎ pointer เค•ा เค‡เคธ्เคคेเคฎाเคฒ เค•เคฐเค•े derived class เค•े object เค•ो access เค•เคฐเคจा เคšाเคนเคคे เคนो เคคो เคเคธा เคจเคนी เคนो เคชाเคคा เคนै เค•्เคฏोंเค•ि base class เค•ा pointer เคนเคฎेเคถा base class เค•े function เค•ो เคนी execute เค•เคฐेเค—ा।
เค‡เคธ problem เค•ो solve เค•เคฐเคจे เค•े เคฒिเค virtual function use เค•िเคฏा เคœाเคคा เคนै।

ukpolytechnicnotes.blogspot.com

            Base class เค•े function เค•ो virtual เคฌเคจाเคฏा เคœाเคคा เคนै เคœिเคธเค•े เคฒिเค base class เค•े เค†เค—े Virtual เคถเคฌ्เคฆ เคฒिเค– เคฆिเคฏा เคœाเคคा เคนै เคœเคฌ เค•ोเคˆ function virtual เคฌเคจा เคฆिเคฏा เคœाเคคा เคนै เคคो C++ base pointer เคฆ्เคตाเคฐा point เค•िเคฏे เค—เค object เคธे เคฏเคน เคจिเคฐ्เคงाเคฐिเคค เค•เคฐเคคी เคนै เค•ि เค•िเคธ function เค•ो เคšเคฒाเคจा เคนै।

Example-
class base
{
void C()
{
Cout<<"C is here";
}
Virtual void C++()       //virtual Function
{
Cout<<"C++ is here";
}
}
class derived : Public base
{
void C()
{
Cout<<"C is there";
}
void C++()
{
Cout<<"C++ is there";
}
}
void main()
{
base.b1;       //object
derived d1;    //object
base*pptr;      //pointer
bptr=&b1;
bptr->C();      // C is here
bptr->C++();   C++ is here
bptr=&d1;
bptr->C();      // C is here
bptr->C++();   //C++ is here
//Output virtual Function
}

derived class เค•े object C function เค•ो class เค•เคฐเคจे เคชเคฐ base class เค•ा เคนी output เค†เคฏेเค—ा, เค•्เคฏोเค•ि base pointer เคนเคฎेเคถा base class เค•े function เค•ो เคนी เคฒाเคคा เคนै เคชเคฐंเคคु เคฏเคฆि base class เค•ा function virtual เคนो เคœैเคธा เค•ि C++ name เค•ा function เคนै เคคो derived class เค•ा function call เคนो เคœाเคคा เคนै।

ukpolytechnicnotes.blogspot.com