Lab 4
Lab 4
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.
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.
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.
Rectangle.h
Rectangle.cpp
Main.cpp
Output:
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:
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.