anonymous
  • anonymous
what is the usage of constructors and destructors in c++?
English
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
rhr12
  • rhr12
Constructors and destructors are fundamental to the concept of classes in C++. Both constructor and destructor are more or less like normal functions (but with some differences) that are provided to enhance the capabilities of a class. Constructor, as the name suggests is used to allocate memory (if required) and construct the objects of a class while destructor is used to do the required clean-up when a class object is destroyed. In this article, we will study the concept of constructors and destructors through working examples. If you are new to C++, you should first understand the concept of C++ classes. Constructors and Destructors Let’s understand the concept of constructors and destructors through this example : #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; obj.setNumOfCities(num); num = obj.getNumOfCities(); std::cout<<"\n Number of cities is equal to "<

Looking for something else?

Not the answer you are looking for? Search for more explanations.