overload.cpp 1 #include "overload.h" 2 3 4 5 6 7 } 8 overload.h 123456 #ifndef OVERLOAD H 2 #define OVERLOAD H 3 // Write your functions here float product (const double& a, const double& b) { return a * b; 4 #include 5 float product(const double& a, const double& b, const double& c) 7 { 8 return a*b* c; 9} 10 11 12 13 #endif Tester.cpp 1 #include 2 using namespace std; 3 4 #include "overload.h" 5 6 7 8 9 10 11 12 13 14 15 int main() { } // 2 arguments cout << "product(6.2, 6.4): " << product (6.2, 6.4) << endl; cout << "Expected: 39.68000000000001" << endl; // 3 arguments cout << "product (1.1, 7.0, 4.1): " << product (1.1, 7.0, 4.1) << endl; cout << "Expected: 31.57" << endl;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Having trouble writing these functions, what am I doing wrong?

**overload.cpp**

```cpp
#include "overload.h"

// Write your functions here
float product(const double& a, const double& b)
{
    return a * b;
}
```

**overload.h**

```cpp
#ifndef OVERLOAD_H
#define OVERLOAD_H

#include <string>

float product(const double& a, const double& b, const double& c)
{
    return a * b * c;
}

#endif
```

**Tester.cpp**

```cpp
#include <iostream>
using namespace std;

#include "overload.h"

int main()
{
    // 2 arguments
    cout << "product(6.2, 6.4): " << product(6.2, 6.4) << endl;
    cout << "Expected: 39.68000000000001" << endl;

    // 3 arguments
    cout << "product(1.1, 7.0, 4.1): " << product(1.1, 7.0, 4.1) << endl;
    cout << "Expected: 31.57" << endl;
}
```

**Explanation:**

This code sample demonstrates function overloading in C++. 

1. **overload.cpp**: Contains a function `product` that takes two `double` arguments and returns their product.

2. **overload.h**: Defines a similar function `product` that takes three `double` arguments and calculates their product. This header file ensures the function is not defined multiple times using include guards (`#ifndef`, `#define`, `#endif`).

3. **Tester.cpp**: 
   - Includes necessary libraries and the `overload.h` header file.
   - In the `main` function, it calls both versions of the `product` function:
     - The first call with two arguments, expecting the result `39.68000000000001`.
     - The second call with three arguments, expecting the result `31.57`.
   - Outputs the results to the console.

This illustrates how C++ allows the use of the same function name with different parameters to perform specific tasks.
Transcribed Image Text:**overload.cpp** ```cpp #include "overload.h" // Write your functions here float product(const double& a, const double& b) { return a * b; } ``` **overload.h** ```cpp #ifndef OVERLOAD_H #define OVERLOAD_H #include <string> float product(const double& a, const double& b, const double& c) { return a * b * c; } #endif ``` **Tester.cpp** ```cpp #include <iostream> using namespace std; #include "overload.h" int main() { // 2 arguments cout << "product(6.2, 6.4): " << product(6.2, 6.4) << endl; cout << "Expected: 39.68000000000001" << endl; // 3 arguments cout << "product(1.1, 7.0, 4.1): " << product(1.1, 7.0, 4.1) << endl; cout << "Expected: 31.57" << endl; } ``` **Explanation:** This code sample demonstrates function overloading in C++. 1. **overload.cpp**: Contains a function `product` that takes two `double` arguments and returns their product. 2. **overload.h**: Defines a similar function `product` that takes three `double` arguments and calculates their product. This header file ensures the function is not defined multiple times using include guards (`#ifndef`, `#define`, `#endif`). 3. **Tester.cpp**: - Includes necessary libraries and the `overload.h` header file. - In the `main` function, it calls both versions of the `product` function: - The first call with two arguments, expecting the result `39.68000000000001`. - The second call with three arguments, expecting the result `31.57`. - Outputs the results to the console. This illustrates how C++ allows the use of the same function name with different parameters to perform specific tasks.
## Error Message Explanation

### Context:
While running `Tester.cpp`, an error was encountered due to a function call with insufficient arguments. The code attempted to call a function named `product`, expecting three double arguments but provided only two.

### Error Details:

#### In `int main()`
- **File & Line:** `Tester.cpp:9:53`
- **Error:** Too few arguments to function `float product(const double&, const double&, const double&)`.
- **Code Snippet:** 
  ```cpp
  9 | cout << "product(2.0, 8.0): " << product(2.0, 8.0) << endl;
    |                                        ^
  ```
- The function `product` was called with only two parameters: `2.0` and `8.0`.

#### Declaration Note:
- **Included File:** `overload.h`
- **Line:** `6:7`
- **Function Declaration:**
  ```cpp
  6 | float product(const double& a, const double& b, const double& c)
    |       ^~~~~~
  ```
- This line shows that the `product` function is declared to accept three `const double&` parameters.

### Solution:
To resolve this error, ensure that the function call to `product` in `Tester.cpp` provides three arguments as required by its declaration. Add a third `double` type argument to the `product` function call within `int main()`.
Transcribed Image Text:## Error Message Explanation ### Context: While running `Tester.cpp`, an error was encountered due to a function call with insufficient arguments. The code attempted to call a function named `product`, expecting three double arguments but provided only two. ### Error Details: #### In `int main()` - **File & Line:** `Tester.cpp:9:53` - **Error:** Too few arguments to function `float product(const double&, const double&, const double&)`. - **Code Snippet:** ```cpp 9 | cout << "product(2.0, 8.0): " << product(2.0, 8.0) << endl; | ^ ``` - The function `product` was called with only two parameters: `2.0` and `8.0`. #### Declaration Note: - **Included File:** `overload.h` - **Line:** `6:7` - **Function Declaration:** ```cpp 6 | float product(const double& a, const double& b, const double& c) | ^~~~~~ ``` - This line shows that the `product` function is declared to accept three `const double&` parameters. ### Solution: To resolve this error, ensure that the function call to `product` in `Tester.cpp` provides three arguments as required by its declaration. Add a third `double` type argument to the `product` function call within `int main()`.
Expert Solution
Program approach

Prototype of both the overloaded functions must be declared in "overload.h" and the functions must be defined in "overload.cpp". Import "overload.cpp" in "tester.cpp" and invoke the functions to get the expected output.

Step 1: Declare the function prototypes for product function with two and three arguments in the overload.h header file.

Step 2: Define the overloaded member functions in overload.cpp file.

Step 3: Define the main() function in Tester.cpp. Invoke the product() member function by passing two and three arguments and display the value returned.

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education