Monday 21 March 2022

Question- Describe the concept of constructors in java with suitable examples.

 

A constructor in Java is similar to a method that is invoked when an object of the class is created.

जावा में एक कंस्ट्रक्टर एक Method के समान होता है जिसे क्लास का ऑब्जेक्ट बनाते समय Invoked (USE) किया जाता है।

 

Unlike Java methods, a constructor has the same name as that of the class and does not have any return type.

Java Methods के विपरीत, एक कंस्ट्रक्टर का नाम Method के समान होता है और इसका कोई रिटर्न Type नहीं होता है।

 

For example,

 

class Test {

  Test() {

    // constructor body

  }

}

 

Here, Test() is a constructor. It has the same name as that of the class and doesn't have a return type.

यहाँ, टेस्ट () एक कंस्ट्रक्टर है। इसका नाम Class के समान है और इसका कोई रिटर्न Type नहीं होता है।

 

Program for constructor

class Main {
  private String name;
 
  // constructor
  Main() {
    System.out.println("Constructor Called:");
    name = "Programiz";
  }
 
  public static void main(String[] args) {
 
    // constructor is invoked while
    // creating an object of the Main class
    Main obj = new Main();
    System.out.println("The name is " + obj.name);
  }
}

Output:

Constructor Called:
The name is Programiz

 

Sunday 20 March 2022

Question. Explain the concept of new and delete in C++. सी++ में न्‍यू एवं डिलीट कीवर्ड का विवरण दीजिए।


New Operator and Delete Operator

Ø  The new operator not only creates an object but also allocates memory. The new operator allocates correct amount of memory from the heap that is also called as a free store.


न्‍यू ऑपरेटर न केवल एक ऑब्जेक्ट बनाता है बल्कि मेमोरी भी आवंटित करता है। नया ऑपरेटर ढेर से सही मात्रा में मेमोरी आवंटित करता है जिसे फ्री स्टोर भी कहा जाता है।

 

Ø  The delete operators not only destroy the object but also release allocated memory. If the object created is not deleted, it occupies the memory unnecessarily. It is a good habit to destroy the object and release the system resources. Sometimes such mismatch operations may corrupt the heap or may crash the system.

डिलीट ऑपरेटर न केवल ऑब्जेक्ट को नष्ट करते हैं बल्कि आवंटित मेमोरी को भी रिलीज करते हैं। यदि बनाये गये ऑब्‍जेक्‍ट को हटाया/डिलीट नहीं किया जाता है, तो यह अनावश्यक रूप से मेमोरी में पडा(जगह घेरे) रहता है।  उपयोग न होने पर ऑब्‍जेक्‍ट को नष्ट करना और सिस्टम संसाधनों को मुक्त करना एक अच्छी आदत है। कभी कभी ऐसे अनावश्‍यक ऑब्‍जेक्‍ट मेमोरी में जगह घेरे रहने के कारण ढेर/मेमोरी खराब कर सकते हैं या सिस्टम को क्रैश कर सकते हैं।

 

Ø  If we send a null pointer to delete operator, it is secure. Using delete to zero has no result. The statement delete x does not destroy the pointer x. It destroys the object associated with it.

यदि हम ऑपरेटर को हटाने के लिए एक नल पॉइंटर भेजते हैं, तो यह सुरक्षित है। जीरो को डिलीट करने का कोई परिणाम नहीं होता है। स्टेटमेंट डिलीट x,  पॉइंटर x को नष्ट नहीं करता है अपितु उससे जुड़ें ऑब्‍जेक्‍ट को नष्ट करता है।

 

Ø  Do not destroy the pointer repetitively or more than one time. First time the object is destroyed and memory is released. If for the second time the same object is deleted, the object is sent to the destructor and no doubt, it will corrupt the heap.

पॉइंटर को बार-बार या एक से अधिक बार नष्ट न करें। पहली बार वस्तु नष्ट हो जाती है और स्मृति मुक्त हो जाती है। किन्‍तु यदि दूसरी बार वही वस्तु हटाई जाती है, तो वस्तु Destructor को भेज दी जाती है और इसमें कोई संदेह नहीं है कि यह मैमोरी/ढेर को खराब कर देगा।

 

Program to allocate memory to store 3 integers. Use new and delete

operators for allocating and deallocating memory. Initialize and display the values.

 

# include <iostream.h>

# include < conio.h>

int main()

{

clrscr() ;

int i,*p;             // integer variable i and pointer *p are declared

            p= &i;

            p= new int[3] ; // new operator memory for three integers is allocated to p

*p=2;                 // first element

*(p+1)=3         // second element

*(p+2)=4         // third element

cout << “Value Address”;

               

                //for loop is used to display the contents of the pointer *p

            for (int x = 0; x<3; x++)

cout <<endl<<*(p+x)” \t” << (unsigned) (p+x) ;

 

delete []p;       //delete operator releases the memory

return 0;

}

OUTPUT

Values Address

2 3350

3 3352

4 3354