Constructor and Destructor In Hindi

C++ In Hindi – C++ Constructor and Destructor In Hindi

C++

Constructor and Destructor In Hindi :- C++ Constructor Ek special type Ka member function Hota Hai, Jo Aapne class Ke Name Jaisa Hota Hai | And C++ Destructor Ye Bhi Ek special type member function Hota Hai, Jo object Ko destroy Kar Deta Hai |

Previous Post Also Read :- What is Classes & Object Etc

C++ Constructor and Destructor In Hindi

Contents

Introduction for Constructor

C++ Constructor Ek special type Ka member function Hota Hai, Jo Aapne class Ke Name Jaisa Hota Hai Jis Class Ka Constructor Bana Ho, Agar Usi Class Ka Jab Object Jab Banta hai | Tab Vo Automatically Call Hota Hai | Constructor ka koi bhi return type nhi hota hai | Void Bhi Retrun Nhi Karta Hai, Agar Koi Value Ko Initialize Karna Ho to Alag se Uska Member Function Bana kar Use Object Se Sath Access Karta Padta Hai | Constructor Ye Kam Sirf Object banate waqt hi kar deta hai |

Constructor ko Ham Class Ke Under And Bahar Bhi Define Kar Sakte Hai |

Inside Constructor Example:

class test{
	private:
	 --------  // private body;
      
	public :
	test() // Constructor
    {         		
	       //constructor body;	
	}
};

Outside Constructor Example:

class test{
	private:
	--------   // private body;
	
	public :
	test ();   //Constructor declaration
};

test::test()   //Constructor Definition
{
 --------//Constructor body;
}

Hamne Ab Tak Constructor ka Syntax Ko Dekha Hai Ab Ham Ek Program Create Karte Hai Hai Constructor Ka |

Example:

#include <iostream.h>
using namespace std;

class Student
{
public :
    Student()  //Constructor
    {    
    cout<<"Hello World!";
    }
};

int main(){

Student e;

return 0;
}

Output:

Hello World!

Types Of Constructor Function in C++ In Hindi

C++ Constructor Waise 4 Types Ke Hote Hai |

  1. Default Constructor
  2. Parameterized Constructor
  3. Default Copy Constructor
  4. Constructor Overloading
  1. Default Constructor

Default Constructor Sabse Simple Type Ka Hota Hai | Iska Istemal Objects Ko Ek Fix Value Se Initialize Karne Ke Liye Kiya Jata Hai |

Example:

#include <iostream.h>
using namespace std;

class Test
{
public :
    Test(){    //Constructor
    cout<<"This Is Default Constructor"<<endl;
}
};

int main(){

Test a1, a2;

return 0;
}

Output:

This Is Default Constructor
This Is Default Constructor

2. Parameterized Constructor

Parameterized Constructor Ek Aisa Constructor Hota Hai, Jiski Help Se Ham Parameter Ko Pass Karke Object Ko Initialize Kar Sakte Hai |

Example:

#include <iostream.h>
using namespace std;

class test{

private:
    int a, b, c;

public :
    test(int x, int y){    //Constructor
    a = x;
    b = y;
    c = a + b;
}
void display(){
    cout<<"Addition of "<<a<<" and "<<b<<" is "<<c;
}
};

int main(){

test a(5, 8);
test.display();

return 0;
}

Output:

Addition of 5 and 8 is 13

3. Default Copy Constructor

Copy Constructor Se Object Ko Create Kar Skate Hai | Default Copy Constructor Mai Ham Pahle Constructor Object Ko kisi Dusre Object Par Copy Kar Sakte Hai | Or Jo Data Pahle Constructor Ka Hota Hai Wohi Data Dusre Constructor Mai Bhi Copy Hota Hai |

Example:

#include <iostream.h>
using namespace std;

class test{

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(){

test a1(5, 6);
test a2(a1);     // Copy Constructor a1 to a2
test 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 Dynamic & Overloading

Constructor Overloading Mai Class Mai Multiple Constructor Overloading ki ja Sakte Hai. Sirf Unke Parameters ki Count Or Uske Type Different Hote Hai |And Dynamic Constructor Ka Istemal Dynamic memory Allocation Mai Kiya Jata Hai | Iski Help Se Object Ko Run Time Mai Banaya Jata hai | Ise Dynamic Creation of Object Bhi kaha Jata Hai |

Example:

#include <iostream.h>
using namespace std;

class test
{
private:
    int a, b;

public:
    test(){
    a = 2;
    b = 3;
}
    test(int x, int y){
    a = x;
    b = y;
}
    void show(){
    cout<<"a : "<<a<<endl;
    cout<<"b : "<<b<<endl;
    }
};
int main(){

test 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

Characteristics of Constructor Function in C++ In Hindi

  • Ise Hamesha Class Ke Public Section Mai Hi Declare Kiya Jata Hai |
  • Inhe Alag Se Call karne Ki Jarurat Nhi Hoti Hai | jab Us Class Ka Object Banta Hai, To Yah Automatic Hi Call Ho Jata Hai |
  • Iska Koi Retrun Type Nhi Hota Hai |
  • Inko Inherit Nhi Kiya Ja sakta Hai |
  • Constructor Function Virtual Nhi Ho Sakta Hai |
  • Constructor Function Ko Point BHi Nhi Kiya Ja Sakta Hai |

Introduction for Destructor

Destructor Ye Ek Special Type Ka Member Function Hota Hai, Jo Object Ko Destroy ya Delete Kar Deta Hai | Jab Object Program Se Bahar Ho Jata Hai,tab Destructor Automatically Call Hota Hai | Destructor Or Constructor Ke Parameters Nhi Hote | Destructor Ko ham Prefix ~(tilde) sign ke Sath Istemal Hota Hai |

Example:

#include <iostream.h>
using namespace std;

class test{
    int a;

public:
    test(int x){       //Constructor
    a = x;
    cout<<"Constructor is created."<<endl;

}
    ~test(){           //Destructor
    cout<<"Constructor is deleted."<<endl;
}
void show(){
    cout<<"Value of a : "<<a<<endl;
}
};
int main(){

test a(10);
a.show();

return 0;
}

Output:

Constructor is cretaed.
Value of a : 10
Constructor is deleted.

Next Post Also Read :- What is Function & Parameters

Leave a Reply

Your email address will not be published. Required fields are marked *