LogFAQs > #895527608

LurkerFAQs, Active DB, DB1, Database 2 ( 09.16.2017-02.21.2018 ), DB3, DB4, DB5, DB6, DB7, DB8, DB9, DB10, DB11, DB12, Clear
Topic List
Page List: 1
TopicC++ class Blog topic.
LookANinja
02/05/18 6:32:03 PM
#35:


My solution for problem 1.

//Using an overloaded function for different types of inputs and outputs
#include<iostream>
#include<string>
using namespace std;

int add(int x,int y)
{
return x+y;
}
float add(float f,float g)//function with same name has different parameters
{
return f+g;
}
string add(string s1, string s2)
{
return s1+s2;
}
int main()
{
int x,y;
float f,g;
string s1,s2;
cout<<"Enter two integers"<<endl;
cin>>x>>y;
cout<<add(x,y)<<endl;
cout<<"Enter two floats"<<endl;
cin>>f>>g;
cout<<add(f,g)<<endl;//function of same name with different output types
cout<<"Enter two strings";
cin>>s1>>s2;
cout<<add(s1,s2);
}

And heres problem 2

//Creating function template for add to find the sum of two ints, floats, and strings
#include<iostream>
#include<string>
using namespace std;

template<typename T>
T add(T x,T y)//template parameter list
{
return x+y;
}

int main()
{

int x=3,y=4;
float f1=3.2,f2=3.3;
string st1="duck",st2="bill";
add(x,y);//function is called
cout<<x<<" + "<<y<<" = "<<add(x,y)<<endl;//test values are displayed along with function
add(f1,f2);
cout<<f1<<" + "<<f2<<" = "<<add(f1,f2)<<endl;
add(st1,st2);
cout<<st1<<" + "<<st2<<" = "<<add(st1,st2);

}

thoughts anyone?
---
... Copied to Clipboard!
Topic List
Page List: 1