0% found this document useful (0 votes)
10 views6 pages

Lab 5

The document outlines the lab tasks for a course on Object Oriented Programming at the University of Central Punjab for Fall 2024. It includes instructions for coding practices, multiple tasks involving C++ programming concepts such as passing objects, deep copying, dynamic memory allocation, and managing static variables. Students are required to complete the tasks within specified time limits and submit their code files accordingly.

Uploaded by

asifhuzaifa123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Lab 5

The document outlines the lab tasks for a course on Object Oriented Programming at the University of Central Punjab for Fall 2024. It includes instructions for coding practices, multiple tasks involving C++ programming concepts such as passing objects, deep copying, dynamic memory allocation, and managing static variables. Students are required to complete the tasks within specified time limits and submit their code files accordingly.

Uploaded by

asifhuzaifa123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

University of Central Punjab

Faculty of Information Technology


Object Oriented Programming
Fall 2024

Lab 05
 Passing objects as arguments
Topics to be  Returning Objects
 Copy Constructor vs Member-wise Copy
covered:  Deep copy
 The this Pointer
 Arrays of Objects.
 Pointers to Objects.
 Array of Pointers to Objects.
 Friend functions and friend classes

Instructions:
 Indent your code properly.
 Use meaningful variable and function names. Use the camelCase notation.
 Use meaningful prompt lines/labels for all input/output.
 Strictly follow the Good Code Writing Style which is already shared by your
theory teacher.
 In each file of your task either it is .cpp file or .h file Your name and roll no must
be mention in the start of your file as comment. For example, //L1F22BSCS1234
– Ali Rehman
 Students must follow the "Code Writing Guide".
 You are not allowed to use string. You can use Cstring only as character arrays /
character pointer.
 You are not allowed to make array of objects in main().
 Perform all the tasks in separate source, header and main driver file.
 Paste your code in the given space below and submit your .cpp and .h file along
with your .docx file.

Understanding of the question is part of the evaluation.


Mobile phone is strictly prohibited during the lab either lab is graded or not.

Students are required to complete the following tasks in lab timings. Submission of each task in
prescribed timing is mandatory.
Task 1: Time: 30 minute
Design a C++ program that models the properties and operations of a Rectangle. The program
should allow creating Rectangle objects with specified dimensions (length and width). It should also
include functions that:

1. Pass Objects as Arguments: A function increaseDimensions() should take a Rectangle object


by reference along with additional values for length and width. It should increase the
rectangle's dimensions and reflect the changes in the original object.

2. Return Objects from Functions: A function combineRectangles() should take two Rectangle
objects as arguments, add their lengths and widths, and return a new Rectangle object
representing the combined dimensions.

The program should demonstrate these functions by displaying the initial dimensions, modifying the
first rectangle's dimensions, and creating a combined rectangle from two existing rectangles. Finally,
it should display each rectangle's details, including length, width, and area.

Write a following function for the program.

Constructor:

Initializes a Rectangle object with specified length and width.

Setters:

setLength(double l): Sets the length of the rectangle.

setWidth(double w): Sets the width of the rectangle.

Getters:

getLength() const: Returns the length of the rectangle.

getWidth() const: Returns the width of the rectangle.

area() const:

Calculates and returns the area of the rectangle.

display() const:

Displays the length, width, and area of the rectangle.

increaseDimensions(Rectangle &rect, double addLength, double addWidth):

Takes a Rectangle object by reference and two additional values.

Increases the rectangle's length and width by the specified values.

combineRectangles(const Rectangle &r1, const Rectangle &r2):

Takes two Rectangle objects as arguments.

Creates and returns a new Rectangle object with combined dimensions (sum of lengths and widths
of r1 and r2).

Sample Output
Task 2: (Deep Copy of the Object) Time: 30 minutes

Write a program in C++ to create a class called “Person” with attributes name and age.
 Implement a constructor that takes two parameters and initializes the attributes.
 Implement a copy constructor that performs a deep copy of the object.
 Create two Person objects p1 and p2 with different values for name and age, and copy p1
to p2.
 Modify the value of name of p1.
 Display the modified values of name of p2

Task 3 Time: 30 minutes

Design a C++ class named MyClass to manage an integer data value using dynamic memory
allocation. Implement functionality for deep copy operations.

 The MyClass class contains an integer pointer int* data for dynamic memory
allocation.
 It features constructors, destructors, getter/setter functions, and deep copy
constructors.
 The problem includes testing with copy operations to ensure the correct behavior.

Task 4: (Dynamic Array of Persons)

Create a C++ program that defines a class called Person with attributes

• Name(char*)

• Age(int)

• Address(char*)

• date of birth (char*)

1. Implement appropriate setters and getters function for all the variables.

2. Implement the default constructor of the class to set the values of data members.

3. Implement the destructor to deallocate the memory if any.

4. Implements a getInput function to take the input from user in appropriate data members.

5. Implement the display function to display the person’s details.

6. Implement a function to sort the persons based on their ages in ascending order.

7. In main() function implement a dynamic array to store a group of persons. Allow the user to

input details for each person and dynamically adjust the size of the array to accommodate all

persons.

Task 5: (Static Variable and Function)

8. Extend the Person class with a static variable to keep track of the number of objects created.

9. Update the constructor and implement parameterized constructor as per the need.

10. Implement a static function that returns the total number of persons created.

11. Create instances of the Person class, and display the total count after each object creation.

12. Enhance the Person class to include a private static variable that represents the average age

of all created persons

13. Implement a static function to calculate and update this average whenever a new person is

added. Create instances of the Person class, and display the average age after each object

creation.
Task 6: (Use of constant functions, parameters, and attributes)

• Define a Circle class that represents a circle with a radius.

• The pi attribute is a constant attribute, which is initialized with a constant value of pi.

• The getArea() function is a constant function that calculates the area of the circle and returns

it, and it is marked with the const keyword at the end of the function declaration, indicating

that it does not modify the object.

• The printInfo() function takes a constant parameter message of type std::string, which is

passed by reference to avoid unnecessary copying, and it is also marked as const, indicating

that it does not modify the object.

• The setRadius() function is a non-constant function that modifies the radius of the circle.

• Create a const Circle object, like c2, we are not allowed to call non-constant functions on it,

as it is considered a constant object. Attempting to modify a constant object using a nonconstant


function will result in a compilation error.

Task 7 (constant member function and static variable and static function)

Problem Statement:

Design and implement a C++ class named Employee that demonstrates the use of constructor
initializer lists, constant members, static members, constant objects, and static objects. The class will
use character pointers instead of std::string.

Requirements:

Attributes:

const char* companyName: A constant character pointer that holds the name of the company and
should be initialized using a constructor initializer list.

int employeeID: A unique identifier for each employee.

char* name: A character pointer to hold the name of the employee. The memory for this name
should be allocated dynamically in the constructor.

static int employeeCount: A static variable to track the number of employees created.

Member Functions:

Constructor: Initializes employeeID and name using a constructor initializer list and increments
employeeCount. The name should be allocated dynamically.

Destructor: Frees the dynamically allocated memory for the name.

Const Member Function: void displayInfo() const that displays the employee's details, including the
company name, employee ID, and name.

Static Function: static int getEmployeeCount() that returns the total number of employees created.
Demonstration:

Create a const object of the Employee class and demonstrate access to only const member
functions.

Create a static object of the Employee class and show how its data persists throughout the program.

----------------------------------------------------------------END------------------------------------------------------------

You might also like