Introduction for Constructor
Constructor ये एक special type का member function होता है, जो अपने class के नाम के जैसा होता है |जिस class का constructor बना हो, अगर उसी class का जब object जब बनता है तब वो automatically call होता है |
constructor का कोई return type नहीं होता | void भी return नहीं करता |
अगर कोई value को initialize करना हो तो अलग से उसका member function बनाकर उसे object के साथ access करना पड़ता है | Constructor ये काम सिर्फ object बनाते ही कर देता है |
Constructor के साथ virtual keyword का इस्तेमाल नहीं किया जाता |
Example for Constructor inside of class
class A{
private:
--------
public :
A(){ // Constructor
//constructor body;
}
};
Constructor को class के बाहर भी define किया जा सकता है |
Example for Constructor outside of class
class A{
private:
--------
public :
A(); //Constructor declaration
};
A::A() //Constructor Definition
{
--------//Constructor body;
}
Example for Constructor
Source Code :
#include <iostream.h>
using namespace std;
class Example
{
public :
Example(){ //Constructor
cout<<"Hello World!";
}
};
int main(){
Example e;
return 0;
}
Output :
Hello World!
Constructor के चार प्रकार होते है |
- Default Constructor
- Parameterized Constructor
- Default Copy Constructor
- Constructor Overloading
1. Default Constructor
Default Constructor कोई parameter या argument नहीं लेता | ऊपर दिया हुआ program Default Constructor का है |Program में 'A' नाम का class है और उसका constructor बनाया गया है | जब A class का object बनेगा तब Constructor automatically call होगा |जितनी बार A class का object बनेगा उतनी बार constructor call होता है |
Source Code :
#include <iostream.h>
using namespace std;
class A
{
public :
A(){ //Constructor
cout<<"Default Constructor."<<endl;
}
};
int main(){
A a1, a2, a3;
return 0;
}
Output :
Default Constructor. Default Constructor. Default Constructor.
2. Parameterized Constructor
Parameterized Constructor में Constructor को parameters pass किये जाते है |Parameterized Constructor में constructor को अलग-अलग arguments दिए जाते है | इसमें arguments की कोई मर्यादा नहीं होती |
Parameterized Constructor में class के object में parameters की values देनी पड़ती है |
निचे दिए हुए program Addition के लिए दो values initialize करके उनका addition किया गया है |
Source Code :
#include <iostream.h>
using namespace std;
class A{
private:
int a, b, c;
public :
A(int x, int y){ //Constructor
a = x;
b = y;
c = a + b;
}
void display(){
cout<<"Addition of "<<a<<" and "<<b<<" is "<<c;
}
};
int main(){
A a(5, 6);
a.display();
return 0;
}
Output :
Addition of 5 and 6 is 11
3. Default Copy Constructor
Copy Constructor से Object को बनाया जाता है | Copy Constructor में पहले Constructor Object को किसी दूसरे Object पर Copy किया जाता है |जो data पहले object में होता है वही data दुसरे Object पर copy होता है |
Copy Constructor में दो तरीके से Object को copy किया जाता है |
A a2(a1); //or A a2 = a1;
Source Code :
#include <iostream.h>
using namespace std;
class A{
private:
int a, b, c;
public :
A(int x, int y){ //Constructor
a = x;
b = y;
c = a + b;
}
void display(){
cout<<"Addition of "<<a<<" and "<<b<<" is : "<<c<<endl;
}
};
int main(){
A a1(5, 6);
A a2(a1); // Copy Constructor a1 to a2
A a3 = a2; // Copy Constructor a2 to a3
a1.display();
a2.display();
a3.display();
return 0;
}
Output :
Addition of 5 and 6 is : 11 Addition of 5 and 6 is : 11 Addition of 5 and 6 is : 11
4. Constructor Overloading
Constructor Overloading में class में multiple Constructor overloading की जा सकती है , सिर्फ उनकी parameters की संख्या और उनके type अलग-अलग होते है |Constructor Overloading; Function Overloading के तरह ही होता है |
Source Code :
#include <iostream.h>
using namespace std;
class A
{
private:
int a, b;
public:
A(){
a = 2;
b = 3;
}
A(int x, int y){
a = x;
b = y;
}
void show(){
cout<<"a : "<<a<<endl;
cout<<"b : "<<b<<endl;
}
};
int main(){
A a1, a2(5, 7);
cout<<"Default Constructor"<<endl;
a1.show();
cout<<"Parameterized Constructor"<<endl;
a2.show();
return 0;
}
Output :
Default Constructor a : 2 b : 3 Parameterized Constructor a : 5 b : 7
Destructor
Destructor ये एक special type member function है, जो object को destroy कर देता है |जब object out of scope जाता है, तब Destructor automatically call होता है |
Destructor; Constructor के तरह ही होता है , लेकिन Destructor में parameters नहीं होते |
Destructor prefix पर ~(tilde) sign के साथ इस्तेमाल होता है |
Syntax for Destructor
~class_name(){
//statement(s);
}
Source Code :
#include <iostream.h>
using namespace std;
class A{
int a;
public:
A(int x){ //Constructor
a = x;
cout<<"Constructor is created."<<endl;
}
~A(){ //Destructor
cout<<"Constructor is deleted."<<endl;
}
void show(){
cout<<"Value of a : "<<a<<endl;
}
};
int main(){
A a(5);
a.show();
return 0;
}
Output:
Constructor is cretaed. Value of a : 5 Constructor is deleted.







No comments: