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 (10th Edition)
- (Java) The Sculpture Subclass Write class as follows: The class is named Sculpture, and it inherits from the Painting class. It has a private boolean member variable named humanForm It has a default constructor that assigns the values "No name" to name, "No artist" to artist, -1 to the year, "No medium" to the medium, and false to the humanForm variable. This default constructor calls the five argument constructor. It has a five-argument constructor to assign values to the name, artist, year, medium, and humanForm variables. It has a getter and setter for the humanForm 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.arrow_forward(Person Class) Design a class named Person that contains: o name, gender, and personCase as a private attribute o Non-default constructor that specifies name and gender o toString method that returns person data o personCase () method with no implementation (Student Class) Design a class named Student which is a child of Person that contains: o studentID as a private attribute o Nondefault constructor that specifies the name, gender, and studentID o toString method that returns student data o Implement personCase () that assigns "Not Studying" to personCase if studentID equals 0, “Studying" in case studentID greater than 0, and "Not a student" in case studentID less than 0. (Employee Class) Design a class named Employee which is a child of Person that contains: o employeelD as a private attribute o Non-default constructor that specifies the name, gender, and employeelD o toString method that returns employee data o Implement personCase () that assign “Technical" to personCase if…arrow_forwardNeed help with C++ Workarrow_forward
- List various types of constructorsarrow_forward(Kotlin/Android Studio) When an Intent is created with a Context and Class object, it is considered an ______ intent. Multiple Choice generic activity implicit expliciyarrow_forwardJAVA PROGRAMMING (TRUE OR FALSE) No further explaination needed. Classes in the java·lang package may be used directly if they are imported first using an import statement(True/False) The fields and methods of a child/sub class are only those inherited from the parent/super class.(True/False) A reference/object of super class type can hold/assume any object of its sub class type.(True/False) An import statement is necessary to use/access a classin the same package.(True/False) A private data field is accessible only by other classes via the exposed methods of its class.(True/False)arrow_forward
- Differentiate between static and instance class members. Explain with proper example when you should make members static?arrow_forwardJust what are "static members" of a class? When and how should you use them, exactly?arrow_forwardNutritional information (classes/constructors) PYTHON ONLY Complete the FoodItem class by adding a constructor to initialize a food item. The constructor should initialize the name (a string) to "None" and all other instance attributes to 0.0 by default. If the constructor is called with a food name, grams of fat, grams of carbohydrates, and grams of protein, the constructor should assign each instance attribute with the appropriate parameter value. The given program accepts as input a food item name, fat, carbs, and protein and the number of servings. The program creates a food item using the constructor parameters' default values and a food item using the input values. The program outputs the nutritional information and calories per serving for both food items. Ex: If the input is: M&M's10.034.02.01.0 where M&M's is the food name, 10.0 is the grams of fat, 34.0 is the grams of carbohydrates, 2.0 is the grams of protein, and 1.0 is the number of servings, the output is:…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