Destructor - Features,Properties

Q.  Which of the following statements are not true about destructor?

1. It is invoked when object goes out of the scope
2. Like constructor, it can also have parameters
3. It can be virtual
4. It can be declared in private section
5. It bears same name as that of the class and precedes Lambda sign.

- Published on 17 Jul 15

a. Only 2, 3, 5
b. Only 2, 3, 4
c. Only 2, 4, 5
d. Only 3, 4, 5

ANSWER: Only 2, 4, 5
 

    Discussion

  • Mansi   -Posted on 07 Mar 19
    Bdiyaaa ????????????
  • ramesh   -Posted on 08 Oct 15
    Destructor has the following properties.
    The destructor has the same name as the constructor, but it is preceded by a ~(tilde).

    • There is no parameter in destructors.
    • Name of the Destructor should be exactly same as that of name of the class. But proceeded by ‘~’ (tilde).
    • Destructor does not have any return type. Not even void.
    • The Destructor of class is automatically called when object goes out of scope.
    • Destructors are used to free the memory of the object whenever it goes out of scope.

    Example:
    class DestructorDemo
    {
    public:
    int a;

    //constructor
    DestructorDemo() {
    cout << " Constructor is called"< cout << "C++ Object created"< }

    //Destructor
    ~DestructorDemo() {
    cout << " Destructor is called"< cout << "C++ Object destructed"< }
    };
    int main( )
    {
    DestructorDemo obj1;
    DestructorDemo obj2;
    return 0;
    }

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)