Professional Documents
Culture Documents
Chapter 8
Chapter 8
#include <iostream>
using namespace std;
int main()
{
int t=10;
return 0;
}
int sqr_it(int x)
{
x = x*x;
return x;
}
listing 2
void swap(int *x, int *y)
{
int temp;
listing 3
#include <iostream>
using namespace std;
int main()
{
int i, j;
i = 10;
j = 20;
return 0;
}
// Exchange arguments.
void swap(int *x, int *y)
{
int temp;
temp = *x; // save the value at address x
*x = *y; // put y into x
*y = temp; // put x into y
}
listing 4
// Using a reference parameter.
#include <iostream>
using namespace std;
int main()
{
int val = 1;
cout << "Old value for val: " << val << '\n';
cout << "New value for val: " << val << '\n';
return 0;
}
listing 5
void f(int &i)
{
i = 10; // this modifies calling argument
}
listing 6
i = 10;
listing 7
f(val); // pass address of val to f()
listing 8
#include <iostream>
using namespace std;
int main()
{
int i, j;
i = 10;
j = 20;
return 0;
}
listing 9
void swap(int& x, int& y);
listing 10
float* p;
listing 11
int* a, b;
listing 12
// Returning a reference.
#include <iostream>
using namespace std;
double &f();
int main()
{
double newval;
return 0;
}
double &f()
{
return val; // return reference to val
}
listing 13
cout << f() << '\n'; // display val's value
listing 14
return val; // return reference to val
listing 15
newval = f(); // assign value of val to newval
listing 16
f() = 99.1; // change val's value
listing 17
#include <iostream>
using namespace std;
int main()
{
int i;
return 0;
}
double &change_it(int i)
{
return vals[i]; // return a reference to the ith element
}
listing 18
// Error, cannot return reference to local var.
int &f()
{
int i=10;
return i;
}
listing 19
#include <iostream>
using namespace std;
int vals[10];
int error = -1;
int main()
{
put(0) = 10; // put values into the array
put(1) = 20;
put(9) = 30;
return 0;
}
listing 20
#include <iostream>
using namespace std;
int main()
{
int j, k;
int &i = j; // independent reference
j = 10;
k = 121;
i = k; // copies k's value into j
// not k's address
listing 21
// Overload a function three times.
#include <iostream>
using namespace std;
int main()
{
f(10); // call f(int)
return 0;
}
void f(int i)
{
cout << "In f(int), i is " << i << '\n';
}
void f(double k)
{
cout << "In f(double), k is " << k << '\n';
}
listing 22
// Create an overloaded abs function.
#include <iostream>
using namespace std;
int main()
{
cout << myabs(-10) << "\n";
return 0;
}
int myabs(int i)
{
cout << "Using integer myabs(): ";
double myabs(double d)
{
cout << "Using double myabs(): ";
long myabs(long l)
{
cout << "Using long myabs(): ";
listing 23
overload myfunc;
listing 24
void myfunc(double num = 0.0, char ch = 'X')
{
.
.
.
}
listing 25
myfunc(198.234, 'A'); // pass explicit values
listing 26
#include <iostream>
using namespace std;
int main()
{
int i;
listing 27
// wrong!
void f(int a = 1, int b);
listing 28
int myfunc(float f, char *str, int i=10, int j);
listing 29
void mystrcat(char *s1, char *s2, int len);
void mystrcat(char *s1, char *s2);
listing 30
// A customized version of strcat().
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str1[80] = "This is a test";
char str2[80] = "0123456789";
return 0;
}
listing 31
int myfunc(double d);
.
.
.
cout << myfunc('c'); // not an error, conversion applied
listing 32
// Overloading ambiguity
#include <iostream>
using namespace std;
int main()
{
// unambiguous, calls myfunc(double)
cout << myfunc(10.1) << " ";
// ambiguous
cout << myfunc(10);
return 0;
}
float myfunc(float i)
{
return i;
}
double myfunc(double i)
{
return -i;
}
listing 33
#include <iostream>
using namespace std;
int main()
{
cout << myfunc('c'); // this calls myfunc(char)
cout << myfunc(88) << " "; // ambiguous
return 0;
}
listing 34
#include <iostream>
using namespace std;
int main()
{
cout << myfunc(4, 5) << " "; // unambiguous
cout << myfunc(10); // ambiguous
return 0;
}
int myfunc(int i)
{
return i;
}