Beruflich Dokumente
Kultur Dokumente
CAMBRIDGE SCHOOL,NOIDA
Constructors
-Once
[Type text]
CAMBRIDGE SCHOOL,NOIDA
}
};
Like any other member function, the constructor can also be defined outside the class
.
class student
{
int rollno;
float perc;
public:
student(); //declaration of constructor function
void initdata( ) //Member function to initialize data members
{
rollno = 0;
perc = 0.0;
}
};
student::student() // constructor definition outside class
{
rollno = 0;
perc = 0.0;
}
Note : A Constructor can be defined in both public and private section of the class
and also follow the usual access rules of a class .
A Constructor, defined in the private section of the class is not available to the
non-member functions and objects of such class can only be created inside
member functions. In such situation no object (of class with private
constructor) can be created outside the class as private constructor cannot be
invoked by non member function and the compiler provided constructor
remains hidden .
A Constructor should be defined under the public section of the class so that
any function can access it to create object.
Default Constructor
[Type text]
CAMBRIDGE SCHOOL,NOIDA
[Type text]
CAMBRIDGE SCHOOL,NOIDA
Temporary instance
A temporary instance is an object of a class, that is accessible in
the memory as long as it is being used or referenced, and after
this it becomes inaccessible. Temporary instance does not have
any name. Explicit constructor call lets you to create a
temporary instance. For example: student(3,89.7).showdata( );
In the above example, student(3,89.7) is an anonymous
temporary object and accessible as long as the function
showdata( ) is executing it. After that it becomes inaccessible.
It is also possible to create an object, by passing one of the two
parameters into the constructor and initialize the other in the
constructor itself.
Example : class student
{
int rollno;
float perc;
Class XII-COMPUTER SCIENCE
4
[Type text]
CAMBRIDGE SCHOOL,NOIDA
public:
student(float p) // parameterized constructor
{
rollno = 0;
perc = p;
}
:
:
} // End of class
void main()
{
student s1(89.3); //s1 objects rollno = 0 and perc = 89.3
:
}
Multiple Constructors In A Class
C++ allows use of more than one constructor in the class. In that situation, depending
on the object creation statement, a particular constructor function is activated. In the
following example, there are three constructors in the class student. The first, which
takes no argument, is used to create objects, which are initialized to 0. The second,
which takes one argument, is used to create objects and initialize them and the third
constructor, which takes two arguments, is also used to create objects and initialize
them to specific values. When more than one constructor function is defined in a
class, it is called Constructor function Overloading. Constructor overloading is a
method to implement Polymorphism feature of OOP.
Example : Program having multiple Constructors
class student
{
int rollno;
float perc;
public:
student() // Constructor 1
{
rollno = 0;
perc = 0.0;
}
student(float p) // Constructor 2
{
rollno = 0;
perc = p;
}
student(int rno,float p) // Constructor 3
{
rollno = rno;
perc = p;
}
:
}; //end of class definition
Class XII-COMPUTER SCIENCE
5
[Type text]
CAMBRIDGE SCHOOL,NOIDA
void main()
{
student s1;
// call to the constructor 1. Now s1s rollno=0 ,perc=0.0
student s2(99.3); //call to the constructor 2. s2s rollno=0 ,perc=99.3
student s2(3,99.3);//call to the constructor 3. S3s rollno=3 ,perc=99.3
:
}
[Type text]
CAMBRIDGE SCHOOL,NOIDA
[Type text]
CAMBRIDGE SCHOOL,NOIDA
}
void main()
{
student st1(2,75.5);
report(st1);//function call
}
In the above example, as soon as the call to the function report (..) is made,
copy constructor will be invoked to copy the object st1 passed as parameter to
another newly created object temp.
-
[Type text]
CAMBRIDGE SCHOOL,NOIDA
[Type text]
CAMBRIDGE SCHOOL,NOIDA
When control will come out of main(), the objects s1 and s2 will be destroyed and
each time the destructor function is destroying any object, it will display destructing
object. If more than one object is being destroyed the destructors are invoked in
reverse order in which the constructor is called. In the above example s2 will be
destroyed first and then s1 will be destroyed.
Sample Questions
1.
Answer the questions (i) and (ii) after going through the following program :
#include<iostream.h>
#include<string.h>
class product
{
char type[20];
char pname[20];
int qty;
float price;
product()
//function1
{
strcpy(type, Electronic);
strcpy(pname,typewriter);
qty=5;
price=500;
}
public :
void disp()
//function2
{
cout<<type<<<<pname<<:<<qty<<@<<price<<endl;
}
};
void main()
{
product p;
//statement1
p.disp();
//statement2
}
(i)
Will statement1 initialize all the data members for object p with the values
given in the function1 ?(yes OR No). Justify your answer suggesting the
corrections to be made in the above code.
(ii)
What shall be the possible output when the program gets executed? (Assuming
, if required the suggested corrections are made in the program)
Solution
(i)
No. Since the default constructor product() is declared inside private section, it
cannot initialize the objects declared outside the class. Corrections needed are
that the constructor should be declared inside public section.
(ii)
Electronic-typewriter : 5 @ 500
2.
[Type text]
CAMBRIDGE SCHOOL,NOIDA
(i)
(ii)
(iii)
(iv)
(v)
(vi)
(vii)
Solution
(i)
(ii)
(iii)
(iv)
(v)
(vi)
(vii)
[Type text]
CAMBRIDGE SCHOOL,NOIDA
3. Answer the questions (i) and (ii) after going through the following program:
class Match
{
int Time;
public:
Match()
//Function 1
{
Time=0;
cout<<Match commences<<end1;
}
~Match()
//Function 2
{
cout<<Unallocatted<<end1;
}
Match(int Duration)
//Function 3
{
Time=Duration;
cout<<Another Match begins now<<end1;
}
Match(Match &M)
//Function 4
{
Time=M.Time;
cout<<Like Previous Match <<end1;
}
};
Match s;
i)
Which member function out of function1, function2, function3,
function4 shown in the above definition of class will be called
automatically , when the scope of an object gets over?
ii)
Match M;
//statement1
Match X(M);
//statement2
Which member function out of function1, function2, function3,
function4
shown in the above definition of class will be called on
execution of statement written as statement2? What is this function called?
iii)
Which feature of object oriented programming is depicted in
Function1,Function 3 and Function4.
Solution
(i)
Function1 which is a default constructor will be invoked and Function2
will be called which is a destructor which will destroy object s.
(ii)
Function4 will be called. This is a copy constructor .
(iii) Polymorphism
[Type text]
CAMBRIDGE SCHOOL,NOIDA
1.
2.
3.
Practical Questions
Declare a class address to read and store the address of a person with the data
members
:
name (character string size 20)
House no (int)
Street (character string size 15)
City (character string size 20)
Country(character string size 15)
Include constructor functions . One Default Constructor to assign each data
members to null value (numeric to 0 and string to \0), second one should be a
parameterized constructor to initialize city to DELHI and Country to
INDIA and rest of the data members to user defined parameter values. Also
include the member function which will display the members.
Create a class time that has data members hour, minutes and seconds of int
data types and two constructor functions. One Default constructor should
initialize these data to zero and another parameterized constructor should
initialize these to user supplied values. Also include one member function to
display the data in the format 11:10:59 and another, which will add two
objects of time passed as arguments. Display the value of the added object.
WAP to generate fibonacci series, using a class fib, which have the following
data members and member functions:
Data members
:
first ,second are the first two terms and n is
the no. of terms which should be supplied
into the constructor as parameter.
fib()
:
Constructor function to initialize first and
second to 0 and 1 and n to 10
fib()
:
A parameterized constructor which takes
value of first, second and n from user
gen_fib()
:
Is the function that generates the series for
terms
4. A class circle stores as data member - the radius and area of a circle and two
constructor functions. The first accepts, as argument the radius and calculate
its area. The second is a copy constructor that increases the area of the circle
by 1 and calculates its area. Also include another member function to display
radius and area of the circle