BCE-2A OOP LAB
AIR UNIVERSITY
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING
EXPERIMENT: 04
Lab Title: Constructor and its types & Destructor in C++
Student Name:
Objectives:
The objective of this lab is to develop student understanding related to constructor and its types &
destructor in C++.
LAB ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate
the results
Effective use of lab
equipment and follows
the lab safety rules
Total Marks: Obtained Marks:
LAB REPORT ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)
Data presentation
Experimental results
Conclusion
Total Marks: Obtained Marks:
Date: Signature:
Submitted To: Engr. Sidrish Ehsan
Date: 18-03-2025
Page | 1
BCE-2A OOP LAB
LABORATORY
EXPERIMENT NO.4
Lab Task 01:
Create a class named Employee with the following private attributes:
• string name (Employee’s Name)
• int empID (Employee ID)
• double salary (Basic Salary)
Implement constructors:
• Default constructor initializes name as “Unknown”, empID as 0, and salary as 0.0.
• Parameterized constructor allows the user to provide values for name, empID, and salary.
Implement get function to display employee details.
Ensure encapsulation (private attributes).
Implement destructor to release memory.
In main(), create:
• An employee using the default constructor.
• An employee using the parameterized constructor with user-defined values.
• Ask user to enter value at run time in main.
Page | 2
BCE-2A OOP LAB
Lab Task 02:
Create a class named Student with the following private attributes:
• string name (Student Name)
• int rollNo (Roll Number)
• float marks (Marks Obtained)
Implement the following constructors and destructor:
• Parameterized Constructor → Initializes name, rollNo, and marks.
• Use if-else to determine the student status “Pass”, “Fail” based on the marks
• Copy Constructor → Creates a copy of an existing Student object. (Implicit & Explicit)
• Destructor → Displays a message when an object is destroyed.
Implement a get function to display student details.
Ensure encapsulation.
Implement a destructor to display a message upon object destruction.
In main(), create:
• A student using the parameterized constructor.
• Ask User to enter information at run time in main.
• A new student using the copy constructor (copying the first student).
• Display details of both students.
• Observe the destructor message when objects go out of scope.
Page | 3
BCE-2A OOP LAB
Page | 4
BCE-2A OOP LAB
Lab Task 03:
Create a class named Rectangle with the following private attributes:
• int length (Length of the rectangle)
• int width (Width of the rectangle)
Implement the following constructors and destructor:
• Default Constructor → Initializes length = 1 and width = 1.
• Parameterized Constructor (one parameter) → Initializes length = width = value.
• Parameterized Constructor (two parameters) → Initializes length and width with user-
defined values.
• Destructor → Displays a message when a Rectangle object is destroyed.
Implement a function to calculate and return the area of the rectangle.
In main(), create:
• A rectangle using the default constructor.
• A rectangle using the single-parameter constructor.
• A rectangle using the two-parameter constructor.
• Display the area of all rectangles.
Page | 5
BCE-2A OOP LAB
CONCLUSION
• Constructors in C++ initialize objects automatically and can be of various types, including
default, parameterized, and copy constructors.
• A default constructor provides initial values, while parameterized constructors allow custom
initialization.
• The destructor is a special member function that is called automatically when an object goes
out of scope, ensuring resource cleanup.
• Proper use of constructors and destructors enhances code efficiency, reliability, and memory
management in C++.
Page | 6