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