📄 chap26.lst
字号:
listing 1
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p;
try {
p = new int; // allocate memory for int
} catch (bad_alloc xa) {
cout << "Allocation failure.\n";
return 1;
}
*p = 20; // assign that memory the value 20
cout << *p; // prove that it works by displaying value
delete p; // free the memory
return 0;
}
listing 2
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p;
try {
p = new int (99); // initialize with 99
} catch(bad_alloc xa) {
cout << "Allocation failure.\n";
return 1;
}
cout << *p;
delete p;
return 0;
}
listing 3
#include <iostream>
#include <new>
using namespace std;
int main()
{
float *p;
int i;
try {
p = new float [10]; // get a 10-element array
} catch(bad_alloc xa) {
cout << "Allocation failure.\n";
return 1;
}
// assign the values 100 through 109
for(i=0; i<10; i++) p[i] = 100.00 + i;
// display the contents of the array
for(i=0; i<10; i++) cout << p[i] << " ";
delete [] p; // delete the entire array
return 0;
}
listing 4
#include <iostream>
#include <new>
using namespace std;
class three_d {
public:
int x, y, z; // 3-d coordinates
three_d(int a, int b, int c);
~three_d() { cout << "Destructing\n"; }
} ;
three_d::three_d(int a, int b, int c)
{
cout << "Constructing\n";
x = a;
y = b;
z = c;
}
// 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
}
int main()
{
three_d *p;
try {
p = new three_d (5, 6, 7);
} catch(bad_alloc xa) {
cout << "Allocation failure.\n";
return 1;
}
cout << *p;
delete p;
return 0;
}
listing 5
#include <iostream>
#include <new>
using namespace std;
class three_d {
public:
int x, y, z; // 3-d coordinates
three_d(int a, int b, int c) ;
three_d(){ x=y=z=0; cout << "Constructing\n"; } // needed for arrays
~three_d() { cout << "Destructing\n"; }
};
three_d::three_d(int a, int b, int c)
{
cout << "Constructing\n";
x = a;
y = b;
z = c;
}
// 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
}
int main()
{
three_d *p;
int i;
try {
p = new three_d [10];
} catch (bad_alloc xa) {
cout << "Allocation failure.\n";
return 1;
}
for(i=0; i<10; i++) {
p[i].x = 1;
p[i].y = 2;
p[i].z = 3;
}
for(i=0; i<10; i++) cout << *p;
delete [] p;
return 0;
}
listing 6
// Demonstrate nothrow version of new.
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p, i;
p = new(nothrow) int[32]; // use nothrow option
if(!p) {
cout << "Allocation failure.\n";
return 1;
}
for(i=0; i<32; i++) p[i] = i;
for(i=0; i<32; i++) cout << p[i] << " ";
delete [] p; // free the memory
return 0;
}
listing 7
#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class loc {
int longitude, latitude;
public:
loc() {}
loc(int lg, int lt) {
longitude = lg;
latitude = lt;
}
void show() {
cout << longitude << " ";
cout << latitude << "\n";
}
void *operator new(size_t size);
void operator delete(void *p);
};
// new overloaded relative to loc.
void *loc::operator new(size_t size)
{
void *p;
cout << "In overloaded new.\n";
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
// delete overloaded relative to loc.
void loc::operator delete(void *p)
{
cout << "In overloaded delete.\n";
free(p);
}
int main()
{
loc *p1, *p2;
try {
p1 = new loc (10, 20);
} catch (bad_alloc xa) {
cout << "Allocation error for p1.\n";
return 1;
}
try {
p2 = new loc (-10, -20);
} catch (bad_alloc xa) {
cout << "Allocation error for p2.\n";
return 1;;
}
p1->show();
p2->show();
delete p1;
delete p2;
return 0;
}
listing 8
#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class loc {
int longitude, latitude;
public:
loc() {}
loc(int lg, int lt) {
longitude = lg;
latitude = lt;
}
void show() {
cout << longitude << " ";
cout << latitude << "\n";
}
};
// Global new
void *operator new(size_t size)
{
void *p;
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
// Global delete
void operator delete(void *p)
{
free(p);
}
int main()
{
loc *p1, *p2;
float *f;
try {
p1 = new loc (10, 20);
} catch (bad_alloc xa) {
cout << "Allocation error for p1.\n";
return 1;
}
try {
p2 = new loc (-10, -20);
} catch (bad_alloc xa) {
cout << "Allocation error for p2.\n";
return 1;
}
try {
f = new float; // uses overloaded new, too
} catch (bad_alloc xa) {
cout << "Allocation error for f.\n";
return 1;
}
*f = 10.10F;
cout << *f << "\n";
p1->show();
p2->show();
delete p1;
delete p2;
delete f;
return 0;
}
listing 9
#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class loc {
int longitude, latitude;
public:
loc() { longitude = latitude = 0; }
loc(int lg, int lt) {
longitude = lg;
latitude = lt;
}
void show() {
cout << longitude << " ";
cout << latitude << "\n";
}
void *operator new(size_t size);
void operator delete(void *p);
void *operator new[](size_t size);
void operator delete[](void *p);
};
// new overloaded relative to loc.
void *loc::operator new(size_t size)
{
void *p;
cout << "In overloaded new.\n";
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
// delete overloaded relative to loc.
void loc::operator delete(void *p)
{
cout << "In overloaded delete.\n";
free(p);
}
// new overloaded for loc arrays.
void *loc::operator new[](size_t size)
{
void *p;
cout << "Using overload new[].\n";
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
// delete overloaded for loc arrays.
void loc::operator delete[](void *p)
{
cout << "Freeing array using overloaded delete[]\n";
free(p);
}
int main()
{
loc *p1, *p2;
int i;
try {
p1 = new loc (10, 20); // allocate an object
} catch (bad_alloc xa) {
cout << "Allocation error for p1.\n";
return 1;
}
try {
p2 = new loc [10]; // allocate an array
} catch (bad_alloc xa) {
cout << "Allocation error for p2.\n";
return 1;
}
p1->show();
for(i=0; i<10; i++)
p2[i].show();
delete p1; // free an object
delete [] p2; // free an array
return 0;
}
listing 10
#include <iostream>
using namespace std;
class shared {
static int a;
int b;
public:
void set(int i, int j) { a=i; b=j; }
void show();
} ;
int shared::a; // define a
void shared::show()
{
cout << "This is static a: " << a;
cout << "\nThis is non-static b: " << b;
cout << "\n";
}
int main()
{
shared x, y;
x.set(1, 1); // set a to 1
x.show();
y.set(2, 2); // change a to 2
y.show();
x.show(); /* Here, a has been changed for both x and y
because a is shared by both objects. */
return 0;
}
listing 11
#include <iostream>
using namespace std;
enum access_t {shared, in_use, locked, unlocked};
// a scarce resource control class
class access {
static enum access_t acs;
// ...
public:
static void set_access(enum access_t a) { acs = a; }
static enum access_t get_access()
{
return acs;
}
// ...
};
enum access_t access::acs; // define acs
int main()
{
access obj1, obj2;
access::set_access(locked); // call using class name
// ... intervening code
// see if obj2 can access resource
if(obj2.get_access()==unlocked) { // call using object
access::set_access(in_use); // call using class name
cout << "Access resource.\n";
}
else cout << "Locked out.\n";
// ...
return 0;
}
listing 12
// This program contains an error and will not compile.
#include <iostream>
using namespace std;
enum access_t {shared, in_use, locked, unlocked};
// a scarce resource control class
class access {
static enum access_t acs;
int i; // non-static
// ...
public:
static void set_access(enum access_t a) { acs = a; }
static enum access_t get_access()
{
i = 100; // this will not compile
return acs;
}
// ...
};
enum access_t access::acs; // define acs
int main()
{
access obj1, obj2;
access::set_access(locked); // call using class name
// ... intervening code
// see if obj2 can access resource
if(obj2.get_access()==unlocked) { // call using object
access::set_access(in_use); // call using class name
cout << "Access resource.\n";
}
else cout << "Locked out.\n";
// ...
return 0;
}
listing 13
// This program contains an error and will not compile.
#include <iostream>
using namespace std;
class base {
public:
int i;
};
// d1 inherits base.
class d1 : public base {
public:
int j;
};
// d2 inherits base.
class d2 : public base {
public:
int k;
};
/* d3 inherits both d1 and d2. This means that there
are two copies of base in d3! */
class d3 : public d1, public d2 {
public:
int m;
};
int main()
{
d3 d;
d.i = 10; // this is ambiguous, which i???
d.j = 20;
d.k = 30;
d.m = 40;
// also ambiguous, which i???
cout << d.i << " ";
cout << d.j << " " << d.k << " ";
cout << d.m;
return 0;
}
listing 14
d.i = 20;
listing 15
#include <iostream>
using namespace std;
class base {
public:
int i;
};
// d1 inherits base.
class d1 : public base {
public:
int j;
};
// d2 inherits base.
class d2 : public base {
public:
int k;
};
/* d3 inherits both d1 and d2. This means that there
are two copies of base in d3! */
class d3 : public d1, public d2 {
public:
int m;
};
int main()
{
d3 d;
d.d2::i = 10; // scope resolved, using d2's i
d.j = 20;
d.k = 30;
d.m = 40;
// scope resolved, using d2's i
cout << d.d2::i << " ";
cout << d.j << " " << d.k << " ";
cout << d.m;
return 0;
}
listing 16
#include <iostream>
using namespace std;
class base {
public:
int i;
};
// d1 inherits base as virtual.
class d1 : virtual public base {
public:
int j;
};
// d2 inherits base as virtual.
class d2 : virtual public base {
public:
int k;
};
/* d3 inherits both d1 and d2. However, now there is
only one copy of base in d3. */
class d3 : public d1, public d2 {
public:
int m;
};
int main()
{
d3 d;
d.i = 10; // no longer ambiguous
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -