Constructors
Constructors
Question 1
A member function having the same name as that of its class is called constructor function.
Question 2
Question 3
Question 4
Question 5
Question 6
Answer
Question 7
Assignment Questions
Question 1
Answer
A constructor is a member function with the same name as that of its class but no return type.
A constructor is used to initialize the objects of that class type with legal initial values.
Question 2
What is that class called which does not have a public constructor ?
Answer
A class which does not have a public constructor is called a private class as only the member
functions may declare objects of the class.
Question 3
Answer
Question 4
Answer
Question 5
Write a class specifier (along with its constructor) that creates a class student having two
private data members : rollno and grade and two public functions init( ) and display( ).
(Do not write full definitions of member functions except for constructor).
Answer
class student
{
private int rollno;
private char grade;
public student() {
rollno = 0;
grade = '\0';
}
Question 6
Can you think of the benefits of a private class if any ? What are they ?
Answer
The benefits of a private class (i.e., a class with a private constructor) are:
1. Controlled object creation — By making the constructor private, we can control the
number of objects of a class that can be created. We can also enforce certain rules or
constraints on how the object is created and used.
2. Better memory management — By limiting the number of objects of a class that can
be created, we can reduce memory usage and improve performance.
Question 7
class Sample
{
int i;
char c;
float f;
:
//public members
:
}
Implement the constructor.
Answer
Question 8
Define a constructor function for a Date class that initializes the Date objects with given
initial values. In case initial values are not provided, it should initialize the object with default
values.
Answer
public Date()
{
day = 1;
month = 1;
year = 1970;
}
public Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
Question 9
Answer
Every time an object is created, the constructor is automatically invoked. If the function
creating the object, does not have access privilege for the constructor, it cannot be invoked
for that function. Thus, the object cannot be created by such function.
Therefore, it is essential for a function to have access privilege for the constructor in order to
create objects of a class.
Question 10
Constructor functions obey the usual access rules. What does this statement mean ?
Answer
Constructor functions obey the usual access rules. It means that a private or protected
constructor is not available to the non-member functions.
With a private or protected constructor, one cannot create an object of the same class in a
non-member function. Only the member functions of that class can create an object of the
same class and invoke the constructor.
Question 11
Answer
Parameterised constructor Non-parameterised constructor
Question 12
Answer
The benefit of temporary instances is that they live in the memory as long as they are being
used or referenced in an expression and after this, they are deleted by the compiler. The
memory is freed as soon as they are deleted rather than staying in the memory till the
program completes execution.
The drawback of temporary instances is that they are anonymous i.e., they do not bear a
name. Thus, they cannot be reused in a program.
Question 13
Answer
Question 14
How can objects be initialized with desired values at the time of object creation ?
Answer
Objects can be initialized with desired values at the time of object creation by using
parameterized constructor and passing the desired values as arguments at the time of creation
of object.
Question 15
When a compiler can automatically generate a constructor if it is not defined then why is it
considered that writing constructors for a class is a good practice ?
Answer
Writing constructors for a class is considered a good practice for the following reasons:
1. Control over object creation — By writing our own constructors, we can control
how objects of our class are created and initialised. It ensures initialising member
variables to specific values and executing certain initialisation logic before the object
is used.
2. Parameter validation — We can prevent runtime errors and bugs by validating input
parameters and ensuring only valid objects are created.
3. Flexibility — Multiple constructors allows users of the class to create objects in
different ways, depending on their needs. This makes the class more flexible and
easier to use.
Question 16
'Accessibility of a constructor greatly affects the scope and visibility of their class.' Elaborate
this statement.
Answer
Every time an object is created, it is automatically initialised by the constructor of the class.
Therefore, it is very much necessary for the constructor of a class to be accessible by the
function in which the object is created. If the constructor of a class is declared 'private' or
'protected', then the scope and visibility of the constructor (and the class) is defined by the
rules of the access specifier.
In such a case, a function not having access to constructor of a class cannot declare and use
objects of that class. Hence, accessibility of a constructor greatly affects the scope and
visibility of their class.
Question 17
Answer
Some of the special properties of the constructor functions are as follows:
Question 18
Answer
Parameterized constructors are the constructors that receive parameters and initialize objects
with received values. For example:
class XYZ
{
private int i;
private float j;
1. Control over object creation — By writing our own constructors, we can control
how objects of our class are created and initialised. It ensures initialising member
variables to specific values and executing certain initialisation logic before the object
is used.
2. Parameter validation — We can prevent runtime errors and bugs by validating input
parameters and ensuring only valid objects are created.
Question 19
Data members
1. Name of the depositor
2. Account number
3. Type of account
4. Balance amount in the account
Methods
Answer
class BankAccount
{
private String name;
private long accno;
private char type;
private double bal;
BankAccount() {
name = "";
accno = 0;
type = '\0';
bal = 0.0d;
}
BankAccount(String n, long accnumber, char t, double b)
{
name = n;
accno = accnumber;
type = t;
bal = b;
}