ch1-Print

Chapter_1     Square Hex







Print.cpp     TCP, p. 5


#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
./Print
int: 0
double: 0.1
string: "Hello!"
int: 0, double: 1
double: 0.1, int: 0
*/









Chapter_1     Square BACK_TO_TOP Hex



Comments

Popular posts from this blog

Contents