📄 module6.lst
字号:
listing 1
// Changing a call-by-value parameter does not affect the argument.
#include <iostream>
using namespace std;
double reciprocal(double x);
int main()
{
double t = 10.0;
cout << "Reciprocal is of 10.0 is " << reciprocal(t) << "\n";
cout << "Value of t is still: " << t << "\n";
return 0;
}
// Return the recirprocal of a value.
double reciprocal(double x)
{
x = 1 / x; // create reciprocal
return x;
}
listing 2
// Exchange the values of the variables pointed to by x and y.
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 3
// Demonstrate the pointer version of swap().
#include <iostream>
using namespace std;
// Declare swap() using pointers.
void swap(int *x, int *y);
int main()
{
int i, j;
i = 10;
j = 20;
cout << "Initial values of i and j: ";
cout << i << ' ' << j << '\n';
swap(&j, &i); // call swap() with addresses of i and j
cout << "Swapped values of i and j: ";
cout << i << ' ' << j << '\n';
return 0;
}
// Exchange values pointed to by x and y.
void swap(int *x, int *y)
{
int temp;
temp = *x; // save the value at x
*x = *y; // put value at y into the variable at x
*y = temp; // put value at x into the variable at y
}
listing 4
// Using a reference parameter.
#include <iostream>
using namespace std;
void f(int &i); // here, i is a reference parameter
int main()
{
int val = 1;
cout << "Old value for val: " << val << '\n';
f(val); // pass address of val to f()
cout << "New value for val: " << val << '\n';
return 0;
}
void f(int &i)
{
i = 10; // this modifies calling argument
}
listing 5
// Use reference parameters to create the swap() function.
#include <iostream>
using namespace std;
// Declare swap() using reference parameters.
void swap(int &x, int &y);
int main()
{
int i, j;
i = 10;
j = 20;
cout << "Initial values of i and j: ";
cout << i << ' ' << j << '\n';
swap(j, i);
cout << "Swapped values of i and j: ";
cout << i << ' ' << j << '\n';
return 0;
}
/* Here, swap() is defined as using call-by-reference,
not call-by-value. Thus, it can exchange the two
arguments it is called with.
*/
void swap(int &x, int &y)
{
int temp;
// use references to exchange the values of the arguments
temp = x;
x = y;
y = temp;
}
listing 6
// Returning a reference.
#include <iostream>
using namespace std;
double &f(); // return a reference.
double val = 100.0;
int main()
{
double x;
cout << f() << '\n'; // display val's value
x = f(); // assign value of val to x
cout << x << '\n'; // display x's value
f() = 99.1; // change val's value
cout << f() << '\n'; // display val's new value
return 0;
}
// This function returns a reference to a double.
double &f()
{
return val; // return reference to val
}
listing 7
// Return a reference to an array element.
#include <iostream>
using namespace std;
double &change_it(int i); // return a reference
double vals[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
int main()
{
int i;
cout << "Here are the original values: ";
for(i=0; i < 5; i++)
cout << vals[i] << ' ';
cout << '\n';
change_it(1) = 5298.23; // change 2nd element
change_it(3) = -98.8; // change 4th element
cout << "Here are the changed values: ";
for(i=0; i < 5; i++)
cout << vals[i] << ' ';
cout << '\n';
return 0;
}
double &change_it(int i)
{
return vals[i]; // return a reference to the ith element
}
listing 8
// Error, cannot return reference to local var.
int &f()
{
int i = 10;
return i;
}
listing 9
// Use an independent reference.
#include <iostream>
using namespace std;
int main()
{
int j, k;
int &i = j; // independent reference
j = 10;
cout << j << " " << i; // outputs 10 10
k = 121;
i = k; // copies k's value into j, not k's address
cout << "\n" << j; // outputs 121
return 0;
}
listing 10
// Overload a function three times.
#include <iostream>
using namespace std;
void f(int i); // integer parameter
void f(int i, int j); // two integer parameters
void f(double k); // one double parameter
int main()
{
f(10); // call f(int)
f(10, 20); // call f(int, int)
f(12.23); // call f(double)
return 0;
}
void f(int i)
{
cout << "In f(int), i is " << i << '\n';
}
void f(int i, int j)
{
cout << "In f(int, int), i is " << i;
cout << ", j is " << j << '\n';
}
void f(double k)
{
cout << "In f(double), k is " << k << '\n';
}
listing 11
// Create various version of the neg() function.
#include <iostream>
using namespace std;
int neg(int n); // neg() for int.
double neg(double n); // neg() for double.
long neg(long n); // neg() for long.
int main()
{
cout << "neg(-10): " << neg(-10) << "\n";
cout << "neg(9L): " << neg(9L) << "\n";
cout << "neg(11.23): " << neg(11.23) << "\n";
return 0;
}
// neg()for int.
int neg(int n)
{
return -n;
}
// neg()for double.
double neg(double n)
{
return -n;
}
// neg()for long.
long neg(long n)
{
return -n;
}
listing 12
// Create various versions of min().
#include <iostream>
using namespace std;
int min(int a, int b); // min() for ints
char min(char a, char b); // min() for chars
int * min(int *a, int *b); // min() for int pointers
int main()
{
int i=10, j=22;
cout << "min('X', 'a'): " << min('X', 'a') << "\n";
cout << "min(9, 3): " << min(9, 3) << "\n";
cout << "*min(&i, &j): " << *min(&i, &i) << "\n";
return 0;
}
// min() for ints. Return the smallest value.
int min(int a, int b)
{
if(a < b) return a;
else return b;
}
// min() for chars -- ignore case.
char min(char a, char b)
{
if(tolower(a) < tolower(b)) return a;
else return b;
}
/*
min() for int pointers.
Compare values and return pointer to smallest value.
*/
int * min(int *a, int *b)
{
if(*a < *b) return a;
else return b;
}
listing 13
/*
Automatic type conversions can affect
overloaded function resolution.
*/
#include <iostream>
using namespace std;
void f(int x);
void f(double x);
int main() {
int i = 10;
double d = 10.1;
short s = 99;
float r = 11.5F;
f(i); // calls f(int)
f(d); // calls f(double)
f(s); // calls f(int) -- type conversion
f(r); // calls f(double) -- type conversion
return 0;
}
void f(int x) {
cout << "Inside f(int): " << x << "\n";
}
void f(double x) {
cout << "Inside f(double): " << x << "\n";
}
listing 14
// Now, add f(short).
#include <iostream>
using namespace std;
void f(int x);
void f(short x);
void f(double x);
int main() {
int i = 10;
double d = 10.1;
short s = 99;
float r = 11.5F;
f(i); // calls f(int)
f(d); // calls f(double)
f(s); // now calls f(short)
f(r); // calls f(double) -- type conversion
return 0;
}
void f(int x) {
cout << "Inside f(int): " << x << "\n";
}
void f(short x) {
cout << "Inside f(short): " << x << "\n";
}
void f(double x) {
cout << "Inside f(double): " << x << "\n";
}
listing 15
/*
Project 6-1
Create overloaded print() and println() functions
that display various types of data.
*/
#include <iostream>
using namespace std;
// These output a newline.
void println(bool b);
void println(int i);
void println(long i);
void println(char ch);
void println(char *str);
void println(double d);
// These functions do not output a newline.
void print(bool b);
void print(int i);
void print(long i);
void print(char ch);
void print(char *str);
void print(double d);
int main()
{
println(true);
println(10);
println("This is a test");
println('x');
println(99L);
println(123.23);
print("Here are some values: ");
print(false);
print(' ');
print(88);
print(' ');
print(100000L);
print(' ');
print(100.01);
println(" Done!");
return 0;
}
// Here are the println() functions.
void println(bool b)
{
if(b) cout << "true\n";
else cout << "false\n";
}
void println(int i)
{
cout << i << "\n";
}
void println(long i)
{
cout << i << "\n";
}
void println(char ch)
{
cout << ch << "\n";
}
void println(char *str)
{
cout << str << "\n";
}
void println(double d)
{
cout << d << "\n";
}
// Here are the print() functions.
void print(bool b)
{
if(b) cout << "true";
else cout << "false";
}
void print(int i)
{
cout << i;
}
void print(long i)
{
cout << i;
}
void print(char ch)
{
cout << ch;
}
void print(char *str)
{
cout << str;
}
void print(double d)
{
cout << d;
}
listing 16
// Demonstrate default arguments.
#include <iostream>
using namespace std;
void myfunc(int x = 0, int y = 100);
int main()
{
myfunc(1, 2);
myfunc(10);
myfunc();
return 0;
}
void myfunc(int x, int y)
{
cout << "x: " << x << ", y: " << y << "\n";
}
listing 17
// A customized version of strcat().
#include <iostream>
#include <cstring>
using namespace std;
void mystrcat(char *s1, char *s2, int len = 0);
int main()
{
char str1[80] = "This is a test";
char str2[80] = "0123456789";
mystrcat(str1, str2, 5); // concatenate 5 chars
cout << str1 << '\n';
strcpy(str1, "this is a test"); // reset str1
mystrcat(str1, str2); // concatenate entire string
cout << str1 << '\n';
return 0;
}
// A custom version of strcat().
void mystrcat(char *s1, char *s2, int len)
{
// find end of s1
while(*s1) s1++;
if(len==0) len = strlen(s2);
while(*s2 && len) {
*s1 = *s2; // copy chars
s1++;
s2++;
len--;
}
*s1 = '\0'; // null terminate s1
}
listing 18
// Overloading ambiguity.
#include <iostream>
using namespace std;
float myfunc(float i);
double myfunc(double i);
int main()
{
// unambiguous, calls myfunc(double)
cout << myfunc(10.1) << " ";
// ambiguous
cout << myfunc(10); // Error!
return 0;
}
float myfunc(float i)
{
return i;
}
double myfunc(double i)
{
return -i;
}
listing 19
// Another example of overloading ambiguity.
#include <iostream>
using namespace std;
char myfunc(unsigned char ch);
char myfunc(char ch);
int main()
{
cout << myfunc('c'); // this calls myfunc(char)
cout << myfunc(88) << " "; // Error, ambiguous!
return 0;
}
char myfunc(unsigned char ch)
{
return ch-1;
}
char myfunc(char ch)
{
return ch+1;
}
listing 20
// More function overloading abiguity.
#include <iostream>
using namespace std;
int myfunc(int i);
int myfunc(int i, int j=1);
int main()
{
cout << myfunc(4, 5) << " "; // unambiguous
cout << myfunc(10); // Error, ambiguous!
return 0;
}
int myfunc(int i)
{
return i;
}
int myfunc(int i, int j)
{
return i*j;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -