Question

In: Computer Science

In C++ a class can have multiple constructors, but only one destructor. Explain why this is...

In C++ a class can have multiple constructors, but only one destructor. Explain why this is the case using code to highlight why.

Solutions

Expert Solution

Do give thumbs up if you find this answer useful !!!!!!!!!!!!!!!!!!!!!!!!!

Happy learning !!!!!!!!!!!!!!!!!!

CONSTRUCTOR:

  • A constructor is a member function whose name is the same as the class name.
  • It is used to initialize the objects of the class.
  • It is used to allocate memory to an object of the class.
  • It may or may not accept the arguments.
  • It is called whenever an instance of a class is created.
  • There can be many constructors in the class.

SYNTAX:

classname()

{

//constructor's body

}

DESTRUCTOR:

  • Like the constructor, the destructor is also a member function of a class that has the same name as the class name but preceded by a tilde (~) operator.
  • It helps to deallocate the memory of an object.
  • A class will always have a single destructor without any parameters so it can't be overloaded.

SYNTAX:

~classname()

{

//destructor's body

}

REASON:

  • The destructor destroys the object as soon as the scope of the object ends.
  • The destructor is called automatically by the compiler when the object goes out of the scope.
  • Even if you have multiple constructors you only need one destructor to destroy the object.


NOTE:

Example: you have an apple and an orange ( multiple constructor ) but you need only one knife to cut it where you will never use two knives to cut two fruits.

PROGRAM:

class A

{

//constructor

A()

{

cout<<"constructor called";

}

//constructor1

A(int)

{

cout<< " constructor1 called";

}

//destructor

~A()

{

cout<< " destructor called";

}

};

int main ()

{

A obj1; // constructor called

int obj2;

A obj2; //constructor1 called

} // object gets out of scope so destructor will be called for both objects.

OUTPUT:

constructor called

constructor1 called

destructor called

destructor called


Related Solutions

Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor overloading) for initializing objects with different parameters. The employee class should have different functions for saving and updating bio-data, educational background, and current status like experience, Salary, position & travel history. The employee class should have a function which asks user what do he likes to see about employee (.i.e., Bio-data, current status....) and only show the respective data. Create two objects of interns...
Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
Code a simple Editor class with 2 constructors (one default, one with a string for a...
Code a simple Editor class with 2 constructors (one default, one with a string for a file name) The Editor class in private has an instance of a LinkedList (of strings) and the position of the user (use our Point class) create a short file - at least 5 lines - could be a program main: string fileName("input.txt"); Editor miniVi (fileName); miniVi.displayLines(); C++ explain in detail plz Thank you
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
MULTIPLE CHOICE ____1.    When only one class of stock is issued by a corporation, it should...
MULTIPLE CHOICE ____1.    When only one class of stock is issued by a corporation, it should be termed         A.  Authorized stock         B.  Treasury stock         C.  Common stock         D.  Preferred stock         E.  None of these ____2.    Which of the following statements is correct?         A.    A corporation's issued stock may exceed its outstanding stock.         B.    A corporation's outstanding stock may exceed its authorized stock.         C.    A...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
C++: 1.When and in what order are constructors and destructors called? 2. Create a class without...
C++: 1.When and in what order are constructors and destructors called? 2. Create a class without any constructors, and show that you can create objects with the default constructor. Now create a non-default constructor (one with an argument, aka parameterized constructor) for the class, and try compiling again. Explain what happened.
Create a class called College that has: At least 2 constructors (one should be the default...
Create a class called College that has: At least 2 constructors (one should be the default constructor) All members private The relevant setters and getters A print_me() function A college_ID member (unique) Write two unsorted lists that manage the data for colleges and their ranking: An unsorted list using an array An unsorted list using a linked list Each college will have a unique ID number that will be used for search. For the lab you don't need to read...
Programming in C (not C++) ## Requirements Only need to edit the challenge.c You have one...
Programming in C (not C++) ## Requirements Only need to edit the challenge.c You have one function to implement: void fork_exec(char** argv): This takes in an array of strings representing arguments. The first argument is the filename of an executable (which will be given as a relative filepath, such as "./a") The remaining terms would be arguments for said executable. The array is null terminated You need to fork your process. The child needs to call exec (rather, a variant...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT