Constructors and Destructors
·
A constructor is a member function of a class whose
function name is same as the name of the class.
·
It is used to initialize the values of data members.
·
It does not return a value nor is it void.
·
It may contain parameters.
·
It will be invoked whenever the object of the class is
created.
·
The memory for the class will be allocated only when
the object is created.
·
When no constructors are used explicitly by the
programmer, the contructor constructs garbage value into its data members.
Eg of
defining a constructor:
class
sample
{
int x;
public:
sample()
{
cout<<"constructing..";
x=5;
}
...
};
void main()
{
y=s.y;
}
};
void main()
{
sample
obj1,obj2(150,100); // statement to invoke the default and parameterized
constructors
sample
obj3=obj2; // statement to invoke the copy constructor
}
Function
1,2 and 3 together are constitute constructor
overloading.
In Object
oriented Programming structure, this concept is called as polymorphism.
Calls to the constructor may be implicit or explicit.
Implicit -
sample s(13,14);
Explicit -
sample s=sample(13,14);
The
explicit call to a constructor allows you to create a temporary instance
Temporary
instance - A temporary instance is an instance that is instantiated by a class
to invoke a member function or functions and is immediately discarded.
eg:
class
sample
{int i,j;
public:
sample(int
a, int b)
{
i=a;j=b;}
void
print()
{cout<<i<<"
"<<j;}
};
void test()
{
sample
s1(2,5);
s1.print();
sample(100,200).print();
}
In order to
use temporary instance, a parameterized constructor should have been present.
What is destructor?
Destructor is a member function which destructs or deletes an object.
When is destructor called?
A destructor function is called automatically when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called
How destructors are different from a normal member function?
Destructors have same name as the class preceded by a tilde (~)
Destructors don’t take any argument and don’t return anything.
Exercises
Answer the questions(i) and (ii) after going through the following
class:
class planet
{
char name[20];char distance[20];
public:
planet() //Function 1
{
strcpy(name, "Venus");
strcpy(distance,"38 million km");
}
void display(char na[],char d[]) //Function 2
{
cout<<na<<"has "<<d<<" distance from Earth"<<endl;
}
planet(char na[], char d[]) //Function 3
{
strcpy(name,na);
strcpy(distance,d);
}
~planet() //Function 4
{
cout<<"Planetarium time over!!!"<<endl;
}
};
I. What is Function 1 referred as? When will it be executed?
Default constructor. It will be executed when the object is created.
II. Write suitable C++ statement to invoke Function 2.
planet p;
p.display("Mars","20 million km");Observe the following C++ code and answer the questions (i) and (ii).
Note : Assume all necessary files are included.
class TEST
{
long TCode;
char TTitle[20];
float Score;
public:
TEST() //Member Function 1
{
TCode=100;strcpy(TTitle,"FIRST Test");Score=0;
}
TEST(TEST &T) //Member Function 2
{
TCode=E.TCode+1;
strcpy(TTitle,T.TTitle);
Score=T.Score;
}
};
void main()
{
_______________ //Statement 1
_______________ //Statement 2
}
(i) Which Object Oriented Programming feature is illustrated by
the Member Function 1 and the Member Function 2 together
in the class TEST ?
(ii) Write Statement 1 and Statement 2 to execute Member
Function 1 and Member Function 2 respectively.
Observe the following C++ code and answer the questions (i) and (ii).
Note : Assume all necessary files are included.
class EXAM
{
long Code;
char EName[20];
float Marks;
public:
EXAM() //Member Function 1
{
Code=100;strcpy(EName,"Noname");Marks=0;
}
EXAM(EXAM &E) //Member Function 2
{
Code=E.Code+1;
strcpy(EName,E.EName);
Marks=E.Marks;
}
};
void main()
{
_____________ //Statement 1
_____________ //Statement 2
}
(i) Which Object Oriented Programming feature is illustrated by the Member
Function 1 and Member Function 2 together in the class EXAM ? 1
(ii) Write Statement 1 and Statement 2 to execute Member Function 1 and
Member Function 2 respectively.
Observe the following C++ code and answer the questions (i) and (ii). Assume all
necessary files are included :
class FICTION
{
long FCode;
char FTitle[20];
float FPrice;
Public:
FICTION() //Member Function 1
{
cout<<”Bought”<<endl;
FCode=100;strcpy(FTitle,”Noname”);FPrice=50;
}
FICTION(int C,char T[],float P) // Member Function 2
{
FCode=C;
strcpy(FTitle,T);
FPrice=P;
}
void Increase(float P) // Member Function 3
{
FPrice+=P;
}
void Show() // Member Function 4
{
cout<<FCode<<”:”<<FTitle<<”:”<<FPrice<<endl;
}
~FICTION() // Member Function 5
{
cout<<”Fiction removed!” <<endl;
}
};
void main() //Line 1
{ //Line 2
FICTION F1,F2(101,”Dare”,75); //Line 3
for (int I=0;I<4;I++) //Line 4
{ //Line 5
F1.Increase(20);F2.Increase(15); //Line 6
F1.Show();F2.Show(); //Line 7
} //Line 8
} //Line 9
(i) Which specific concept of object oriented programming out of the following
is illustrated by Member Function 1 and Member Function 2 combined
together ? 1
• Data Encapsulation
• Data Hiding
• Polymorphism
• Inheritance
(ii) How many times the message “Fiction removed!” will be displayed
after executing the above C++ code ? Out of Line 1 to Line 9, which line is
responsible to display the message “Fiction removed!” ?
No comments:
Post a Comment