VIII. CONSTRUCTORS AND DESTRUCTORS
SHORT ANSWERS
1.What is a constructor?
The constructor function initializes the class object.
The memory space is allocated to an object.
A constructor is automatically invoked when an object is created.
SYNTAX
Class simple
{
……..
}
public:
simple()
};
2.List out the Rules for constructor definition and usage:
The name of the constructor must be same as that of the class
A constructor can have parameter list
The constructor function can be overloaded
The compiler generates a constructor, in the absence of a user defined constructor
The constructor is executed automatically
They are declared under public:
3.What are the functions of a constructor?
Functions of constructor:
4.What are the types of constructor?
1.Default constructor (or) Non- Parameterized constructor
2.Parameterized constructor
3.Copy constructor
4..Match the following for the class my class.
1.myclass ( ) a.copy constructor
2.myclass (int x, int y) b.method
3.void show ( ) c.default constructor
4.myclass (myclass & a) d.parameterized constructor
Ans:1. (c)2. (d) 3. (b) 4. (a)
5.What is Non-Parameterized (or) default constructor
It is a constructor without parameters
It is called as default constructor
It is referred to compiler generated constructors
It is executed when an object without parameters is declared
ex. add()
6.What is Parameterized constructor?
It is a constructor with parameters
To invoke this constructor , the object should be declared with integer constants or variables
Ex. add(int a,int b)
7.When does a copy constructor get executed?
A copy constructor is executed
1.When an object is passed as a parameter to any of the member function.
Example add c(b);
2.When a member function returns an object.
Example a.add(b);
3.When an object is passed by reference to constructor.
Example, add a , b(a);
8.Explain constructor overloading.
Function overloading can be applied for constructors, as constructors are special functions of classes
Three types of constructors,they are
1.Non-Parameterized constructor
2.Parameterized constructor
3.copy constructor
9..What is a destructor?
A destructor is a function that removes the memory of an object
It is executed when the scope of the object is exit.
It carries the same name as the class tag, but with a tilde (~) as prefix.
SYNTAX
Class simple
{
……..
}
public:
~simple()
};
10.Write the rules of destructor?
The destructor has the same name as that of the class prefixed by the tilde character ‘~’.
The destructor cannot have arguments
It has no return type
Destructors cannot be overloaded
There can be only one destructor in a class
In the absence of user defined destructor, it is generated by the compiler
It is executed when the scope of the object is exit.
11.What is constructor overloading?
Function overloading can be applied for constructors, as constructors are special functions of classes.
Constructor overloading includes parameterized (default) constructor, Non-Parameterized constructors and copy constructor
12. What are the rules for constructor definition and usage
13. What are the rules for destructor definition and usage.
14.What are the similarities of Constructors and Destructor?
The Constructors and Destructor have the same name as that of the class
They are not associated with any data type.
Both the functions return nothing
Both are generated by the complier in the absence of User Defined Constructors/ Destructor respectively.
Both are executed automatically.
Both are declared under public:
15.Differentiate between Constructor and Destructor.
Constructors | Destructor |
The name of the constructor must be same as that of the class
| The destructor has the same name as that of the class prefixed by the tilde character ‘~’.
|
The constructor is executed automatically
| The Destructor is executed automatically
|
can have parameter list | cannot have parameter |
no return type | no return type |
can be overloaded | cannot be overloaded, only one destructor in a class |
It is executed when the scope of the object is created.
| It is executed when the scope of the object is exit.
|
Additional questions
1)Find the out out of the following program
#include <iostream.h>
#include <conio.h>
class add
{
int num1, num2, sum;
public:
add ( )
{
cout<< "\n constructor without parameters..";
num1=0;
num2=0;
sum=0;
}
add (int s1, int s2)
{
cout<< "\n parameterized constructor..";
num1 = s1;
num2 = s2;
sum = NULL;
}
add (add & a)
{
cout<< "\n Copy constructor…";
num1 = a.num1;
num2 = a.num2;
sum = NULL;
}
void getdata ( )
{
cout<< "Enter a data";
cin>> num1>>num2;
}
void addition ( )
{
sum=num1+num2;
}
void putdata ( )
{
cout<< "\n The numbers are..";
cout<<num1<< "\t" <<num2;
cout<< "\n The sum of the numbers are .."<<sum;
}
};
void main ( )
{
clrscr();
{
clrscr ( );
add a, b (10, 20), c(b);
a. getdata ( );
a. addition ();
b. addition ( );
c. addition ( );
cout<< "\n object a:";
a. putdata ( );
cout<< "\n object b:";
b. putdata ( );
cout<< "\n object c:<<";
c. putdata ( );
}
getch();
}
OUTPUT:
Constructor without parameters….
Parameterized Constructor...
Copy Constructors…
Enter data .. 5 6
Object a:
The numbers are 5 6
The sum of the numbers are ….. 11
Object b:
The numbers are 10 20
The sum of the numbers are … 30
Object c:
The numbers are 10 20
The sum of the numbers are ….. 30
2)Find the out out of the following program
#include<iostream.h>
#include<conio.h>
class simple
{
private:
int a,b;
public:
simple()
{
a= 0 ;
b= 0;
cout<< “\n Constructor of class-simple “;
}
~simple()
{
cout<<“\n Destructor of class – simple .. “;
}
void getdata()
{
cout<<“\n Enter values for a and b... “;
cin>>a>>b;
}
void putdata()
{
cout<<“\nThe two integers .. “<<a<<‘\t’<< b;
cout<<“\n The sum of the variables .. “<< a+b;
}
};
void main()
{
simple s;
s.getdata();
s.putdata();
getch();}
output:
Constructor of class - simple ..
Enter values for a & b… 5 6
The two integers….. 5 6
The sum of the variables….. 11
Destructor of class - simple …
III.Find the out out of the following program
#include <iostream.h>
#include <conio.h>
class distance
{
int feet;
float inch;
public:
distance ( )
{
feet = 0;
inch = 0;
}
distance (int x, float y)
{
feet = x;
inch = y;
}
distance (float a)
{
feet = int (a);
inch = (a-feet)* 12;
}
void putdata ( )
{
cout<< “\n Distance is “<<feet<<” feet and “<<inch<<” inches”;
};
void main ( )
{
clrscr ( );
distance d1;
distance d2 (4, 5), d3 (5, 3.6);
d1.putdata ( );
d2.putdata ( );
d3.putdata ( );
getch ( );
}
output:
Distance is 0 feet and 0 inches
Distance is 4 feet and 5 inches
Distance is 5 feet and 3.6 inches
IV.Find the out out of the following program
#include <iostream.h>
#include <conio.h>
class cartesian
{
int x, y;
public:
cartesian (int a, int b)
{
x = a;
y = b;
}
cartesian ( )
{
x = 0;
y = 0;
}
void print ( )
{
cout<< “\n (“<<x<<”, “<<y<<”)”;
}
};
void main ( )
{
clrscr ( );
cartesian p1, p2 (7, -1), p3 (4, 5);
p1.print ( );
p2.print ( );
p3.print ( );
getch ( );
}
Output:
0, 0
7, -1
4, 5
V.Read the following program and answer the following question.
#include <iostream.h>
#include <conio.h>
class simple
{ float x;
public:
simple ( )
{
x = 1.0;
}
simple (float m)
{
x = m;
}
simple (float a, float b)
{
x = a + b;
}
simple (simple & k)
{
x = k.x;
cout<< “\n copy constructor invoked \n”;
}
void show ( )
{
cout<< “\n x =” <<x<< ‘\n’;
}
};
void main ( )
{ clrscr ( );
simple s, s1;
s1. show ( );
simple s2 (7), s3 (s1);
simple s4 = s2, s5 (4, 5);
s = s5;
s2.show ( );
s3.show ( );
s4.show ( );
s5.show ( );
s.show ( );
getch ( );
}
(a) Write prototype of non-parameterized constructor.
simple ( );
(b) Write prototype of parameterized constructor.
simple (float m);
simple (float a, float b)
(c) Write prototype of copy constructor.
simple (simple & k);
(d) Name the private members of the class.
float x
(e) Name the public members of the class.
void show ( );
(f) Identify statements that invoke copy constructor.
s4 = s2
s = s5;
VI.Read the following program and answer the following question.
#include <iostream.h>
#include <conio.h>
class example
{
int x, y;
public:
example (example & a)
{
x = a.x;
y = a.y;
cout << “copy constructor …”;
}
example ( )
{
x = 1;
y = 1;
}
example (int a, int b)
{
x = a;
y = b;
}
void display ( )
{
cout<< “\nx =” <<x<< “and y =” <<y;
}
};
void main ( )
{
clrscr ( );
example e1;
example e2 (2, 4);
example e3 = e2;
e1.display ( );
e2.display ( );
e3.display ( );
getch( );
}
(a) Write prototype of non parameterized constructor.
example ( );
(b) Write prototype of parameterized constructor.
example (int a, int b);
(c) Write prototype of copy constructor.
example (example & a);
(d) Name private members of the class.
int x, y.
(e) Name public members of the class.
void display ( );
(f) Indentify statements that invoke copy constructor.
example e3 = e2;
VI.Debug the following program to get given output.
#include <iostream.h>
#include <conio.h>
class class1
{
int x;
int y;
public:
class1 ( )
{
x = 0;
y = 0;
}
class1 (int m, int n)
{
m = x;
n = y;
}
void print ( )
{
cout<<endl<<x<< ‘\t’ <<y;
}
};
void main ( )
{
class1 c1, c2 (3, 4);
c1.print ( );
c2.print ( );
}
Output:
0 0
3 4
Solution:
(i) m = x;
error: the statement must be given as x = m;
(ii) n = y;
error: the statement must be given as y = n;
Book Exercises:
I)
1.Complete the following table
| Constructor | Destructor |
1.Should be declared under | Public | Public |
2.Overloading is | allowed | Not allowed |
3.Is executed when an object is | Created | Removed |
4.The function of a | Initialize and allocate memory space to an object | Remove allocated memory of an object |
II. Why do the following snippets throw error?
a)
Answer: Constructor must be declared under public scope.
class simple
{
private :
int x;
public:
simple ( )
{ x = 5; }
} ;
b)
Simple s ; Error
Answer: Cannot create object s as it does not match any constructor type.
class simple
{
private :
int x;
public:
simple (int y)
{ x = y; }
} ;
void main ( )
{
simple s(15);
}
c)
are similar.