📄 chap22.lst
字号:
}
// This handles int + ob.
CL operator+(int i, CL ob)
{
CL temp;
temp.count = ob.count + i;
return temp;
}
int main()
{
CL obj;
obj = 10;
cout << obj.count << " "; // outputs 10
obj = 10 + obj; // add object to integer
cout << obj.count << " "; // outputs 20
obj = obj + 12; // add integer to object
cout << obj.count; // outputs 32
return 0;
}
listing 15
#include <iostream>
using namespace std;
void swap(int *a, int *b);
int main()
{
int x, y;
x = 99;
y = 88;
cout << x << " " << y << "\n";
swap(&x, &y); // exchange their values
cout << x << " " << y << "\n";
return 0;
}
// C-like, explicit pointer version of swap().
void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
listing 16
void f(int &f)
{
f = rand(); // this modifies the calling argument
}
listing 17
#include <iostream>
using namespace std;
void swap(int &a, int &b); // declare as reference parameters
int main()
{
int x, y;
x = 99;
y = 88;
cout << x << " " << y << "\n";
swap(x, y); // exchange their values
cout << x << " " << y << "\n";
return 0;
}
/* Here, swap() is defined as using call-by-reference,
not call-by-value. */
void swap(int &a, int &b)
{
int t;
t = a;
a = b; // this swaps x
b = t; // this swaps y
}
listing 18
#include <iostream>
using namespace std;
class cl {
int id;
public:
int i;
cl(int i);
~cl();
void neg(cl &o) {o.i = -o.i;}
};
cl::cl(int num)
{
cout << "Constructing " << num << "\n";
id = num;
}
cl::~cl()
{
cout << "Destructing " << id << "\n";
}
int main()
{
cl o(1);
o.i = 10;
o.neg(o);
cout << o.i << "\n";
return 0;
}
listing 19
#include <iostream>
using namespace std;
char &replace(int i); // return a reference
char s[80] = "Hello There";
int main()
{
replace(5) = 'X'; // assign X to space after Hello
cout << s;
return 0;
}
char &replace(int i)
{
return s[i];
}
listing 20
#include <iostream>
using namespace std;
int main()
{
int j, k;
int &i = j; // independent reference to j
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 21
void f() {
int &i = 100;
// ...
}
listing 22
// Overload a unary operator.
three_d three_d::operator++()
{
x++;
y++;
z++;
return *this;
}
listing 23
// THIS WILL NOT WORK
three_d operator++(three_d op1)
{
op1.x++;
op1.y++;
op1.z++;
return op1;
}
listing 24
// This version uses a friend operator++() function.
#include <iostream>
using namespace std;
class three_d {
int x, y, z; // 3-d coordinates
public:
friend three_d operator+(three_d op1, three_d op2);
three_d operator=(three_d op2); // op1 is implied
// use a reference to overload the ++
friend three_d operator++(three_d &op1);
void show() ;
void assign(int mx, int my, int mz);
} ;
// This is now a friend function.
three_d operator+(three_d op1, three_d op2)
{
three_d temp;
temp.x = op1.x + op2.x; // these are integer additions
temp.y = op1.y + op2.y; // and the + retains its original
temp.z = op1.z + op2.z; // meaning relative to them
return temp;
}
// Overload the =.
three_d three_d::operator=(three_d op2)
{
x = op2.x; // these are integer assigments
y = op2.y; // and the = retains its original
z = op2.z; // meaning relative to them
return *this;
}
/* Overload a unary operator using a friend function.
This requires the use of a reference parameter. */
three_d operator++(three_d &op1)
{
op1.x++;
op1.y++;
op1.z++;
return op1;
}
// Show X, Y, Z coordinates.
void three_d::show()
{
cout << x << ", ";
cout << y << ", ";
cout << z << "\n";
}
// Assign coordinates.
void three_d::assign(int mx, int my, int mz)
{
x = mx;
y = my;
z = mz;
}
int main()
{
three_d a, b, c;
a.assign(1, 2, 3);
b.assign(10, 10, 10);
a.show();
b.show();
c = a+b; // now add a and b together
c.show();
c = a+b+c; // add a, b and c together
c.show();
c = b = a; // demonstrate multiple assignment
c.show();
b.show();
++c; // increment c
c.show();
return 0;
}
listing 25
#include <iostream>
using namespace std;
class atype {
int a[3];
public:
atype(int i, int j, int k) {
a[0] = i;
a[1] = j;
a[2] = k;
}
int operator[](int i) { return a[i]; }
};
int main()
{
atype ob(1, 2, 3);
cout << ob[1]; // displays 2
return 0;
}
listing 26
#include <iostream>
using namespace std;
class atype {
int a[3];
public:
atype(int i, int j, int k) {
a[0] = i;
a[1] = j;
a[2] = k;
}
int &operator[](int i) { return a[i]; }
};
int main()
{
atype ob(1, 2, 3);
cout << ob[1]; // displays 2
cout << " ";
ob[1] = 25; // [] on left of =
cout << ob[1]; // now displays 25
return 0;
}
listing 27
// A safe array example.
#include <iostream>
#include <cstdlib>
using namespace std;
class atype {
int a[3];
public:
atype(int i, int j, int k) {
a[0] = i;
a[1] = j;
a[2] = k;
}
int &operator[](int i);
};
// Provide range checking for atype.
int &atype::operator[](int i)
{
if(i<0 || i> 2) {
cout << "Boundary Error\n";
exit(1);
}
return a[i];
}
int main()
{
atype ob(1, 2, 3);
cout << ob[1]; // displays 2
cout << " ";
ob[1] = 25; // [] appears on left
cout << ob[1]; // displays 25
ob[3] = 44; // generates runtime error, 3 out-of-range
return 0;
}
listing 28
#include <iostream>
#include <cstring>
using namespace std;
class str_type {
char string[80];
public:
str_type(char *str = "\0") { strcpy(string, str); }
str_type operator+(str_type str); // concatenate
str_type operator=(str_type str); // assign
// output the string
void show_str() { cout << string; }
} ;
listing 29
// Concatenate two strings.
str_type str_type::operator+(str_type str) {
str_type temp;
strcpy(temp.string, string);
strcat(temp.string, str.string);
return temp;
}
// Assign one string to another.
str_type str_type::operator=(str_type str) {
strcpy(string, str.string);
return *this;
}
listing 30
int main()
{
str_type a("Hello "), b("There"), c;
c = a + b;
c.show_str();
return 0;
}
listing 31
class str_type {
char string[80];
public:
str_type(char *str = "\0") { strcpy(string, str); }
str_type operator+(str_type str); // concatenate objects
str_type operator+(char *str); /* concatenate object with
a string */
str_type operator=(str_type str); /* assign object to
object */
char *operator=(char *str); // assign string to object
void show_str() { cout << string; }
} ;
listing 32
// Assign a string to an object.
str_type str_type::operator=(char *str)
{
str_type temp;
strcpy(string, str);
strcpy(temp.string, string);
return temp;
}
// Add a string to an object.
str_type str_type::operator+(char *str)
{
str_type temp;
strcpy(temp.string, string);
strcat(temp.string, str);
return temp;
}
listing 33
// Expanding the string type.
#include <iostream>
#include <cstring>
using namespace std;
class str_type {
char string[80];
public:
str_type(char *str = "\0") { strcpy(string, str); }
str_type operator+(str_type str);
str_type operator+(char *str);
str_type operator=(str_type str);
str_type operator=(char *str);
void show_str() { cout << string; }
} ;
str_type str_type::operator+(str_type str) {
str_type temp;
strcpy(temp.string, string);
strcat(temp.string, str.string);
return temp;
}
str_type str_type::operator=(str_type str) {
strcpy(string, str.string);
return *this;
}
str_type str_type::operator=(char *str)
{
str_type temp;
strcpy(string, str);
strcpy(temp.string, string);
return temp;
}
str_type str_type::operator+(char *str)
{
str_type temp;
strcpy(temp.string, string);
strcat(temp.string, str);
return temp;
}
int main()
{
str_type a("Hello "), b("There"), c;
c = a + b;
c.show_str();
cout << "\n";
a = "to program in because";
a.show_str();
cout << "\n";
b = c = "C++ is fun";
c = c+" "+a+" "+b;
c.show_str();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -