📄 chap22.lst
字号:
listing 1
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class timer{
int seconds;
public:
// seconds specified as a string
timer(char *t) { seconds = atoi(t); }
// seconds specified as integer
timer(int t) { seconds = t; }
// time specified in minutes and seconds
timer(int min, int sec) { seconds = min*60 + sec; }
void run();
} ;
void timer::run()
{
clock_t t1, t2;
t1 = t2 = clock()/CLK_TCK;
while(seconds) {
if(t1/CLK_TCK+1 <= (t2=clock())/CLK_TCK) {
seconds--;
t1 = t2;
}
}
cout << "\a"; // ring the bell
}
int main()
{
timer a(10), b("20"), c(1, 10);
a.run(); // count 10 seconds
b.run(); // count 20 seconds
c.run(); // count 1 minute, 10 seconds
return 0;
}
listing 2
/* Incorrect in C */
void f()
{
int i;
i = 10;
int j;
/* ... */
}
listing 3
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int i;
i = 10;
int j = 100; // perfectly legal in C++
cout << i*j << "\n";
cout << "Enter a string: ";
char str[80]; // also legal in C++
cin >> str;
// display the string in reverse order
int k; // in C++, declare k where it is needed
k = strlen(str);
k--;
while(k>=0) {
cout << str[k];
k--;
}
return 0;
}
listing 4
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class timer{
int seconds;
public:
// seconds specified as a string
timer(char *t) { seconds = atoi(t); }
// seconds specified as integer
timer(int t) { seconds = t; }
// time specified in minutes and seconds
timer(int min, int sec) { seconds = min*60 + sec; }
void run();
} ;
void timer::run()
{
clock_t t1, t2;
t1 = t2 = clock()/CLK_TCK;
while(seconds) {
if(t1/CLK_TCK+1 <= (t2=clock())/CLK_TCK) {
seconds--;
t1 = t2;
}
}
cout << "\a"; // ring the bell
}
int main()
{
timer a(10);
a.run();
cout << "Enter number of seconds: ";
char str[80];
cin >> str;
timer b(str); // initialize at runtime using a string
b.run();
cout << "Enter minutes and seconds: ";
int min, sec;
cin >> min >> sec;
timer c(min, sec); /* initialize at runtime
using minutes and seconds */
c.run();
return 0;
}
listing 5
#include <iostream>
using namespace std;
float myfunc(float i);
double myfunc(double i);
int main()
{
cout << myfunc(10.1) << " "; // unambiguous, calls myfunc(double)
cout << myfunc(10); // ambiguous
return 0;
}
float myfunc(float i)
{
return i;
}
double myfunc(double i)
{
return -i;
}
listing 6
#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) << " "; // ambiguous
return 0;
}
char myfunc(unsigned char ch)
{
return ch-1;
}
char myfunc(char ch)
{
return ch+1;
}
listing 7
#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); // ambiguous
return 0;
}
int myfunc(int i)
{
return i;
}
int myfunc(int i, int j)
{
return i*j;
}
listing 8
#include <iostream>
using namespace std;
int myfunc(int a);
int myfunc(int a, int b);
int main()
{
int (*fp)(int a); // pointer to int xxx(int)
fp = myfunc; // points to myfunc(int)
cout << fp(5);
return 0;
}
int myfunc(int a)
{
return a;
}
int myfunc(int a, int b)
{
return a*b;
}
listing 9
#include <iostream>
using namespace std;
class cl {
int i;
public:
void load_i(int val) { this->i = val; } // same as i = val
int get_i() { return this->i; } // same as return i
} ;
int main()
{
cl o;
o.load_i(100);
cout << o.get_i();
return 0;
}
listing 10
#include <iostream>
using namespace std;
class three_d {
int x, y, z; // 3-d coordinates
public:
three_d operator+(three_d t);
three_d operator=(three_d t);
void show() ;
void assign(int mx, int my, int mz);
} ;
// Overload the +.
three_d three_d::operator+(three_d t)
{
three_d temp;
temp.x = x+t.x;
temp.y = y+t.y;
temp.z = z+t.z;
return temp;
}
// Overload the =.
three_d three_d::operator=(three_d t)
{
x = t.x;
y = t.y;
z = t.z;
return *this;
}
// 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();
return 0;
}
listing 11
#include <iostream>
using namespace std;
class three_d {
int x, y, z; // 3-d coordinates
public:
three_d operator+(three_d op2); // op1 is implied
three_d operator=(three_d op2); // op1 is implied
three_d operator++(); // op1 is also implied here
void show() ;
void assign(int mx, int my, int mz);
} ;
// Overload the +.
three_d three_d::operator+(three_d op2)
{
three_d temp;
temp.x = x+op2.x; // these are integer additions
temp.y = y+op2.y; // and the + retains its original
temp.z = 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.
three_d three_d::operator++()
{
x++;
y++;
z++;
return *this;
}
// 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 12
loc operator++(int x);
listing 13
#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
three_d operator++(); // op1 is implied here, too
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 assignments
y = op2.y; // and the = retains its original
z = op2.z; // meaning relative to them
return *this;
}
// Overload a unary operator.
three_d three_d::operator++()
{
x++;
y++;
z++;
return *this;
}
// 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 14
#include <iostream>
using namespace std;
class CL {
public:
int count;
CL operator=(int i);
friend CL operator+(CL ob, int i);
friend CL operator+(int i, CL ob);
};
CL CL::operator=(int i)
{
count = i;
return *this;
}
// This handles ob + int.
CL operator+(CL ob, int i)
{
CL temp;
temp.count = ob.count + i;
return temp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -