#include <iostream>
using std::cout;
using std::endl;
double square(double x); // return square of `x'
void print_square(double x);
int main()
{
print_square(1.234);
return 0;
}
// square a double-precision floating-point number
double square(double x)
{
return x*x;
}
void print_square(double x)
{
cout << "The square of " << x << " is " << square(x) << endl;
}
/*
g++ Square.cpp -o Square
./Square
The square of 1.234 is 1.52276
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <iostream>
using std::cout;
using std::endl;
using std::string;
// overloaded print functions:
void print(int); // takes an integer argument
void print(double); // takes a floating-point argument
void print(string); // takes a string argument
void print(int, double); // takes two arguments: int, double
void print(double, int); // takes two arguments: double, int
int main()
{
print(0); // calls print(int)
print(0.1); // calls print(double)
print("Hello!"); // calls print(string)
// print(0, 0); // compile error: ambiguous call
print(0, 1.0); // calls print(int, double)
print(0.1, 0); // calls print(double, int)
return 0;
}
void print(int i)
{
cout << "int: " << i << endl;
}
void print(double d)
{
cout << "double: " << d << endl;
}
void print(string s)
{
cout << "string: \"" << s << '\"' << endl; // '"' or "\""
}
void print(int i, double d)
{
cout << "int: " << i << ", double: " << d << endl;
}
void print(double d, int i)
{
cout << "double: " << d << ", int: " << i << endl;
}
/*
g++ Print.cpp -o Print
int: 0
double: 0.1
string: "Hello!"
int: 0, double: 1
double: 0.1, int: 0
*/