Driver.C 1/3
[top][prev][next]
#include <iostream>
#include "Foo.H"
using namespace std;
const int SENTINAL = 0;
int main () {
int num;
Foo foo = Foo();
cout << "Enter a positive number (" << SENTINAL << " to exit): " ;
cin >> num;
while ( num != SENTINAL ) {
try {
foo.setNum(num); // setNum might throw an exception
cout << "Success - foo.num = " << foo.getNum() << endl;
} catch (IllegalArgumentException) {
cerr << "Failure - caught IllegalArgumentException" << endl;
}
cout << "Enter a positive number (" << SENTINAL << " to exit): " ;
cin >> num;
}
return EXIT_SUCCESS;
}
Foo.C 2/3
[top][prev][next]
#include "Foo.H"
using namespace std;
void Foo::setNum(int num) {
if ( num > 0 ) { // if negative throw an exception
this->num = num;
} else {
throw IllegalArgumentException();
}
}
int Foo::getNum() {
return this->num;
}
Foo.H 3/3
[top][prev][next]
#ifndef _FOO_H_
#define _FOO_H_
class IllegalArgumentException {}; // lame exception class - empty
class Foo {
public:
void setNum(int num);
int getNum();
private:
int num;
};
#endif
Generated by GNU enscript 1.6.1.