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...
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...
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...
Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the...
Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates an array that can hold a single element by default (that is, init capacity == 1); --1.2) (20' pts) Getters and setters and basic methods we discussed during the class including: getCapacity(), getSize(), set(int index, E val), get(int index), isEmpty(); --1.3) (20' pts)...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
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
C++ This assignment will have you designing a program which can test multiple combinations of the...
C++ This assignment will have you designing a program which can test multiple combinations of the Boolean variables A, B, and C, and determine which combinations of them will yield true values. However this time the statement is designed in the style of a Logical Circuit. You are asked to create three truth tables for the three problems. To achieve this, you should create SEVEN gate functions for your checkpoint. For each of the problem: First, convert the diagram to...
C++ This assignment will have you designing a program which can test multiple combinations of the...
C++ This assignment will have you designing a program which can test multiple combinations of the Boolean variables A, B, and C, and determine which combinations of them will yield true values. However the statement is designed in the style of a Logical Circuit.You are asked to create three truth tables for the three problems.First, convert the diagram to boolean algebra equations.Then evaluate their values step by step based on the values of A, B and C. Finally, print the...
Metals that can have multiple charges?
Metals that can have multiple charges?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT