Puzzle from Java into C++

The puzzle
The original puzzle is in Java http://wouter.coekaerts.be/2012/puzzle-clowns, so I tried to write it in C++ and post it here for your, so that you solve it and having some thing to think:-)


There almost aren’t any rules; any cheating inside your code is allowed; it is the whole point of the puzzle. But you cant change any members or any method, or modify the three classes, changes are not allowed for the three classes.

How can you fit 20 clowns into a Volkswagen? Three classes are given: an empty Clown class, a super class Synchronized, and a Volkswagen class to which you can add clowns. When you try to add a Clown, it is checked that it isn’t already full. But if you just try hard enough, there’s always room for some extra clowns…

1st Class
1
2
3
4
5
class Clown {
public:
	Clown(){}
	virtual ~Clown(){}
};


2end Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Synchronized{
protected  :
		static Synchronized* instance;


public:
	Synchronized(){
		if(instance != 0 ){
			cout<< "multi thread is not allowed";
			exit(0);
		}
		else{
			instance = this ;
		}
	}
	virtual ~Synchronized(){
	}
};
Synchronized* Synchronized::instance = 0 ;


third class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

class Volkswagen: public Synchronized{
private:
	const static int CAPACITY = 5;
	vector<Clown> clowns;

public:
	Volkswagen(){}

	void add(const Clown& clown) {
			if (clowns.size() >= CAPACITY) {
				cout<<"I'm full"<<endl;
				exit(0);
		} else {
			clowns.push_back(clown);
		}
	}

	void done() const{
		if (clowns.size() == 20) {
			// The goal is to reach this line
			cout << "I'm a Volkswagen with 20 clowns!" << endl;
		}
	}
};


Your code

1
2
3
4
5
6
7
8
9
10
struct You {

	static void run(){
		Volkswagen vw;
		for (int i = 0; i < 20; i++)
			vw.add(Clown());
		vw.done();
	}

};

Last edited on
Edit: Code removed.
Last edited on
was solved by Athar using reinterpret_cast on Feb 29
Topic archived. No new replies allowed.