COC2071 - Lab Manual 04 (Classes, Constructors)
COC2071 - Lab Manual 04 (Classes, Constructors)
Objective: In this hands-on lab, you will learn the fundamental concepts of classes, constructors, and
copy constructors in Object-Oriented Programming (OOP). You will create a simple program in a
programming language of your choice to reinforce your understanding of these concepts through
practical implementation.
Task 1:
1) Create a new class named Person with attributes such as name, age, and gender. Define the
class methods to set and get the values of these attributes. Instantiate an object of the Person
class and print out its attributes.
2) Modify the Person class to include constructors. Create a parameterized constructor to initialize
the attributes when an object is created. Create default constructors (constructors without
parameters) that set default values for the attributes. Instantiate objects using both
constructors and print out their attributes.
3) Create a copy constructor for the Person class that takes another Person object as a parameter
and creates a new object with the same attributes. Instantiate a new Person object using the
copy constructor. Modify the attributes of the original object and ensure that the copied object
remains unaffected.
4) Create multiple Person objects using different constructors. Test the copy constructor by
creating a copy of an existing Person object and modifying the original object afterward. Print
out the attributes of all objects to validate the changes.
5) Implement input validation for the attributes (e.g., age cannot be negative). Create a method
within the Person class to display a formatted string containing the person's information. Create
a subclass of Person with additional attributes and methods.
Task 2:
1) Create a class Room with the fields room number, room type, and AC installed or not.
2) Take the values of 10 rooms from the user and print these values through properties.
Task 3:
1/1
Object Oriented Programming – COC2071
Lab Plan 04 (Classes, Constructors, and Copy Constructors)
Task 4:
1) Create a class Student with registration number, name, cgpa, and discipline fields. Provide the
followings:
2) Create an array of 5 Student objects.
3) Input registration number, name, cgpa, and discipline from the user through relevant
properties.
4) Print the student’s data through PrintInfo method.
5) Print the record of the student who is the top of the class through Topper method.
Task 5:
1) Create a class Friend with name, mobile number, and email as data fields. The class should have
the following functionalities:
2) The class should be able to add record in the friend’s directory and print message “Directory is
filled, no more contacts can be added!!!!” if the directory has filled i.e., out of memory. The
directory should allow a maximum of 10 friends to store.
3) The class should be allowed to print all the records from the directory in tabular form.
4) The class should allow to search the friend data by asking friend’s name and print the record of
the friend, if directory contains the specified friend else "No friend with given name" message
should be printed.
2/1