Concept explainers
(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?
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
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(); }
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?
Chapter 3 Solutions
C++ How To Program Plus Mylab Programming With Pearson Etext -- Access Card Package (10th Edition)
- Define return Statement.arrow_forwardStatic analyzer write code python read text and check Do the attributes (e.g., data type and size) of each parameter match theattributes of each corresponding argument?arrow_forwardInstructions-Java Assignment is to define a class named Address. The Address class will have three private instance variables: an int named street_number a String named street_name and a String named state. Write three constructors for the Address class: an empty constructor (no input parameters) that initializes the three instance variables with default values of your choice, a constructor that takes the street values as input but defaults the state to "Arizona", and a constructor that takes all three pieces of information as input Next create a driver class named Main.java. Put public static void main here and test out your class by creating three instances of Address, one using each of the constructors. You can choose the particular address values that are used. I recommend you make them up and do not use actual addresses. Run your code to make sure it works. Next add the following public methods to the Address class and test them from main as you go: Write getters and…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education