[ create a new paste ] login | about

Link: http://codepad.org/Pz5jwMVP    [ raw code | output | fork ]

C++, pasted on Apr 17:
/* Problem 43 */
	    #include <iostream>
	    using namespace std;
	    #include <cstring>
	    class scary {
		char is[31];
		char sez[31];
	      public:
		scary() { strcpy(is, "Creep"); strcpy(sez, "boooo"); }
		scary(const char i[], const char s[])
		    { strcpy(is, i); strcpy(sez, s); }
		virtual void sayis(ostream &os) const { os << is; }
		void saysez(ostream &os) const // this is NOT virtual!!
		    { os << sez; }
	    };
	    ostream &operator<<(ostream &os, const scary &x) {
		x.saysez(os);
		os << ", said the ";
		x.sayis(os);
		return os;
	    }
	    class ghost: public scary {
	      public:
		ghost():scary("Ghost", "Boo!") {cout<<"made a ghost\n";}
	    };
	    class witch: public scary {
		char extra[31];
	      public:
		witch(): scary("Witch", "Toil and Trouble") {
		    cout << "which wicked witch?\n";
		    strcpy(extra, "Double, Double");
		}
		void saysez(ostream &os) const { os << extra << " ";
						 scary::saysez(os);
		}
	    };
	    void trythis(scary g) { cout << g << '\n'; }
	    int main() { scary s;  ghost g;  witch w;
			 cout << s << '\n' << g << '\n' << w << '\n';
			 trythis(s);  trythis(g);  trythis(w);
			 s.saysez(cout);  cout << '\n';
			 g.saysez(cout);  cout << '\n';
			 w.saysez(cout);  cout << '\n';
			 return 0;
	    }


Output:
1
2
3
4
5
6
7
8
9
10
11
made a ghost
which wicked witch?
boooo, said the Creep
Boo!, said the Ghost
Toil and Trouble, said the Witch
boooo, said the Creep
Boo!, said the Ghost
Toil and Trouble, said the Witch
boooo
Boo!
Double, Double Toil and Trouble


Create a new paste based on this one


Comments: