-
Notifications
You must be signed in to change notification settings - Fork 1
W6 ‐ References and Overloading
Join the sixth workshop at the dedicated LINK 💻
You are tasked with writing a C++ program that demonstrates the use of references to swap two values. Your program should show how references can be used to modify values in functions and how they affect the original variables.
Instructions:
- Implement a function named swapValues that takes two integer references as parameters and swaps their values.
Here’s a more advanced task using references in a way that emphasizes their utility in modifying complex data structures. This example involves a simple inventory system with an Item
class and an Inventory
class to manage a collection of items. The task will highlight the use of references to access and modify objects directly within a container (e.g., a vector of Item
objects).
Instructions:
- Define an Item class with attributes like
name
,quantity
, andprice
. - Define an Inventory class that holds a collection of
Item
objects. NB: use the 'vector' class - Create functions to:
- Add a new item to the inventory.
- Update an existing item’s quantity and price by using references. Return
true
orfalse
depending if the item has been found and modified or not. - Display the inventory items. NB: inspect the
inventory_test.cpp
file to understand the expected format returned from this method.
By using references, the program should modify items directly in the inventory without copying them, which is crucial for large collections.
You are tasked with creating a C++ program that demonstrates function overloading. Your program should define multiple overloaded functions with the same name but different parameter types. Each overloaded function should perform a specific mathematical operation based on the input types.
Instructions:
- Define a function named calculate with the following overloaded versions:
-
int calculate(int a, int b)
: This version should return the sum of two integers. -
double calculate(double a, double b)
: This version should return the product of two double-precision floating-point numbers. -
int calculate(int a, int b, int c)
: This version should return the result of subtracting c from the product of a and b.
-