0% found this document useful (0 votes)
169 views12 pages

Chapter 9-Constructors & Destructors (61-71)

The document contains questions and answers related to constructors in C++. It discusses various types of constructors like default constructor, parameterized constructor, copy constructor and destructor. It provides syntax and examples for each. Key points covered are: a destructor is called when an object is destroyed; a constructor cannot return a value; it is possible to overload default and parameterized constructors but not copy constructor or destructor.

Uploaded by

Khushi Y.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views12 pages

Chapter 9-Constructors & Destructors (61-71)

The document contains questions and answers related to constructors in C++. It discusses various types of constructors like default constructor, parameterized constructor, copy constructor and destructor. It provides syntax and examples for each. Key points covered are: a destructor is called when an object is destroyed; a constructor cannot return a value; it is possible to overload default and parameterized constructors but not copy constructor or destructor.

Uploaded by

Khushi Y.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

One Mark Questions

1. What is Destructor? Which operator is used with destructor?


A. A destructor is special member function that is executed when an object of that class is
destroyed. Tilde operator is used(~).
2. Can a constructor return a value to a calling function.
A. No
3. Is it possible to overload a default constructor?
A. No
Three Mark Questions
4. * Write syntax and example for copy constructor.
A. Copy constructor is a parameterized constructor using one object can be copied to
another object.
Syntax
class Class_Name
{
public:
class_Name(Class_Name &ptr )
{
…….
}
};
5. * Write syntax and example for default constructor.
A. Syntax
class class_Name
{
public:
class_Name( )
{
…….
}
Page 61
};
Example
class number
{
public:
number( )
{
n = 0;
}
};
*6. What is Constructor? Give Example.
A. A Constructor is a special member function that is called automatically when an object is
created.
The purpose of a constructor is to mainly initialize the member variables of a class.
Automatic initialization is carried out using special member functions called
constructors.
Example:
class sum
{
private:
int n, s;
public:
sum ( )
{
s = 0;
}
};
7. Mention the features of parameterized constructor.
A. The parameterized constructors can be overloaded.
For an object created with one argument, constructor with only one argument is invoked
and executed.

Page 62
Page 63
Class_Name( argu1, argu2….)
{
…….
}
};
Example
class max
{
public:
max(int a, int b )
{
if (a > b)
big = a;
else
big = b;
}
};
Some of the features of the parameterized constructors are:
The parameterized constructors can be overloaded.
For an object created with one argument, constructor with only one argument is invoked
and executed.
The parameterized constructor can have default arguments and default values.
*13. Write the rules for writing a constructor function.
A. Rules for writing a constructor is as follows
The name of the constructor is the same as the name of the class.
Example: In the above class, name of the class is Sum and the function name is Sum.
A Constructor, even though it is a function, has no return type, i.e. it is neither a value-
returning function nor a void function.
Example: Sum ( ) function has no return type and not even void.
The constructor should be declared in the public section.

Page 64
Constructors are executed automatically i.e. they are never invoked. They are executed
when a class object is created.
A class can have more than one constructor. However all constructor of a class should
have the same name.
It is not possible to refer to the address of the constructors.
The constructors make implicit calls to the operator new and delete when memory
allocation is required.
*14. Explain destructor with syntax and example.
A. A destructor is special member function that is executed when an object of that class is
destroyed.
Destroying an object means, de-allocating all the resources such as memory that was
allocated for the object by the constructor.
It will have like constructor, the name same as that of the class but preceded by a tilde
(~).
The destructor cannot take arguments therefore cannot be overloaded.
The destructor has no return type.
There can be only one destructor in each class.
In should have public access in the class declaration.
The destructor cannot be inherited.
The general format of destructor is as follows:
Syntax
class Class_Name
{
public:
Class_Name( );
~ Class_Name( );
};
Example
class Counter
{
public:

Page 65
Counter( ) //Constructor
{
n = 0;
}
~Counter ( ) //Destructor
{}
};
15. Explain constructor overloading with an example.
A. A class has two or more constructor functions with the same name but different
signatures are called Constructors Overloading.
Depending upon the type of argument, the constructors will be invoked automatically by
the compiler to initialize the objects.
Example: Program to find simple interest using constructor overloading.
#include<iostream.h>
#include<conio.h>
class simpleinterset
{
private:
float p, r, t, si;
public:
simpleinterset( ) //Default constructor
{
}
simpleinterset(float x, float y, float z) //Parameterized Constructor3
{
p = x;
r = y;
t = z;
}
void compute ( )
{

Page 66
si = (p * t * r)/100;
cout<<”Simple Interest is = “<<si;
}
};
void main( )
{
simpleinterest s1, s2(10000.0, 12.0, 2.0);
S2.compute( );
}
*16. Explain the different types of invoking of a constructor.
A. Invoking Constructors:
A Constructor is automatically invoked by C++ compiler with an object declaration.
The Constructor can be invoked through the following methods.
Implicit Call
Explicit Call
Initialization at the time of declaration with “ = “ operator.
Implicit Call:
An Implicit call means the declaration of the object is followed by argument list
enclosed in parenthesis.
Example: Program to initialize the data members using implicit declaration
#include<iostream.h>
#include<conio.h>
class num
{
private:
int a, b;
public:
num ( int m, int n) //Parameterized Constructor
{
a = m;
b = n;

Page 67
}
void display( )
{
cout<<” a = “ << a <<” b = “ << b;
}
};
void main( )
{
num obj1(10, 20); //Implicit Call
num obj2(40, 50); //Implicit Call
obj1.display( );
obj2.display( );
}
Explicit Call:
In explicit call, declaration of an object is followed by assignment operator, constructor
name and argument list enclosed in parenthesis.
Example: Program to initialize the data members using explicit declaration
#include<iostream.h>
#include<conio.h>
class num
{
private:
int a, b;
public:
num ( int m, int n) //Parameterized Constructor
{
a = m;
b = n;
}
void display( )
{

Page 68
cout<<” a = “ << a <<” b = “ << b;
}
};
void main( )
{
num obj1 = num(10, 20); //Explicit Call
num obj2 = num(40, 50); //Explicit Call
obj1.display( );
obj2.display( );
}
Initialization of object during declaration with assignment operator “ = “:
This method is used for the constructor with exactly one argument. In this method
declaration is followed by assignment operator and value to be initialized.
Example: Program to initialize objects using assignment operator.
#include<iostream.h>
#include<conio.h>
class num
{
private:
int a;
public:
num ( int m) //Parameterized Constructor
{
a = m;
}
void display( )
{
cout<< a <<endl ;
}
};
void main( )

Page 69
{
num obj1 = 100
num obj2 = 200
cout<<”Object1 = “ ;
obj1.display( );
cout<<”Object2 = “ ;
obj2.display( );
}
17. Explain Copy constructor with example.
A. Copy constructor is a parameterized constructor using one object can be copied to
another object.
Copy Constructors are used in the following situations:
To initialize an object with the values of already existing objects.
When objects must be returned as function values.
To state objects as by value parameters of a function.
Copy Constructor can accept a single argument of reference to same class type.
The argument must be passed as a constant reference type.
The general format of copy constructor is as follows:
Syntax
class Class_Name
{
public:
Class_Name(Class_Name&ptr )
{
…….
}
};
Example
class number
{
public:

Page 70
number(int n)
{ a = n; }
number(number & X)
{
a = x.a;
cout<<”Copy Constructor invoked”;
}
};
Note :
Copy constructor is not invoked explicitly.
Copy constructors are invoked automatically when a new object is created and equated
to an already existing object in the declaration statement itself.
Example:
x a1; //Default Constructor
x a2 = a1; //Copy Constructor
When a new object is declared and existing object is passed as a parameter to it in the
declaration, then also copy constructor is invoked.
Example:
x a1(100, 200); //Parameterized Constructor
x a2(a1); //Copy Constructor invoked
When object is passed to a function using pass by value, copy constructor is
automatically called.
Copy constructor is invoked when an object returns a value.

Page 71

You might also like