Lab Report 2
Lab Report 2
Experiment No. : 02
Date of Exp. : 2.10.2024
Date of Submission : 24.10.24
Remarks : Name : ALI HOSEN
ID : 2108039
Level : 02
Term : 02
Group : 02
Experiment Name:
Constructor and Destructor in C++
Objectives
• Introduce with the Constructor Class in C++.
1
Example 1:
Write C++ program to demonstrate the use of default constructor.
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
c l a s s wal {
private :
do uble l e n g t h ;
public :
wal ( ) {
length =5.5;
cout <<”C r e a t i n g a w a l l :”<< e n d l ;
cout <<”l e n g t h = ”<<l e n g t h <<e n d l ;
}
};
i n t main ( ) {
wal wal1 ;
}
Output Screenshot
Example 2:
Write C++ program to demonstrate the use of Parameterized Con-structor.
#i n c l u d e <i o s t r e a m >
2
u s i n g namespace s t d ;
class rect {
private :
do uble l e n g t h ;
do uble h e i g h t ;
public :
r e c t ( double a , double b ){
l e n g t h=a ;
h e i g h t=b ;
}
do ub le c a l a r e a ( ) {
return length ∗ height ;
}
};
i n t main ( ) {
r e c t r e c t 1 (10 ,5);
rect rect2 (8 ,4);
cout <<”Area o f t he r e c t 1 : ”<<r e c t 1 . c a l a r e a ()<< e n d l ;
cout <<”Area o f t he r e c t 2 : ”<<r e c t 2 . c a l a r e a ()<< e n d l ;
}
Output Screenshot
3
Example 3:
Write a C++ program to understand the Destructor Class in C++.
#i n c l u d e <i o s t r e a m >
#i n c l u d e <c s t r i n g >
u s i n g namespace s t d ;
c l a s s wal
{
int side ;
public :
˜ wal ( )
{
cout <<”D e s t r u c t o r C a l l e d ” ;
}
};
i n t main ( )
{
wal c ;
}
Output Screenshot
4
Example 4:
Suppose you have a Savings Account with an initial amount of 500 and you have to add
some more amount to it. Create a class ’AddMoney’ with a data member named ’amount’
with an initial value of 500. Now make two constructors of this class as follows:
• having a parameter which is the amount that will be added to the Savings Account.
Create an object of the ’AddMoney’ class and display the final amount in the Savings
Account.
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
c l a s s Addmoney
{
private :
do uble amount ;
public :
Addmoney ( )
{
amount =500;
}
Addmoney ( double a )
{
amount=500+a ;
}
v o i d displayamount ( )
{
cout <<”F i n a l amount i n t he s a v i n g s account = ”<<amount<<e n d l ;
}
5
};
i n t main ( )
{
Addmoney noadd ;
noadd . displayamount ( ) ;
Addmoney addmoney ( 2 0 0 ) ;
addmoney . displayamount ( ) ;
}
Output Screenshot
Example 5:
Write a C++ Program to define a class Car with the following specifica- tions:
Private members: car name, model name, fuel type: string type mileage: float type
price: double type Public members: displaydata(): Function to display the data members
on the screen.
Use Constructor (both Default and prameterized) and destructor. When no pa- ram-
eter is passed the default constructor will be called with a message ”Default
constructor has been called”.
#i n c l u d e <i o s t r e a m >
#i n c l u d e <s t r i n g >
u s i n g namespace s t d ;
c l a s s Car
{
private :
6
s t r i n g carName ;
s t r i n g modelName ;
s t r i n g fuelType ;
f l o a t mileage ;
do uble p r i c e ;
public :
Car ( ) {
carName = ” Mercedes ” ;
modelName = ”C−C l a s s ” ;
fuelType = ” D i e s e l ” ;
mileage = 18;
price = 42000;
}
void displayData ( ) {
cout << ”Car Name : ” << carName << e n d l ;
cout << ”Model Name : ” << modelName << e n d l ;
cout << ” Fuel Type : ” << f u e l T y p e << e n d l ;
cout << ” M i l e a g e : ” << m i l e a g e << ” km/ l ” << e n d l ;
cout << ” P r i c e : $” << p r i c e << e n d l ;
}
˜Car ( ) {
7
cout << ” D e s t r u c t o r has been c a l l e d f o r ” << carName << e n d l ;
}
};
i n t main ( ) {
Car c a r 1 ;
Car c a r 2 ( ” Toyota ” , ”Camry ” , ” P e t r o l ” , 1 5 , 2 8 0 0 0 ) ;
c o u t << ” F i r s t c a r d e t a i l s : ” << e n d l ;
car1 . displayData ( ) ;
c o u t << e n d l ;
c o u t << ” Second c a r d e t a i l s : ” << e n d l ;
car2 . displayData ( ) ;
return 0;
}
Output Screenshot
8
Discussion
• In this experiment the creation of constructors and destructors in C++ was studied.
Their roles in managing object lifecycles and ensuring proper resource handling were
understood.
• A common mistake made was the omission of semicolons at the end of class declara-
tions or function definitions. This simple error resulted in confusion and compilation
failures.