HSC Science (Computer Science) 12th Board ExamMaharashtra State Board
Share
Notifications

View all notifications

What is Constructor and Destructor in c++ ? Give example of Constructor and Destructor in a class. - HSC Science (Computer Science) 12th Board Exam - Computer Science 1

Login
Create free account


      Forgot password?

Questions

What is Constructor and Destructor in c++ ? Give example of Constructor and Destructor in a class.

Explain Constructor and Destructor with the example in C++

Solution 1

constructor :

A class constructor is a special member function of a class that is executed whenever we create new objects of that class.

A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

destructor :

A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.

A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.

example of Constructor and Destructor in a class:

#include

class country
{
    public:

    country()
    {
        std::cout<<"\n Constructor called \n";
    }

    void setNumOfCities(int num);
    int getNumOfCities(void);

    ~country()
    {
        std::cout<<"\n Destructor called \n";
    }

    private:

    int num_of_cities;

};

void country::setNumOfCities(int num)
{
    num_of_cities = num;
}

int country::getNumOfCities(void)
{
    return num_of_cities;
}

int main(void)
{
    country obj;
    int num = 5;

 num = obj.getNumOfCities();
 obj.setNumOfCities(num);

    std::cout<<"\n Number of cities is equal to "<<num;

    return 0;
}

When this code is executed, here is the output :

 $ ./cnstrDestr 

 Constructor called 

 Number of cities is equal to 134514633
 Destructor called

Solution 2

Constructor :

  • A constructor is a function whose task is to initialize the objects of its class
  • It is a special function as the class name and the constructor name is same.
  • It is called automatically when the object of the class is created

Destructor :

  • The destructor is a function that is used to destroy the objects that have been created by a constructor.
  • Destructor function name is the same as constructor name with tilde sign (~).
  • A destructor is called as the end of program execution.

Example:

class circle
 { private:
   int rad;
   public:
   circle( ){ rad=5;} // constructor declared and defined
   ~circle( ) { delete rad; } // Destructor declared and defined
 };
  Is there an error in this question or solution?

APPEARS IN

 2014-2015 (March) (with solutions)
Question 3.2.2 | 4.00 marks
 2017-2018 (March) (with solutions)
Question 3.1.2 | 3.00 marks
Solution What is Constructor and Destructor in c++ ? Give example of Constructor and Destructor in a class. Concept: C++ Programming.
S
View in app×