module12.lst
来自「Programs for the book Advanced Engineeri」· LST 代码 · 共 1,141 行 · 第 1/2 页
LST
1,141 行
dQb.put(30.03);
cout << "Contents of double queue dQa: ";
for(int i=0; i < 3; i++)
cout << dQa.get() << " ";
cout << endl;
cout << "Contents of double queue dQb: ";
for(int i=0; i < 3; i++)
cout << dQb.get() << " ";
cout << endl;
return 0;
}
listing 18
// Demonstrate new and delete.
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p;
try {
p = new int; // allocate space for an int
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
return 1;
}
*p = 100;
cout << "At " << p << " ";
cout << "is the value " << *p << "\n";
delete p;
return 0;
}
listing 19
// Initialize memory.
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p;
try {
p = new int (87); // initialize to 87
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
return 1;
}
cout << "At " << p << " ";
cout << "is the value " << *p << "\n";
delete p;
return 0;
}
listing 20
// Allocate an array.
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p, i;
try {
p = new int [10]; // allocate 10 integer array
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
return 1;
}
for(i=0; i<10; i++ )
p[i] = i;
for(i=0; i<10; i++)
cout << p[i] << " ";
delete [] p; // release the array
return 0;
}
listing 21
// Allocate an object.
#include <iostream>
#include <new>
using namespace std;
class Rectangle {
int width;
int height;
public:
Rectangle(int w, int h) {
width = w;
height = h;
cout << "Constructing " << width <<
" by " << height << " rectangle.\n";
}
~Rectangle() {
cout << "Destructing " << width <<
" by " << height << " rectangle.\n";
}
int area() {
return width * height;
}
};
int main()
{
Rectangle *p;
try {
p = new Rectangle(10, 8);
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
return 1;
}
cout << "Area is " << p->area();
cout << "\n";
delete p;
return 0;
}
listing 22
// Allocate an array of objects.
#include <iostream>
#include <new>
using namespace std;
class Rectangle {
int width;
int height;
public:
Rectangle() {
width = height = 0;
cout << "Constructing " << width <<
" by " << height << " rectangle.\n";
}
Rectangle(int w, int h) {
width = w;
height = h;
cout << "Constructing " << width <<
" by " << height << " rectangle.\n";
}
~Rectangle() {
cout << "Destructing " << width <<
" by " << height << " rectangle.\n";
}
void set(int w, int h) {
width = w;
height = h;
}
int area() {
return width * height;
}
};
int main()
{
Rectangle *p;
try {
p = new Rectangle [3];
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
return 1;
}
cout << "\n";
p[0].set(3, 4);
p[1].set(10, 8);
p[2].set(5, 6);
for(int i=0; i < 3; i++)
cout << "Area is " << p[i].area() << endl;
cout << "\n";
delete [] p;
return 0;
}
listing 23
// Demonstrate a namespace.
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 24
// 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 25
namespace NS {
int i;
}
// ...
namespace NS {
int j;
}
listing 26
// 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 27
// Use a static instance variable.
#include <iostream>
using namespace std;
class ShareVar {
static int num;
public:
void setnum(int i) { num = i; };
void shownum() { cout << num << " "; }
};
int ShareVar::num; // define num
int main()
{
ShareVar a, b;
a.shownum(); // prints 0
b.shownum(); // prints 0
a.setnum(10); // set static num to 10
a.shownum(); // prints 10
b.shownum(); // also prints 10
return 0;
}
listing 28
// Refer to static variable through its class name.
#include <iostream>
using namespace std;
class Test {
public:
static int num;
void shownum() { cout << num << endl; }
};
int Test::num; // define num
int main()
{
Test a, b;
// Set num through its class name.
Test::num = 100;
a.shownum(); // prints 100
b.shownum(); // prints 100
// Set num through an object.
a.num = 200;
a.shownum(); // prints 200
b.shownum(); // prints 200
return 0;
}
listing 29
// Demonstrate a static member functions.
#include <iostream>
using namespace std;
class Test {
static int count;
public:
Test() {
count++;
cout << "Constructing object " <<
count << endl;
}
~Test() {
cout << "Destroying object " <<
count << endl;
count--;
}
static int numObjects() { return count; }
};
int Test::count;
int main() {
Test a, b, c;
cout << "There are now " <<
Test::numObjects() <<
" in existence.\n\n";
Test *p = new Test();
cout << "After allocating a Test object, " <<
"there are now " <<
Test::numObjects() <<
" in existence.\n\n";
delete p;
cout << "After deleting an object, " <<
" there are now " <<
a.numObjects() <<
" in existence.\n\n";
return 0;
}
listing 30
// A simple example that uses typeid.
#include <iostream>
#include <typeinfo>
using namespace std;
class MyClass {
// ...
};
int main()
{
int i, j;
float f;
MyClass ob;
cout << "The type of i is: " << typeid(i).name();
cout << endl;
cout << "The type of f is: " << typeid(f).name();
cout << endl;
cout << "The type of ob is: " << typeid(ob).name();
cout << "\n\n";
if(typeid(i) == typeid(j))
cout << "The types of i and j are the same\n";
if(typeid(i) != typeid(f))
cout << "The types of i and f are not the same\n";
return 0;
}
listing 31
// An example that uses typeid on a polymorphic class heirarchy.
#include <iostream>
#include <typeinfo>
using namespace std;
class Base {
virtual void f() {}; // make Base polymorphic
// ...
};
class Derived1: public Base {
// ...
};
class Derived2: public Base {
// ...
};
int main()
{
Base *p, baseob;
Derived1 ob1;
Derived2 ob2;
p = &baseob;
cout << "p is pointing to an object of type ";
cout << typeid(*p).name() << endl;
p = &ob1;
cout << "p is pointing to an object of type ";
cout << typeid(*p).name() << endl;
p = &ob2;
cout << "p is pointing to an object of type ";
cout << typeid(*p).name() << endl;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?