Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to perform the following tasks: Create (define) three functi

ID: 3688026 • Letter: W

Question

Write a C++ program to perform the following tasks:

Create (define) three functions called "sum". Differentiate the functions by the number and type of arguments

In the main function

a. call function1 with arguments 6 and 9. Print the returned value.

b. call function 2 with an integer variable s as its argument (assign a value of 8 to it before calling the function). Print the changed value of s.

c. call function 3 with 12 and 3.5 as arguments. Print the returned value.

All values must be printed on a new line

Explanation / Answer

#include <iostream>
using namespace std;

class OverloadDemo
{
   public:
      int sum(int i,int j)//sum with two integer arguments
      {
        return i+j;
      }

      int sum(int &n)// sum function with one integer argument as reference
      {
        n=n+5;
      }

      double sum(int x,double y)//sum with one integer and one doub le arguments returns double sum
      {
        return x+y;
      }
};

int main(void)
{
OverloadDemo obj;

   int s=obj.sum(6,9);
   cout<<"Return value of first function is "<<s<<endl;
   int x=8;
   obj.sum(x);
   cout<<"sum value after passing x to sum function2 is"<<x<<endl;
    double val=obj.sum(12,3.5);
    cout<<"Return value of function 3 is "<<val<<endl;
   return 0;
}

output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                 

sh-4.3$ main                                                                                         

Return value of first function is 15                                                                 

sum value after passing x to sum function2 is13                                                      

Return value of function 3 is 15.5                                                                   

sh-4.3$                                                                                              

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote