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

C++ Program Overload a function add(result, p1, p2) that can add two integers (1

ID: 3686290 • Letter: C

Question

C++ Program

Overload a function add(result, p1, p2) that can add two integers (1 + 2 -> 3), chars (‘a’ + ‘b’ -> “ab”), and add(result, p0, p1, p2) to add three strings (“hello” + “c++” + “world” -> “helloc++world”). The first argument result is a call-by-reference argument and should contain the result of the summation. The type of its value should be int, string and string for the three functions. Arguments p1 and p2 should have default parameters. P0 should not have default parameters. When only one argument is provided (for the first two functions) or two arguments are provided (for the third function), the add function should use 1 (‘1’, “11”) as p1 and 2 (‘2’, “22”) as p2, as default parameters, and should display the sum.

Constraints:

· All functions must have return type void.

· Sum of two integers is an int.

· Sum of two characters is a string.

· Sum of two strings is a string.

· You can assume that there is no space in string argument.

· Provide prototypes of all three functions in the beginning of your file.

Main function should call each function twice: once with user input arguments, a second time with least necessary arguments (such that default arguments will be used).

Example output (bold texts are input by user):

> Please input two arguments (int, int) to add:

> 123 987

> 123 + 987 = 1110

> Adding default arguments (int, int):

> 1 + 2 = 3

> Please input two arguments (char, char) to add:

> a b

> a + b = ab

> Adding default arguments (char, char):

> 1 + 2 = 12

> Please input three arguments (string, string, string) to add:

> over loading function

> over + loading + function = overloadingfunction

> Please input one argument (string) to be added with default parameters:

> modified

> modified + 11 + 22 = modified1122

Explanation / Answer

#include <iostream>

#include <cstring>

using namespace std;

void add(int, int);

void add(char *s1,char *s2);

void add(char *s1,char *s2, char *s3);

int main() {

    int a, b;

    char c1, c2;

    char s1[100], s2[100], s3[100];

    cout<<"Enter two integers";

    cin>>a>>b;

    add(a,b);

    add(2,4);

    cout << "Enter two char: ";

    cin >> s1 >> s2;

    add(s1,s2);

    cout << "Enter three string: ";

    cin >> s1 >> s2 >> s3;

    add(s1,s2,s3); //here you can pass default arguments

   

    return 0;

}

void add(char *s1, char *s2)

{

   

    int i, j;

    char temp;

    for(i=0; s1[i]!=''; ++i); /* i contains length of string s1. */

    for(j=0; s2[j]!=''; ++j, ++i)

    {

        s1[i]=s2[j];

    }

    s1[i]='';

   

    cout << "After char concatenation: " << s1;

   

}

void add(char *s1, char *s2, char *s3)

{

    char s4[100];

    //concatenates str1 and str2

    strcat( s1, s2);

    strcpy( s4, s1);

    strcat( s4, s3);

    cout << "After string concatenation: " << s4;

}

void add(int var1, int var2)

{

int ans;

ans = var1 + var2;

cout<< "Addition of Integers :" <<ans <<" ";

}

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