Sunteți pe pagina 1din 10

OOP Chp 2.

2 Ali Karim Sir (Mumbra)


Chapter 2.2
Constructor
Syllabus:
2.4. Concepts of Constructors, Types of constructors.
2.5. Multiple Constructors in a Class, Constructors with default arguments.
2.6. Destructors.

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.

Q Explain any two types of constructor with syntax and example.


(Any two types each type description-1Mark, Syntax-1/2 Mark, Example-1/2Mark)
Ans:
Types of constructor:-
 Default constructor: - A constructor that does not accept parameters is called as default
constructor.

Syntax:-
constrctor_name();

Example:- account() // default constructor.


class account {
{ accno=1;
int accno,bal; bal=1000;
public: }
};

ALI KARIM SIR 9220990566 1


OOP Chp 2.2 Ali Karim (Mumbra)
 Parameterized constructor: - A constructor that accepts parameters is called as parameterized
constructor.

Syntax:- constrctor_name(datatype parameter1, datatype parameter1,…, datatype parameter n);

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.

Syntax:- constrctor_name(datatype parameter1, datatype parameter1=value);

Example:-
class account
{
int accno,bal;
public:
account(int a, int b=1000) // parameterized constructor
{
accno=a;
bal=b;
}
};

ALI KARIM SIR 9220990566 2


OOP Chp 2.2 Ali Karim Sir (Mumbra)

Q. Define constructor overloading. (Definition-2Marks)


Ans: Defining more than one constructor function in a class is called as constructor overloading
(give example)

Q. Explain the concept of parameterized constructor with example. (parameterized constructor- 2


Marks; Example- 2 Marks; Syntax is optional; any one method shall be considered)
Ans:
Description
Parameterized Constructor: - Constructor which accepts one or more value(s) as argument(s)/
parameter(s) is known as parameterized constructor. These constructors gets invoked when an
object is created and this object shall be supplemented with appropriate numbers of arguments.

Syntax for parameterized constructor is as follows.


Syntax: -
class class_name
{
data member(s);
public:
class_name(arg1, arg2,… ) // parameterized constructor
{
….
….
}
};

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();

cout<<"\n\nEnter Value of Roll Number:";


cin>>r_no;
cout<<"Enter Value of Percentage:";
cin>>per;
cout<<"\n\n Output of Object s2:";
student s2(r_no,per); //Passing user values to Constructor
s2.display();
getch();
}
OUTPUT:

Q. Explain copy constructor with example. (Explanation- 2 Marks, Example- 2 Marks)


Ans
The copy constructor is a constructor which creates an object by initializing it with an object of the
same class, which has been created previously. The copy constructor is used to:

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();
}

ALI KARIM SIR 9220990566 4


OOP Chp 2.2 Ali Karim Sir (Mumbra)

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);
};

Q. Explain concept of overloaded constructor in a class with example. (Description of Overloaded


constructor - 1 Mark; Example - 3 Marks)
Ans:
Overloaded Constructor:
When more than one constructor are present in a class then it is a example of constructor overloading. By
using this concept in program user can create different object with various methods & initialization.

#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();
}

ALI KARIM SIR 9220990566 5


OOP Chp 2.2 Ali Karim
Q. Write a program to demonstrate constructor in derived class with respect to order of calling
constructor and passing parameters to base class constructor. (Declaring base class with proper
functions and data members - 3 Marks, Declaring derived class with proper functions and data members -
3 Marks, Main functions - 2 Marks)
Ans:
#include <iostream.h>
class alpha
{
private:
int x; class gamma : public beta, public alpha
public: {
alpha(int i) private:
{ int n, m;
x = i; public:
cout << "\n alpha initialized \n"; gamma(int a, float b, int c, int d):: alpha(a), beta(b)
} {
void show_x() m = c;
{ n = d;
cout << "\n x = "<<x; cout << "\n gamma initialized \n";
} }
}; void show_mn()
{
class beta cout << "\n m = "<<m;
{ cout << "\n n = "<<n;
private: }
float y; };
public:
beta(float j) void main()
{ {
y = j; gamma g(5, 7.65, 30, 100);
cout << "\n beta initialized \n"; cout << "\n";
} g.show_x();
void show_y() g.show_y();
{ g.show_mn();
cout << "\n y = "<<y; }
}
};

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);

ALI KARIM SIR 9220990566 6


OOP Chp 2.2 Ali Karim
Example:-
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int roll_no;
char name[10], course[30];
public:
student(char n[],int r, char c[]="Information Technology")
{
strcpy(name, n);
roll_no=r;
strcpy(course, c);
}
void display()
{
cout<<"\n\n Name of student : "<<name;
cout<<"\n Roll number of student : "<<roll_no;
cout<<"\n Course of student : "<<course;
}
};
void main()
{
student s1("ABC",1);
student s2("XYZ",2,"Computer Engineering");
clrscr();
s1.display();
s2.display();
getch(); }

Q. Explain overloaded constructor in class with example. (Relevant explanation of overloaded


constructor 2M, Any other relevant example/program 2M)
Ans:
 Overloaded constructor is one where we can have more than one constructor in same class.
Appropriate constructor will be called as per object creation.
 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.

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.

Multiple constructor can be declared in different ways:

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

ALI KARIM SIR 9220990566 8


OOP Chp 2.2 Ali Karim
integer(int a, int b)
{
m = a;
n = b;
cout<<"value of m="<<a;
cout<<"value of n="<<b;
} // constructor 2
};
void main()
{
clrscr();
integer i1;
integer i2(20,40);
getch();
}

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:

ALI KARIM SIR 9220990566 10

S-ar putea să vă placă și