Question

In: Computer Science

What is a constructor ? What is a destructor? What is a default constructor? Is it...

What is a constructor ? What is a destructor?

What is a default constructor? Is it possible to have more than one default constructor?

Solutions

Expert Solution

Constructor:

Constructor is also a member method, which is meant for allocating memory for the instance variables of the class, thus creating an object of the class. In other words, Constructor is a method for creating a new object.

The Constructor should have the same name as that of the class and it can be non-parameterized or parameterized. The purpose of the constructor is to assign initial values to the instance variables of the class.

The method header of a Constructor is different from an instance method by two ways:

(i) Constructor should not have any return type mentioned, not even void

(ii) The Constructor name should be the class name.

Destructor:

Destructor is also a member method of a class that is used to free the allocated memory resources of an object. Once the lifetime of an object is over or in other words, when the scope of the object is terminated,  the destructor helps to free the object from memory.

Default Constructor

A default Constructor is a non-parameterized constructor which assigns default values to all the instance variables of the object, thus intializing the object once it is created.

It can also be parameterised in language like C++ where all parameters should assign default values to the instance variables.

Even if no constructors are defined for a class, the compiler implicitly uses a non-parameterized constructor which assigns default values to all the instance variables of the class. That is the default constructor.

Default Constructor can also be given in a class as mentioned above, either as a non-parameterized Constructor or parameterized with all parameters having default vaues

Yes, It is possible to have more than one default Constructor( in C++) where one can be a non-parameterized Constructor and the other can be parameterized with all parameters having default vaues,.

Language like Java, there is only one Default Constructor


Related Solutions

(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three...
(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c public void setA(double newA) public void setB(double newB) public void setC(double newC) (6) public static QuadraticExpression scale( double r, QuadraticExpression q) returns...
Shirt inherits clothing. In the Shirt constructor, how is the Clothing default constructor called? (Select all...
Shirt inherits clothing. In the Shirt constructor, how is the Clothing default constructor called? (Select all that apply) Group of answer choices super(); Clothing(); Clothing.Clothing() Make no explicit call Shirt inherits Clothing. In the Shirt constructor, how is a Clothing constructor with one int parameter called? For the answer, assume that an int variable called "myInt" is in scope. Group of answer choices Clothing.Clothing(myInt); Clothing(myInt); super(myInt); Make no explicit call Shirt inherits Clothing. Both classes have their own version of...
c++ creat a bst.h with constructor, copyconstructor, overloaded assignment operator, destructor, count leaves, count full nodes,...
c++ creat a bst.h with constructor, copyconstructor, overloaded assignment operator, destructor, count leaves, count full nodes, print ancestors using c++ create a binary search tree class with tree.h class tree.cpp class and a driver to test the method. tree.h need to define the copy constructor, overloaded assignment operator, destructor, find height, print ancesstor, print post order traversal, print iterative in order traversal, constructor, find key, count full nodes, count leaves,
in c++ a Circle should be created with the default constructor, then its area, diameter, and...
in c++ a Circle should be created with the default constructor, then its area, diameter, and circumference printed to the screen for verification that it has the correct state and those respective methods work correctly. Then, output of the state should be repeated after calling each mutator to ensure that the object’s new state reflects the change Create a Circle class with the class implementation in a file named circle.cpp and the class declaration in a file named circle.h (or...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template...
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template <typename ItemType> ArrayBag<ItemType>::ArrayBag() : item_count_(0) { // initializer list } // end default constructor template <typename ItemType> int ArrayBag<ItemType>::getCurrentSize() const { return item_count_; } template <typename ItemType> bool ArrayBag<ItemType>::isEmpty() const { return item_count_ == 0; } template <typename ItemType> bool ArrayBag<ItemType>::add(const ItemType &new_entry) {    bool has_room_to_add = (item_count_ < DEFAULT_CAPACITY); if (has_room_to_add) { items_[item_count_] = new_entry; item_count_++; } // end if return has_room_to_add;...
What problems could a programmer encounter if they defined a destructor for a class but no...
What problems could a programmer encounter if they defined a destructor for a class but no copy constructor? Illustrate your description with an example class. Programming language: C++ Requirement: provide answer along with one code example with illustration.
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.
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type)...
Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT