// Hoare's Prime Number Sieve in C++
// Each prime is an object of class Prime
// Notice that the class is developed in terms of what a Prime is held to
// be responsible for in the program.
#include
class Prime{
int p; //A Prime must remember its value
Prime *next; //A Prime must remember where the next prime is.
public:
// A Prime is responsible for making sure it is set up correctly
Prime() { cerr<<"New Prime not initialized\n"; exit(1); }
Prime(const int n) { p=n ; next=(Prime*)NULL; }
// A Prime is responsible for handling possible primes it is sent
void sent(const int n){
if(n%p)
if(next) next->sent(n);
else next=new Prime(n);
}
// A Prime is responsible for printing itself and its next prime (if any)
friend ostream& operator<<(ostream& s, const Prime n)
{ s<<(n.p);
if(n.next){ s<<" "<<(*(n.next));
}
return s;
}
}; // end class Prime
main()
{
Prime *first=new Prime(2);
for( int n=3; n<=100; n++,n++)
first->sent(n);
cout<<*first<<"\n";
}