Question

1.Assume we've instantiated a static object named xyz of some class. When will xyz go out...

1.Assume we've instantiated a static object named xyz of some class. When will xyz go out of scope?

2.Assume we've instantiated a dynamic object of some class. We used a pointer variable named p. Code a statement to free the memory occupied by abc.

Homework Answers

Answer #1

1. Destructors destroy the object whenever the object goes out of scope. It has the same name as that of the class with a tilde (~) sign before it.

class xyz
{
~xyz();
};

Here, ~xyz() is the destructor of class xyz. Destructors don't take any argument and have no return type.

A destructor gets automatically called when the object goes out of scope. We know that a non-parameterized constructor gets automatically called when an object of the class is created. Exactly opposite to it, a destructor gets called when the object goes out of scope and destroys the object.

Note that the destructor will get automatically called even if we do not explicitly define it in the class.

2. Clean up memory with operator 'delete'. Apply to the pointer. Use 'delete [ ]' form for arrays:

delete ptr; // deallocates the integer allocated above

delete [ ] nums; // deallocates the double array

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these classes: Product, Customer, and Store. All data members of each class should be marked as **private** (a leading underscore in the name). Since they're private, if you need to access them from outside the class, you should do so via get or set methods. Any get or set methods should be named per the usual convention ("get_" or "set_" followed by the name of...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
History: Offer an analysis of the articles included in the following articles: Science Fiction. Do you...
History: Offer an analysis of the articles included in the following articles: Science Fiction. Do you think the article offers a valid interpretation of the content offered in the current or previous chapters? Use an example to support your viewpoint. Minimum 250 words "Why Sci-Fi Keeps Imagining the Subjugation of White People" As much as the genre imagines the future, it also remixes the past—often by envisioning Western-style imperialism visited on the Western world. Science-fiction "contemplates possible futures." So says...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...