Chapter – 5. CONSTRUCTORS & DESTRUCTORS

DELHI 2006

2.b. Answer the following questions (i) and (ii) after going through the following class. 2

class Interview
{
int Month;
public: interview(int y)
{
Month=y;
} //constructor 1 interview
(Interview&t); //constructor 2
};

(i) create an object, such that it invokes Constructor 1.

Ans: Interview A(10); //invoking constructor 1 by passing a number.

(ii) write complete definition for Constructer 2.

Ans: Interview(Interview &t) //This is a copy constructor.
{
Month=t.Month;
}

OUTSIDE DELHI 2006

1.f. What is a default constructor? How does it differ from destructor? 2

Ans: Default constructor: A constructor that accepts no parameter is called the default constructor. With a default constructor, objects are created just the same way as variables of other data types are created.

class X
{
int i ;
public: int j, k ;
------ //Members Functions
------
};

Eg: X ob1; Student s1; If a class has no explicit constructor defined, the compiler will supply a default constructor. This implicitly declared default constructor is an inline public members of its class. Declaring a constructor with arguments hides the default constructor.
There can be a default constructor as well as another constructor with arguments for a class, having multiple constructors is called as constructor overloading.

2.b. Answer the following questions (i) and (ii) after going through the following class. 2

class Exam
{
int Year;
public: Exam(int y) //Constructor 1
{
Year=y;
} Exam(Exam &t); //Constructor 2
};

(i) Create an object, such that it invokes Constructor 1

Ans: Exam E((2008);

(ii) Write complete definition for constructor 2.

Ans: Exam(Exam &t) //Copy Constructor.
{
Year=t.Year;
}