📄 module12.lst
字号:
listing 1
// A simple exception handling example.
#include <iostream>
using namespace std;
int main()
{
cout << "start\n";
try { // start a try block
cout << "Inside try block\n";
throw 99; // throw an error
cout << "This will not execute";
}
catch (int i) { // catch an error
cout << "Caught an exception -- value is: ";
cout << i << "\n";
}
cout << "end";
return 0;
}
listing 2
// This example will not work.
#include <iostream>
using namespace std;
int main()
{
cout << "start\n";
try { // start a try block
cout << "Inside try block\n";
throw 99; // throw an error
cout << "This will not execute";
}
catch (double i) { // won't work for an int exception
cout << "Caught an exception -- value is: ";
cout << i << "\n";
}
cout << "end";
return 0;
}
listing 3
/* Throwing an exception from a function called
from within a try block. */
#include <iostream>
using namespace std;
void Xtest(int test)
{
cout << "Inside Xtest, test is: " << test << "\n";
if(test) throw test;
}
int main()
{
cout << "start\n";
try { // start a try block
cout << "Inside try block\n";
Xtest(0);
Xtest(1);
Xtest(2);
}
catch (int i) { // catch an error
cout << "Caught an exception -- value is: ";
cout << i << "\n";
}
cout << "end";
return 0;
}
listing 4
// A try block can be localized to a function.
#include <iostream>
using namespace std;
// A try/catch is reset each time a function is entered.
void Xhandler(int test)
{
try{
if(test) throw test;
}
catch(int i) {
cout << "Caught One! Ex. #: " << i << '\n';
}
}
int main()
{
cout << "start\n";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "end";
return 0;
}
listing 5
// Use multiple catch statements.
#include <iostream>
using namespace std;
// Different types of exceptions can be caught.
void Xhandler(int test)
{
try{
if(test) throw test; // throw int
else throw "Value is zero"; // throw char *
}
catch(int i) {
cout << "Caught One! Ex. #: " << i << '\n';
}
catch(char *str) {
cout << "Caught a string: ";
cout << str << '\n';
}
}
int main()
{
cout << "start\n";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "end";
return 0;
}
listing 6
// Catching derived classes. This program is wrong!
#include <iostream>
using namespace std;
class B {
};
class D: public B {
};
int main()
{
D derived;
try {
throw derived;
}
catch(B b) {
cout << "Caught a base class.\n";
}
catch(D d) {
cout << "This won't execute.\n";
}
return 0;
}
listing 7
// This example catches all exceptions.
#include <iostream>
using namespace std;
void Xhandler(int test)
{
try{
if(test==0) throw test; // throw int
if(test==1) throw 'a'; // throw char
if(test==2) throw 123.23; // throw double
}
catch(...) { // catch all exceptions
cout << "Caught One!\n";
}
}
int main()
{
cout << "start\n";
Xhandler(0);
Xhandler(1);
Xhandler(2);
cout << "end";
return 0;
}
listing 8
// Restricting function throw types.
#include <iostream>
using namespace std;
// This function can only throw ints, chars, and doubles.
void Xhandler(int test) throw(int, char, double)
{
if(test==0) throw test; // throw int
if(test==1) throw 'a'; // throw char
if(test==2) throw 123.23; // throw double
}
int main()
{
cout << "start\n";
try{
Xhandler(0); // also, try passing 1 and 2 to Xhandler()
}
catch(int i) {
cout << "Caught int\n";
}
catch(char c) {
cout << "Caught char\n";
}
catch(double d) {
cout << "Caught double\n";
}
cout << "end";
return 0;
}
listing 9
// Example of "rethrowing" an exception.
#include <iostream>
using namespace std;
void Xhandler()
{
try {
throw "hello"; // throw a char *
}
catch(char *) { // catch a char *
cout << "Caught char * inside Xhandler\n";
throw ; // rethrow char * out of function
}
}
int main()
{
cout << "start\n";
try{
Xhandler();
}
catch(char *) {
cout << "Caught char * inside main\n";
}
cout << "end";
return 0;
}
listing 10
// Function template example.
#include <iostream>
using namespace std;
// This is a function template.
template <class X> void swapargs(X &a, X &b)
{
X temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int i=10, j=20;
float x=10.1, y=23.3;
char a='x', b='z';
cout << "Original i, j: " << i << ' ' << j << '\n';
cout << "Original x, y: " << x << ' ' << y << '\n';
cout << "Original a, b: " << a << ' ' << b << '\n';
swapargs(i, j); // swap integers
swapargs(x, y); // swap floats
swapargs(a, b); // swap chars
cout << "Swapped i, j: " << i << ' ' << j << '\n';
cout << "Swapped x, y: " << x << ' ' << y << '\n';
cout << "Swapped a, b: " << a << ' ' << b << '\n';
return 0;
}
listing 11
#include <iostream>
using namespace std;
template <class Type1, class Type2>
void myfunc(Type1 x, Type2 y)
{
cout << x << ' ' << y << '\n';
}
int main()
{
myfunc(10, "hi");
myfunc(0.23, 10L);
return 0;
}
listing 12
// Overriding a template function.
#include <iostream>
using namespace std;
template <class X> void swapargs(X &a, X &b)
{
X temp;
temp = a;
a = b;
b = temp;
cout << "Inside template swapargs.\n";
}
// This overrides the generic version of swapargs() for ints.
void swapargs(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
cout << "Inside swapargs int specialization.\n";
}
int main()
{
int i=10, j=20;
float x=10.1, y=23.3;
char a='x', b='z';
cout << "Original i, j: " << i << ' ' << j << '\n';
cout << "Original x, y: " << x << ' ' << y << '\n';
cout << "Original a, b: " << a << ' ' << b << '\n';
swapargs(i, j); // calls explicitly overloaded swapargs()
swapargs(x, y); // calls generic swapargs()
swapargs(a, b); // calls generic swapargs()
cout << "Swapped i, j: " << i << ' ' << j << '\n';
cout << "Swapped x, y: " << x << ' ' << y << '\n';
cout << "Swapped a, b: " << a << ' ' << b << '\n';
return 0;
}
listing 13
// Use the newer-style specialization syntax.
template<> void swapargs<int>(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
cout << "Inside swapargs int specialization.\n";
}
listing 14
// A simple generic class.
#include <iostream>
using namespace std;
template <class T> class MyClass {
T x, y;
public:
MyClass(T a, T b) {
x = a;
y = b;
}
T div() { return x/y; }
};
int main()
{
// Create a version of MyClass for doubles.
MyClass<double> d_ob(10.0, 3.0 );
cout << "double division: " << d_ob.div() << "\n";
// Create a version of MyClass for ints.
MyClass<int> i_ob(10, 3);
cout << "integer division: " << i_ob.div() << "\n";
return 0;
}
listing 15
/* This example uses two generic data types in a
class definition. */
#include <iostream>
using namespace std;
template <class T1, class T2> class MyClass
{
T1 i;
T2 j;
public:
MyClass(T1 a, T2 b) { i = a; j = b; }
void show() { cout << i << ' ' << j << '\n'; }
};
int main()
{
MyClass<int, double> ob1(10, 0.23);
MyClass<char, char *> ob2('X', "This is a test");
ob1.show(); // show int, double
ob2.show(); // show char, char *
return 0;
}
listing 16
// Demonstrate class specialization.
#include <iostream>
using namespace std;
template <class T> class MyClass {
T x;
public:
MyClass(T a) {
cout << "Inside generic MyClass\n";
x = a;
}
T getx() { return x; }
};
// Explicit specialization for int.
template <> class MyClass<int> {
int x;
public:
MyClass(int a) {
cout << "Inside MyClass<int> specialization\n";
x = a * a;
}
int getx() { return x; }
};
int main()
{
MyClass<double> d(10.1);
cout << "double: " << d.getx() << "\n\n";
MyClass<int> i(5);
cout << "int: " << i.getx() << "\n";
return 0;
}
listing 17
/*
Project 12-1
A template queue class.
*/
#include <iostream>
using namespace std;
const int maxQsize = 100;
// This creates a generic queue class.
template <class QType> class Queue {
QType q[maxQsize]; // this array holds the queue
int size; // maximum number of elements that the queue can store
int putloc, getloc; // the put and get indices
public:
// Construct a queue of a specific length.
Queue(int len) {
// Queue must be less than max and positive.
if(len > maxQsize) len = maxQsize;
else if(len <= 0) len = 1;
size = len;
putloc = getloc = 0;
}
// Put a data into the queue.
void put(QType data) {
if(putloc == size) {
cout << " -- Queue is full.\n";
return;
}
putloc++;
q[putloc] = data;
}
// Get data from the queue.
QType get() {
if(getloc == putloc) {
cout << " -- Queue is empty.\n";
return 0;
}
getloc++;
return q[getloc];
}
};
// Demonstrate the generic Queue.
int main()
{
Queue<int> iQa(10), iQb(10); // create two integer queues
iQa.put(1);
iQa.put(2);
iQa.put(3);
iQb.put(10);
iQb.put(20);
iQb.put(30);
cout << "Contents of integer queue iQa: ";
for(int i=0; i < 3; i++)
cout << iQa.get() << " ";
cout << endl;
cout << "Contents of integer queue iQb: ";
for(int i=0; i < 3; i++)
cout << iQb.get() << " ";
cout << endl;
Queue<double> dQa(10), dQb(10); // create two double queues
dQa.put(1.01);
dQa.put(2.02);
dQa.put(3.03);
dQb.put(10.01);
dQb.put(20.02);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -