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...
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,
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; };
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...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be...
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be used         - how to instantiate and initialize data members that are user-defined types at the same time
What are default risk premiums, and what do they measure?
What are default risk premiums, and what do they measure?
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age with 1, id with -1, and name with "NoName". Notice that MyString's default constructor does not get called. Note: If you instead create a traditional default constructor as below, MyString's default constructor will be called, which prints output and thus causes this activity's test to fail. Try it! // A wrong solution to this activity... PoundDog::PoundDog() { age = 1; id = -1; name.SetString("NoName");...
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the first and last names to "None", the seller ID to "ZZZ999", and the sales total to 0.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT