0% found this document useful (0 votes)
2 views5 pages

Lab 4

The document outlines Lab 04 for the Object Oriented Programming course at the University of Central Punjab, focusing on passing objects as arguments, returning objects, and implementing copy constructors. It includes detailed instructions for coding practices, tasks involving classes such as Car, Rectangle, Person, and MyClass, and emphasizes the importance of proper coding style and submission guidelines. Students are required to complete the tasks within specified time limits, demonstrating their understanding of shallow and deep copy concepts.

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)
2 views5 pages

Lab 4

The document outlines Lab 04 for the Object Oriented Programming course at the University of Central Punjab, focusing on passing objects as arguments, returning objects, and implementing copy constructors. It includes detailed instructions for coding practices, tasks involving classes such as Car, Rectangle, Person, and MyClass, and emphasizes the importance of proper coding style and submission guidelines. Students are required to complete the tasks within specified time limits, demonstrating their understanding of shallow and deep copy concepts.

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/ 5

University of Central Punjab

Faculty of Information Technology


Object Oriented Programming
Fall 2024

Lab 04
 Passing objects as arguments
Topics to be  Returning Objects
covered:  Copy Constructor vs Member-wise Copy
 Deep copy
 The this Pointer

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 minutes

Write a class named Car that has the following private member variables:
 yearModel: An int that holds the car’s year model.
 make: A character pointer that holds the make of the car.
 speed: An int that holds the car’s current speed.
In addition, the class should have the following constructor and other public member
functions.
1. Write the default constructor which set the appropriate 0 values to data members.
2. Write the appropriate parameterized constructor should accept the car’s year model
and make as arguments. These values should be assigned to the object’s yearModel
and make member variables. The constructor should also assign 0 to the speed
member variables.
3. Write aappropriate getter and setter functions to get the values stored in an object’s
yearModel, make, and speed member variables.
4. Define the accelarete() function which should add 5 to the speed member variable
each time it is called.
5. Define the brake() function should subtract 5 from the speed member variable each
time it is called.

Notes: Validate the values in the above function.

Hint: Car speed cannot be negative and above from 150. At the time of brake if car speed is near
to zero it’s mean car is already stopped.

Demonstrate the class in a program that creates a Car objects, and then calls the accelerate
function five times. After each call to the accelerate function, get the current speed of the car
and display it. Then, call the brake function five times. After each call to the brake function,
get the current speed of the car and display it.

In this task accelerate and brake functions are bonus task.


Sample output:

Task 2: (Shallow Copy of the Object) Time: 30 minutes


Enter Car make year : 2002
Enter Car make Company : Toyota

Car Year Model: 2002


Car Make: Toyota
Write a program in C++ to create a class Rectangle with attributes length and width.
 Implement a constructor that takes two parameters and initializes the attributes.
 Implement a copy constructor.
 Create two Rectangle objects r1 and r2 with different values for length and width, and
copy r1 to r2.
 Modify the values of length and width of r1.
 Display the modified values of length and width of r2?

Rectangle.h

Rectangle.cpp

Main.cpp

Output:

Task 3: (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
Person.h

Person.cpp

Main.cpp

Output:

Task 4 Time: 30 minutes

Design a C++ class named MyClass to manage an integer data value using dynamic memory
allocation. Implement functionality for both shallow copy and 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy