📄 chap26.lst
字号:
d.j = 20;
d.k = 30;
d.m = 40;
cout << d.i << " "; // no longer ambiguous
cout << d.j << " " << d.k << " ";
cout << d.m;
return 0;
}
listing 17
// define a class of type d1
d1 myclass;
myclass.i = 100;
listing 18
class X {
int some_var;
public:
int f1() const; // const member function
};
listing 19
/*
Demonstrate const member functions.
This program won't compile.
*/
#include <iostream>
using namespace std;
class Demo {
int i;
public:
int geti() const {
return i; // ok
}
void seti(int x) const {
i = x; // error!
}
};
int main()
{
Demo ob;
ob.seti(1900);
cout << ob.geti();
return 0;
}
listing 20
// Demonstrate mutable.
#include <iostream>
using namespace std;
class Demo {
mutable int i;
int j;
public:
int geti() const {
return i; // ok
}
void seti(int x) const {
i = x; // now, OK.
}
/* The following function won't compile.
void setj(int x) const {
j = x; // Still Wrong!
}
*/
};
int main()
{
Demo ob;
ob.seti(1900);
cout << ob.geti();
return 0;
}
listing 21
class X {
public:
void f2(int a) volatile; // volatile member function
};
listing 22
#include <iostream>
using namespace std;
extern "C" void myCfunc(void);
int main()
{
myCfunc();
return 0;
}
// This will link as a C function.
void myCfunc(void)
{
cout << "This links as a C function.\n";
}
listing 23
#include <iostream>
using namespace std;
class myclass {
public:
int sum;
void myclass::sum_it(int x);
};
void myclass::sum_it(int x) {
int i;
sum = 0;
for(i=x; i; i--) sum += i;
}
int main()
{
int myclass::*dp; // pointer to an integer class member
void (myclass::*fp)(int x); // pointer to member function
myclass c;
dp = &myclass::sum; // get address of data
fp = &myclass::sum_it; // get address of function
(c.*fp)(7); // compute summation of 7
cout << "summation of 7 is " << c.*dp;
return 0;
}
listing 24
#include <iostream>
using namespace std;
class myclass {
public:
int sum;
void myclass::sum_it(int x);
};
void myclass::sum_it(int x) {
int i;
sum = 0;
for(i=x; i; i--) sum += i;
}
int main()
{
int myclass::*dp; // pointer to an integer class member
void (myclass::*fp)(int x); // pointer to member function
myclass *c, d; // c is now a pointer to an object
c = &d; // give c the address of an object
dp = &myclass::sum; // get address of data
fp = &myclass::sum_it; // get address of function
(c->*fp)(7); // now, use ->* to call function
cout << "summation of 7 is " << c->*dp; // use ->*
return 0;
}
listing 25
operator int() { return x * y * z; }
listing 26
#include <iostream>
using namespace std;
class three_d {
int x, y, z; // 3-d coordinates
public:
three_d(int a, int b, int c) { x=a; y=b, z=c; }
three_d operator+(three_d op2) ;
friend ostream &operator<<(ostream &stream, three_d &obj);
operator int() { return x*y*z; }
} ;
// Display X, Y, Z coordinates - three_d inserter.
ostream &operator<<(ostream &stream, three_d &obj)
{
stream << obj.x << ", ";
stream << obj.y << ", ";
stream << obj.z << "\n";
return stream; // return the stream
}
three_d three_d::operator+(three_d op2)
{
three_d temp(0, 0, 0);
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;
}
int main()
{
three_d a(1, 2, 3), b(2, 3, 4), c(0, 0, 0);
cout << a << b;
cout << b+100; // displays 124 because of conversion to int
cout << "\n";
c = a+b; // adds two objects
cout << c;
return 0;
}
listing 27
myclass x = y; // y explicitly initializating x
func(y); // y passed as a parameter
y = func(); // y receiving a temporary, return object
listing 28
/* This program creates a "safe" array class. Since space
for the array is allocated using new, a copy constructor
is provided to allocate memory when one array object is
used to initialize another.
*/
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
class array {
int *p;
int size;
public:
array(int sz) {
try {
p = new int[sz];
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
exit(EXIT_FAILURE);
}
size = sz;
}
~array() { delete [] p; }
// copy constructor
array(const array &a);
void put(int i, int j) {
if(i>=0 && i<size) p[i] = j;
}
int get(int i) {
return p[i];
}
};
// Copy Constructor
array::array(const array &a) {
int i;
try {
p = new int[a.size];
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
exit(EXIT_FAILURE);
}
for(i=0; i<a.size; i++) p[i] = a.p[i];
}
int main()
{
array num(10);
int i;
for(i=0; i<10; i++) num.put(i, i);
for(i=9; i>=0; i--) cout << num.get(i);
cout << "\n";
// create another array and initialize with num
array x(num); // invokes copy constructor
for(i=0; i<10; i++) cout << x.get(i);
return 0;
}
listing 29
array a(10);
// ...
array b(10);
b = a; // does not call copy constructor
listing 30
class base {
public:
int j; // public in base
};
// Inherit base as private.
class derived: private base {
public:
// here is access declaration
base::j; // make j public again
.
.
.
};
listing 31
base::j;
listing 32
#include <iostream>
using namespace std;
class base {
int i; // private to base
public:
int j, k;
void seti(int x) { i = x; }
int geti() { return i; }
};
// Inherit base as private.
class derived: private base {
public:
/* The next three statements override
base's inheritance as private and restore j,
seti(), and geti() to public access. */
base::j; // make j public again - but not k
base::seti; // make seti() public
base::geti; // make geti() public
// base::i; // illegal, you cannot elevate access
int a; // public
};
int main()
{
derived ob;
//ob.i = 10; // illegal because i is private in derived
ob.j = 20; // legal because j is made public in derived
//ob.k = 30; // illegal because k is private in derived
ob.a = 40; // legal because a is public in derived
ob.seti(10);
cout << ob.geti() << " " << ob.j << " " << ob.a;
return 0;
}
listing 33
namespace CounterNameSpace {
int upperbound;
int lowerbound;
class counter {
int count;
public:
counter(int n) {
if(n <= upperbound) count = n;
else count = upperbound;
}
void reset(int n) {
if(n <= upperbound) count = n;
}
int run() {
if(count > lowerbound) return count--;
else return lowerbound;
}
};
}
listing 34
if(count > lowerbound) return count--;
listing 35
CounterNameSpace::upperbound = 10;
listing 36
CounterNameSpace::counter ob;
listing 37
// Demonstrate a namespace.
#include <iostream>
using namespace std;
namespace CounterNameSpace {
int upperbound;
int lowerbound;
class counter {
int count;
public:
counter(int n) {
if(n <= upperbound) count = n;
else count = upperbound;
}
void reset(int n) {
if(n <= upperbound) count = n;
}
int run() {
if(count > lowerbound) return count--;
else return lowerbound;
}
};
}
int main()
{
CounterNameSpace::upperbound = 100;
CounterNameSpace::lowerbound = 0;
CounterNameSpace::counter ob1(10);
int i;
do {
i = ob1.run();
cout << i << " ";
} while(i > CounterNameSpace::lowerbound);
cout << endl;
CounterNameSpace::counter ob2(20);
do {
i = ob2.run();
cout << i << " ";
} while(i > CounterNameSpace::lowerbound);
cout << endl;
ob2.reset(100);
CounterNameSpace::lowerbound = 90;
do {
i = ob2.run();
cout << i << " ";
} while(i > CounterNameSpace::lowerbound);
return 0;
}
listing 38
using CounterNameSpace::lowerbound; // only lowerbound is visible
lowerbound = 10; // OK because lowerbound is visible
using namespace CounterNameSpace; // all members are visible
upperbound = 100; // OK because all members are now visible
listing 39
// Demonstrate using.
#include <iostream>
using namespace std;
namespace CounterNameSpace {
int upperbound;
int lowerbound;
class counter {
int count;
public:
counter(int n) {
if(n <= upperbound) count = n;
else count = upperbound;
}
void reset(int n) {
if(n <= upperbound) count = n;
}
int run() {
if(count > lowerbound) return count--;
else return lowerbound;
}
};
}
int main()
{
// use only upperbound from CounterNameSpace
using CounterNameSpace::upperbound;
// now, no qualification needed to set upperbound
upperbound = 100;
// qualification still needed for lowerbound, etc.
CounterNameSpace::lowerbound = 0;
CounterNameSpace::counter ob1(10);
int i;
do {
i = ob1.run();
cout << i << " ";
} while(i > CounterNameSpace::lowerbound);
cout << endl;
// now, use entire CounterNameSpace
using namespace CounterNameSpace;
counter ob2(20);
do {
i = ob2.run();
cout << i << " ";
} while(i > lowerbound);
cout << endl;
ob2.reset(100);
lowerbound = 90;
do {
i = ob2.run();
cout << i << " ";
} while(i > lowerbound);
return 0;
}
listing 40
#include <iostream>
using namespace std;
namespace NS {
int i;
}
// ...
namespace NS {
int j;
}
int main()
{
NS::i = NS::j = 10;
// refer to NS specifically
cout << NS::i * NS::j << "\n";
// use NS namespace
using namespace NS;
cout << i * j;
return 0;
}
listing 41
#include <iostream>
using namespace std;
namespace NS1 {
int i;
namespace NS2 { // a nested namespace
int j;
}
}
int main()
{
NS1::i = 19;
// NS2::j = 10; Error, NS2 is not in view
NS1::NS2::j = 10; // this is right
cout << NS1::i << " "<< NS1::NS2::j << "\n";
// use NS1
using namespace NS1;
/* Now that NS1 is in view, NS2 can be used to
refer to j. */
cout << i * NS2::j;
return 0;
}
listing 42
using namespace NS1;
listing 43
using namespace std;
listing 44
// Use explicit std:: qualification.
#include <iostream>
int main()
{
int val;
std::cout << "Enter a number: ";
std::cin >> val;
std::cout << "This is your number: ";
std::cout << std::hex << val;
return 0;
}
listing 45
// Bring only a few names into the global namespace.
#include <iostream>
// gain access to cout, cin, and hex
using std::cout;
using std::cin;
using std::hex;
int main()
{
int val;
cout << "Enter a number: ";
cin >> val;
cout << "This is your number: ";
cout << hex << val;
return 0;
}
listing 46
class MyClass {
int i;
public:
MyClass(int j) {i = j;}
// ...
};
listing 47
MyClass ob1(1);
MyClass ob2 = 10;
listing 48
MyClass ob2 = 10;
listing 49
MyClass ob2(10);
listing 50
class MyClass {
int i;
public:
explicit MyClass(int j) {i = j;}
// ...
};
listing 51
MyClass ob(110);
listing 52
template <typename X> void swapargs(X &a, X &b)
{
X temp;
temp = a;
a = b;
b = temp;
}
listing 53
typename X::Name someObject;
listing 54
int f();
listing 55
int f();
int f(void);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -