In Lab 04 Tasks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CC-214 Object Oriented Programming Lab

Spring 2022
Issue Date: March 11,2024
LAB-04

The objective of this lab is to:


Practice Types of constructors, Default arguments in a constructor, Passing object by reference,
Return object from a function, Const objects, data members and member functions.

Instructions!

1. Please follow dress code before coming to the lab. Keep your student identity cards with you.
2. This is an individual lab, you are strictly NOT allowed to discuss your solutions with
your fellow colleagues, even not allowed asking how is he/she is doing, it may result in
negative marking. You can ONLY discuss with your TAs or with me.
3. Strictly follow good coding conventions (commenting, meaningful variable and
functions names,properly indented and modular code.
4. Save your work frequently. Make a habit of pressing CTRL+S after every line of code
you write.
5. Beware of memory leaks and dangling pointers.

Task 01: [30 Marks]

Create a class SaleItem that a grocery store might use to represent a particular item being sold at the
store. The class SaleItem should have private member variables to store each of the
following:
• ID (an integer to uniquely identify each item)
• Name (String)
• Quantity (an integer to represent how many such items are present in the store)
• Price (a double value indicating the price of the item in Rupees)
Member Functions:
 Implement a Default constructor for SaleItem class that assigns -999 to the ID, an empty c-
strings to the Name, and assigns -1 to Quantity and Price.
 Implement an Overloaded Parameterized constructor for SaleItem class that accepts the
following 4 values (in the given order) as arguments and assigns them to the appropriate
member variables:
ID, Name, Quantity(Stock), and Price.
 Implement a public member function decrementQuantity of the SaleItem class which
will take an integer value as argument and decrements the quantity of the calling SaleItem
object by that much and returns true. If the parameter is zero or negative, or the value of
parameter is greater than the available quantity of item, then this function should make no
changes and should return false.
 Implement a public member function displayInformation of the SaleItem class. This
function should display all the information about the calling SaleItem object on a single line
on the screen, in a neat and readable way.
 Implement a copy constructor of the SaleItem Class.
 calculateTotalPrice That takes Quantity as an argument and returns the total price of item for
that Quantity.

All member functions of the SaleItem class, which are not supposed to change data
stored in the calling object, should be declared as const member functions.

Madiha Khalid Page 1 of 3


Department of Software Engineering, University of the
Punjab.
CC-214 Object Oriented Programming Lab
Spring 2022
Issue Date: March 11,2024
LAB-04

Before moving to the next step, you must write a driver main function to test the working of
SaleItem class and its member functions.
Write a menu based program using the SaleItem class. Your program should ask the user
that how many SaleItems he/she wants to create. Then, it should dynamically allocate an
array of SaleItems. Then, the program should ask the user to enter the values of all attributes
for each Sale Item object except for the ID which is auto incremented every time an item is added. (i.e.
Object at Index 0 is going to have ID 1, Object at index 1 have ID = 2). The user will be required to
enter the comma-separated details of a SaleItem on a single line, in the
following order: ID, Name, Quantity, Price.

Once you have added all the details of Items you Have to ask the user if he/she wants to Order any
item. If user chooses Yes, then prompt user to enter ID of the item he/she wants to order

and then Quantity of that item. Once ID of item is validated you have to call the function
decrementQuantity for that specific item Object user choose to order. If it returns True Print message
on Console That Order is Done and display the Total Bill of the order and if it returns False Print
message that this Item is not in sufficient amount. Keep on Ordering Until User chooses to exit.

Task 02: [30 Marks]

Additional Functionalities of Person Class You’ve Already Implemented in Practice


Tasks:

You are not allowed to use any built in function for these functions. Except for
stringname. Length (). Make your own Logic Otherwise you will be rewarded Zero.
And in all these functions Objects’ Data members should not be altered.
In case of returning a string make a new string variable in function and return that
string.

1. string concatenateNames (Person& other) const: Concatenates names of the calling


Object and the object passed as argument and returns the resulting string and in main
you have to Display the resulting string.

2. bool CompareNames (Person& other) const: Compares first names of both objects
and returns true if they are same and return False if they are not.

3. string ReverseName () const: Stores the reversed name of calling Object in a string
and return that string and in main Display the reversed name on the console.

4. string Substring (int startindex, int Lastindex) const: Stores the name starting from
Startindex upto Lastindex in a string and return that string. Validate The starting index
i.e. it should be greater than zero and less than Last Index and if the Lastindex
becomes greater than the length of name Consider it to be equal to the length. e.g.
length of name = 8, startindex = 2, lastindex = 10. In this case Lastindex should
become 8.

Madiha Khalid Page 2 of 3


Department of Software Engineering, University of the
Punjab.
CC-214 Object Oriented Programming Lab
Spring 2022
Issue Date: March 11,2024
LAB-04

5. string stringToUpper () const: returns the name of calling object by converting it


into uppercase.

Program Functionality (Main):

 Ask from user the name and age of 1st Person Object and make a const object via
parameterized constructor.
 Then ask the name and age of 2nd Person object and make a const object via parameterized
constructor.
 Make a menu

1. Compare Age
2. Is Adult
3. Concatenate Names
4. Compare Names
5. Reverse Name
6. Make a Sub String
7. Convert name to Upper Case.

Madiha Khalid Page 3 of 3


Department of Software Engineering, University of the
Punjab.

You might also like