Documente Academic
Documente Profesional
Documente Cultură
Q. Define constructor. State any two type of constructor. (Def 1Mk, any two type’s each-1/2 Mark)
Ans:
Definition- A constructor is a special member function whose task is to initialize the objects of its class.
Types of constructor:-
1. Default constructor 5. (Overloaded) Multiple constructor
2. Parameterized constructor 6. Constructor with default value
3. Copy constructor.
4. Dynamic constructor
Q. State any four characteristics of constructor. (Any four characteristics - 1 Mark for each
characteristics)
Ans:
1. Constructors should be declared in the public section.
2. They are invoked automatically when the objects are created.
3. They do not have return type, not even void and therefore they cannot return values.
4. They can accept arguments.
5. They cannot be inherited, though a derived class can call the base class constructor.
6. They cannot be virtual.
7. One cannot refer to their addresses.
8. An object with a constructor cannot be used as a member of a union.
9. They make implicit calls to the operators new and delete when memory allocation is required.
Syntax:-
constrctor_name();
Example:-
class account
{
int accno,bal;
public:
account(int a, int b) // parameterized constructor
{
accno=a;
bal=b;
}
};
Copy constructor:- A constructor that is used to declare and initialize an object from another
object is called as copy constructor.
Syntax:- constructor_name(class_name &object_name);
Example:-
class account account(account &a) //copy constructor
{ {
int accno, bal; accno=a.accno;
public: bal=a.bal;
account( ) // default constructor }
{ };
accno =10001; void main( )
bal = 5000; {
} account b; // default constructor gets invoked;
account c =b; //copy constructor for c gets invoked
}
Constructor with default value:- A constructor that accepts parameters and in which some
parameters can be declared with default value is called as constructor with default value.
Example:-
class account
{
int accno,bal;
public:
account(int a, int b=1000) // parameterized constructor
{
accno=a;
bal=b;
}
};
Example:-
#include<iostream.h>
#include<conio.h>
class student
{
int roll_number;
float per;
public:
student(int r, float p ) // parameterized constructor
{
roll_number = r;
per = p;
}
void display( )
{
cout<<"\n Roll Number is: "<<roll_number;
cout<<"\n Percentage is: "<<per;
}
};
void main( )
{
student s1(1, 75.00); //Calling Parameterized Constructor
int r_no;
float per;
clrscr( );
cout<<"\n Output of Object s1:";
ALI KARIM SIR 9220990566 3
OOP Chp 2.2 Ali Karim Sir (Mumbra)
s1.display();
If a copy constructor is not defined in a class, the compiler itself defines one. If the class has pointer
variables and has some dynamic memory allocations, then it is a must to have a copy constructor.
The most common form of copy constructor is shown here:
#include<iostream.h> void main()
class Point {
{ clrscr();
private: Point p1(10, 15); // param constructor is called here
int x, y; Point p2 = p1; // Copy constructor is called here
public: // Let us access values assigned by
Point(int x1, int y1) { x = x1; y = y1; } //constructors
// Copy constructor cout<<"\n Output of Parameterize Object p1:";
Point(Point &p2) {x = p2.x; y = p2.y; } cout << "p1.x = " << p1.getX() << ", p1.y = " <<
int getX() { return x; } p1.getY();
int getY() { return y; } cout<<"\n\n Output of Copy Constructor Object
}; p2:";
cout << "p2.x = " << p2.getX() << ", p2.y = " <<
p2.getY();
getch();
}
Q. Write example code of constructor with default argument. (Example code of constructor only, not
whole program) (Any example - 2 Marks)
Ans:
Class complex
{
……….……..
………………..
complex (float real, float imag=0);
};
#include<iostream.h>
#include<conio.h>
class integer
{
int m, n;
public: void display()
integer() //default constructor {
{ cout<<"m="<<m<<"\t";
m=0; cout<<"n="<<n<<"\n";
n=0; }
} };
void main()
integer(int a, int b) //Param Cons {
{ integer I1; //default constructor
m=a; integer I2(10,20); //Param Cons
n=b; integer I3(I2); //Copy cons
} cout<<"object I1";
I1.display();
integer(integer &i) //Copy cons cout<<"\nobject I2";
{ I2.display();
m=i.m; cout<<"\nobject I3";
n=i.n; I3.display();
} getch();
}
Q. State the use of default parameters in constructor with example. (Use of default parameters in
constructor 2M, Any other relevant example/program 2M)
Ans:
Default parameters in constructor
A constructor that accepts parameters in which some parameters can be declared with default value is called
as constructor with default value.
Syntax:-
constrctor_name(datatype parameter1, datatype parameter2=value);
Example:
#include<iostream.h>
#include<conio.h>
class demo
{
int num;
public:
demo() //Default Constructor
{
num=10;
cout<<"\n Values from Default Constructor"<<num;
}
ALI KARIM SIR 9220990566 7
OOP Chp 2.2 Ali Karim
demo(int x) //Parameterized Constructor
{
num = x;
cout<<"\n Values from Parameterized Constructor"<<num;
}
void display()
{
cout<<"\n Values of are"<<num;
}
};
void main()
{
clrscr();
demo d1,d2(30);
demo d3=d2; //Copy constructor
d3.display();
getch();
}
Q. Explain multiple constructor in class with example. (Explanation -2M, Any relevant example/program
– 2M)
Ans:
Multiple constructor is a type of constructor in which a class can contain more than one constructor. This is
known as constructor overloading. All constructors are defined with the same name as the class they belong
to. All the constructors contain different number of arguments. Depending upon the number of arguments, the
compiler executes appropriate constructor.
integer(); // No arguments
integer(int, int); // Two arguments
When the object is created the first constructor invoked.
In the first case, the constructor itself supplies the data values and no values are passed by the calling
program.
In the second case, the function call passes the appropriate values from main ( ).
C++ permits us to use both these constructors in the same class.
For example, we could define a class as follows:
#include<iostream.h>
#include<conio.h>
class integer
{
int m, n;
public:
integer()
{
m = 0;
n = 0;
} // constructor 1
This declared three constructors for an integer object. The first constructor receives no arguments, the
second receives two integer arguments and the third receives one integer object as an argument. For
example, the declaration.
integer i1;
would automatically invoke the first constructor and set both m and n of i1 to zero. The statement
integer i2 (20, 40);
would call the second constructor which will initialize the data members m and n i2 to 20 and 40 respectively.
Finally, the statement.
The process of sharing the same name by two or more functions is referred to as function overloading.
Q. Explain the concept of destructor in a class with example. (Expl destructor-2Marks, Ex 2Mk)
Ans:
Description:-
A destructor is used to destroy the objects that are created by a constructor.
It is member function whose name is same as the class name but proceeded by a tilde (~).
A destructor never takes parameters and it does not return any value.
It will be invoked by the compiler upon exit from the program (or block or function) to clean up storage
that is no longer accessible.
-
~destructor_name()
{
…
…
} Example:-
class student
{
public:
student()
{
cout<<”object is initialized”;
}
~student()
{
cout<<”object destroyed”;
}
};
ALI KARIM SIR 9220990566 9
OOP Chp 2.2 Ali Karim
Q. Differentiate between constructor and destructor. (Any - 4 , Each - 1 Mark)
Ans: