CS 235 Unit 08 Exercise: Overloaded functions and constructors

Table of Contents


1. Introduction

About
Function overloading allows multiple functions to have the same name but different parameters. This can be useful for implementing similar functionality given different amounts of inputs.
Goals
  • Practice with function overloading.
  • Practice with constructor overloading.
  • Practice with copy constructors.
Setup
Make sure to download the starter code, which is here: https://gitlab.com/moosadee/courses/-/tree/main/wip_exercises/starter_code/c3_u08_OverloadedFunctions?ref_type=heads

2. Creating the projcet

Start by creating a new project. You’ll need a file for your main() function (main.cpp) as well as a header and source file for a Product class.

3. Product class declaration

Create a Product class that contains the following private member variables:

  • m_name, a string
  • m_quantityInStock, an integer
  • m_price, a float

The following constructors:

  • A default constructor
  • A parameterized constructor, which takes in a newName (string), newQuantity (int), and newPrice (float).
  • A copy constructor.

The following overloaded functions:

  • Setup with 3 parameters: newName (string), newQuantity (int), and newPrice (float).
  • Setup with 1 parameter: newName (string)

And the following additional functions:

  • std::string GetName() const;
  • int GetQuantityInStock() const;
  • float GetPrice() const;

Context: These functions are marked as const because accessor (getter) functions should not modify the member variables – so we mark them as const to enforce this.

4. Class function definitions

4.1. void Product::Setup(std::string newName, int newQuantity, float newPrice)

Within this function set up the private member variables m_name, m_quantityInStock, and m_price to have the values of the items passed in as parameters.

A common error is to do this: name = m_name; THIS IS INCORRECT!

Also – DO NOT DECLARE m_name, m_quantityInStock, and m_price here – they are declared in the class declaration. This function shouldn’t have ANY VARIABLE DECLARATIONS within the function body!

4.2. void Product::Setup(std::string newName)

Within this function, update the m_name member variable to have the value passed in as the newName.

4.3. Default constructor - Product::Product()

For this constructor, call the Setup function, passing in “UNSET” as the default name, 0 as the default quantity, and 0 as the default price.

4.4. Parameterized constructor - Product::Product(std::string newName, int newQuantity, float newPrice)

For this constructor, call the Setup function, passing in the newName, newQuantity, and newPrice as arguments.

4.5. Copy constructor - Product::Product(const Product& copyMe)

4.6. std::string Product::GetName() const

Return the value of the m_name member variable.

4.7. int Product::GetQuantityInStock() const

Return the value of the m_quantityInStock member variable.

4.8. float Product::GetPrice() const

Return the value of the m_price member variable.

5. Testing the product

Test 1: Set up product with default constructor
EXPECTED Name:     UNSET      ACTUAL Name:     UNSET
EXPECTED Quantity: 0          ACTUAL Quantity: 0
EXPECTED Price:    0          ACTUAL Price:    0

Test 2: Set up product with parameterized constructor
EXPECTED Name:     ASDF       ACTUAL Name:     ASDF
EXPECTED Quantity: 123        ACTUAL Quantity: 123
EXPECTED Price:    200        ACTUAL Price:    200

(etc)

In the main() function we’re going to write some tests that we can use to verify that all the class functionality works as intended. We will create several products and use the different constructors and setup functions, then use cout statements to verify that the item was updated as we expected.

5.1. Test 1: Set up product with default constructor

For this test, create a Product variable without any arguments:

Product product1;

Then, we’ll display the EXPECTED values of the Product’s member variables, and the ACTUAL values:

cout << setw( 30 ) << "EXPECTED Name:     UNSET"  
  << setw( 15 ) << "ACTUAL Name:     " << product1.GetName() 
  << endl;
cout << setw( 30 ) << "EXPECTED Quantity: 0"      
  << setw( 15 ) << "ACTUAL Quantity: " << product1.GetQuantityInStock() 
  << endl;
cout << setw( 30 ) << "EXPECTED Price:    0"      
  << setw( 15 ) << "ACTUAL Price:    " << product1.GetPrice() 
  << endl;

5.2. Test 2: Set up product with parameterized constructor

Create a second product, but pass in 3 arguments. This should trigger the parameterized constructor.

Product product2( "ASDF", 123, 200 );

Use cout statements to display the EXPECTED values (“ASDF”, 123, and 200) and the ACTUAL values.

5.3. Test 3: Set up product with copy constructor

Create a third product, and pass in product2 as the argument. This should trigger the copy constructor.

Product product3( product2 );

Use cout statements to display the EXPECTED values (“ASDF”, 123, and 200) and the ACTUAL values.

5.4. Test 4: Call 3-parameter Setup on a Product

Call the Setup function on product1, passing in three arguments.

product1.Setup( "NAME", 20, 30 );

Use cout statements to display the EXPECTED values (“NAME”, 20, and 30) and the ACTUAL values.

5.5. Test 5: Call 1-parameter Setup on a Product

Call the Setup function on product2, passing in one argument.

product2.Setup( "ITEM" );

Use cout statements to display the EXPECTED values (“ITEM”, 123, and 200) and the ACTUAL values.

5.6. Example test results

Test 1: Set up product with default constructor
EXPECTED Name:     UNSET      ACTUAL Name:     UNSET
EXPECTED Quantity: 0          ACTUAL Quantity: 0
EXPECTED Price:    0          ACTUAL Price:    0

Test 2: Set up product with parameterized constructor
EXPECTED Name:     ASDF       ACTUAL Name:     ASDF
EXPECTED Quantity: 123        ACTUAL Quantity: 123
EXPECTED Price:    200        ACTUAL Price:    200

Test 3: Set up product with copy constructor
EXPECTED Name:     ASDF       ACTUAL Name:     ASDF
EXPECTED Quantity: 123        ACTUAL Quantity: 123
EXPECTED Price:    200        ACTUAL Price:    200

Test 4: Call 3-parameter Setup on a Product...
EXPECTED Name:     NAME       ACTUAL Name:     NAME
EXPECTED Quantity: 20         ACTUAL Quantity: 20
EXPECTED Price:    30         ACTUAL Price:    30

Test 5: Call 1-parameter Setup on a Product...
EXPECTED Name:     ITEM       ACTUAL Name:     ITEM
EXPECTED Quantity: 123        ACTUAL Quantity: 123
EXPECTED Price:    200        ACTUAL Price:    200

Author: Rachel Wil Sha Singh

Created: 2023-09-28 Thu 18:07

Validate