Question

In: Computer Science

The most common way to declare a destructor explicitly is to use an identifier that consists...

The most common way to declare a destructor explicitly is to use an identifier that consists of a tilde followed by the class name.

  1. Please explain the purpose of a constructor method. Note that constructors typically provide initial values for the fields of an object.
  2. Discuss the use of the default constructor that takes no arguments and contrast it with a non-default constructor, otherwise known as a parameterized constructor that requires arguments.
  3. Describe the purpose of a destructor method. Explain when destructors are called, and note that a default destructor is provided automatically.

Solutions

Expert Solution

Constructor

  • Constructor is the special method whose name is  same as the class name which called automatically when objects are created.
  • They are used to initialize an instance variables of object.When objects are created, its instance variable must gets some valid value.This is done by the constructor.

Default constructors

  • These are the constructors taking no parameters.
  • Programmer can define default constructor without providig parameters and using these, instance varibles can be initialized with some default values.
  • When the programmer writes no constructor,the compiler itself provide a default constructor
  • The purpose of default constructor is to assign same default values to all objects which are created by without providing any values(parameters)
  • On contrast, parameterized constructor takes parameters and initialize instance variables using these parameters.
  • The purpose of parameterized constructor is to provide user wanted specific values to the variables of different objects

Destructor

  • Destructors are the method whose name is same as the class name but preceeded by tilde(~) symbol
  • This method called automatically when objects are destructed
  • It is used to de allocating or deleting the resources such as heap memory,network connections,files after usage

Example

Class sample
{
   int *ptr;
   ifstram f;
   Sample()
   {
       ptr=new int[50];
       f.open("student.txt")
   }
   ~Sample()
   {
       delete [] ptr;
       f.close();
   }
}

  • In the above example,inside the constructor resources are acquired and inside the destructor heap memory is deallocated and file is closed.



Related Solutions

In this unit, we introduce five paths to business ownership. The second most common way is...
In this unit, we introduce five paths to business ownership. The second most common way is to purchase an existing business. In the US, it is really an environment of caveat emptor when it comes to purchasing a business. My business for the project is a Tea Lounge. Find a business that is for sale in your industry and preferably, is similar to your business idea at BizBuySell. I found: Grilled Cheese Restaurant For Sale in Downtown Cranford Cranford, NJ...
​Of the following important figures in the history of social psychology, who most explicitly emphasized the...
​Of the following important figures in the history of social psychology, who most explicitly emphasized the importance of making important practical contributions to society?
Parenchyma is one of the most common tissue types. Describe common human use(s) for parenchyma. Which...
Parenchyma is one of the most common tissue types. Describe common human use(s) for parenchyma. Which properties of parenchyma make it suitable for human use?
The most common attributes of a book are the Book Title, and ISBN. The most common...
The most common attributes of a book are the Book Title, and ISBN. The most common functions are to set the Book Title, and ISBN, Write the code to implement this problem. 1. Write the UML Diagram that represents this class Book 2. Use code blocks editor and in C++ Write a header file Book with these properties. Write the implementation file for the member functions.
Leases are one of the most common means by which companies obtain the use of long...
Leases are one of the most common means by which companies obtain the use of long term operating assets. Lets look at the airline industry and the effect of leasing. What you believe are the benefits if any as airlines decide to lease versus buying assets? Why or why not?
4. Explain the most common tool that the Fed use to expand the amount of money...
4. Explain the most common tool that the Fed use to expand the amount of money in the banking system, and draw balance sheets (of CB and commercial banks) to show the process.
Leases are one of the most common means by which companies obtain the use of long...
Leases are one of the most common means by which companies obtain the use of long term operating assets. Lets look at the airline industry and the effect of leasing. What you believe are the benefits if any as airlines decide to lease versus buying assets? Why or why not?
Please justify and prove each statement (Use explicitly the four axioms) a) Prove that a finite...
Please justify and prove each statement (Use explicitly the four axioms) a) Prove that a finite positive linear combination of metrics is a metric (Use explicitly the four axioms). If it is infinite, will it be metric? b) Is the difference between two metrics a metric? (d1 - d2)
Create a scenario in which a business’s most attractive option may be to declare bankruptcy identifying...
Create a scenario in which a business’s most attractive option may be to declare bankruptcy identifying the benefits and consequences of the filing. Provide a rationale with your response. Create a scenario in which you would sell a portion of your financially troubled credit repair business to a turnaround management company in hopes of getting the business back on track. Provide support for how this may be a desirable option.
1. What are the two most common measures of inflation? How are they measured? 2. Use...
1. What are the two most common measures of inflation? How are they measured? 2. Use the loanable funds model to analyze: i).The effects of a government budget deficit on the equilibrium interest ate and quantity of loanable fund 2. An investment tax credit increase on the equilibrium interest ate and quantity of loanable fund
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT