C++ How to Program (10th Edition)
C++ How to Program (10th Edition)
10th Edition
ISBN: 9780134448237
Author: Paul J. Deitel, Harvey Deitel
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 3, Problem 3.5E

(Default Constructor) What's a default constructor? How are an object's data members initialized if a class has only a default constructor defined by the compiler?

Expert Solution & Answer
Check Mark
To determine

Program Plan:

Default Constructor:

Constructor is the first function which is automatically called by the compiler when object is created. Its main purpose is make initializations for the class. Default constructors are the one which take no arguments or parameters, and even if they have parameters, they have default value. By default value we mean initial blank value which variable can take.

Syntax

Classname();
Classname(datatype=default value);

Example

Let us see following example:

#include<iostream>
using namespace std;
class A
{
     int x;
     float y;
public:
     A(){};				// default constructor
     void disp()
    {
            cout<<"\nx = "<<x;
            cout<<"\ny = "<<y;
     }
};

int main()
{
A a;				// making object of class A
a.disp();			// call to disp() function 
}

Explanation of Solution

C++ How to Program (10th Edition), Chapter 3, Problem 3.5E , additional homework tip  1

A(){} is the default constructor which initialize the variable x and y with default value 0 as evident from the output.

If user does not define any default constructor then compiler itself perform the same working, by automatically initializing the variable of the class on object creation.
Example:

#include<iostream>
using namespace std;
class A
{
    int x;
    float y;
public:
    void disp()
    {
        cout<<"\nx = "<<x;
        cout<<"\ny = "<<y;
    }
};

int main()
{
A a;
a.disp();
}

C++ How to Program (10th Edition), Chapter 3, Problem 3.5E , additional homework tip  2

Above piece of code does not have default constructor, but when object is created for class A, its data members are automatically initialized to value 0. Thus its quite evident that even if we do not create any default constructor, complier itself will initialize every data member.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Constructor (default vs user defined)
(The Time class) Design a class named Time. The class contains: - The data fields hour, minute, and second that represent a time. - A no-arg constructor that creates a Time object for the current time. (The values of the data fields will represent the current time.) -A constructor that constructs a Time object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. (The values of the data fields will represent this time.) -A constructor that constructs a Time object with the specified hour, minute, and second. - Three getter methods for the data fields hour, minute, and second, respectively. -A method named setTime (long elapseTime) that sets a new time for the object using the elapsed time. For example, if the elapsed time is 555550000 milliseconds, the hour is 10, the minute is 19, and the second is 10. Draw the UML diagram for the class and then implement the class. Write a test program that creates two Time objects (using new Time (), new Time(555550000),…
(java) The Painting Subclass Write class as follows: The class is named Painting, and it inherits from the Art class. It has a private String member variable named medium It has a default constructor that assigns the values "No name" to name, "No artist" to artist, -1 to year, and "No medium" to the medium variable. This default constructor calls the four-argument constructor. It has a four-argument constructor to assign values to the name, artist, year, and medium variables. It has a getter and settersfor the medium variable. It has a toString() method This class contains no other methods Make sure to include your name, the name of this class, our course number, and the Activity number in a Javadoc comment at the top. Make sure to write a Javadoc comment for each of these methods.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
What is Abstract Data Types(ADT) in Data Structures ? | with Example; Author: Simple Snippets;https://www.youtube.com/watch?v=n0e27Cpc88E;License: Standard YouTube License, CC-BY