C++ Quiz question

Pages: 12
Hello folks,
Here an easy (or not) question for those ho don't know what to do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class MyClass
{
public:
	// PUBLIC AREA MUST BE BLANK

protected:
	// PROTECTED AREA MUST BE BLANK

private:
	// PRIVATE AREA IS ALLOWED for Print functioin only

	virtual void Print()
	{
		cout << "MyClass" << endl;
	}
};

int main()
{
	MyClass test;
	test.Print(); // ERROR
	return 0;
}


Question is as folows:

execute Print function by not changing it's privacy.(means do not move it into PROTECTED OR PUBLIC AREA inside the class)
also do not duplicate access modifers(private, protected, public)
also do not change the function it self.

EDIT:
do not change the implementation of the class(THAT MEANS only Print function is allowed)
Last edited on
Can we add stuff into the class's private section as long as we do not modify the function? Or can we not modify the class at all?
Hi firedraco,
no, only Print function is allowed in the entry class.

Last edited on
Are we allowed to make MyClass inherit from a base class? In that case i have solved it.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

class Base
{
public:
	void PublicPrint()
	{
		Print();
	}
private:
	virtual void Print() = 0;
};

class MyClass : public Base
{
public:
	// PUBLIC AREA MUST BE BLANK

protected:
	// PROTECTED AREA MUST BE BLANK

private:
	// PRIVATE AREA IS ALLOWED for Print functioin only

	virtual void Print()
	{
		cout << "MyClass" << endl;
	}
};

int main()
{
	MyClass test;
	test.PublicPrint();
	return 0;
}
Hi Peter87,
OK! :D
while your answer is legal and correct, honestly, I was expecting another kind of answer:
please let's mark your answer as 50% solved

let's continue this "Quiz"...

Are we allowed to make MyClass inherit from a base class?

yes inheritance is allowed but, answer must be more efficient:

base class may not have any helper member functions like PublicPrint() (but pure virtual method is allowed aniway)

cheers :D
Does this count? I know it's ugly but I guess it's an answer.

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
26
27
28
29
30
#include <iostream>

#define private public

using namespace std;

class MyClass
{
public:
	// PUBLIC AREA MUST BE BLANK

protected:
	// PROTECTED AREA MUST BE BLANK

private:
	// PRIVATE AREA IS ALLOWED for Print functioin only

	virtual void Print()
	{
		cout << "MyClass" << endl;
	}
};

int main()
{
	MyClass test;
	test.Print();
	return 0;
}
closed account (DSLq5Di1)
Minor change to Peters answer,
1
2
3
4
5
class Base
{
public:
	virtual void Print() = 0;
};

1
2
3
4
5
6
7
int main()
{
	MyClass test;
	Base& ref = test;
	ref.Print();
	return 0;
}
Sloppy9, TheMassiveChipmunk,
Yeah you got it :D

well, TheMassiveChipmunk is cheating by using his approach, but however the purpose of this quiz was to show the power of...... INTERFACES!! which sadly aren't implemented into C++ language but we are stil able to create them :D

cheers!
New quiz:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<typename T, typename U> //cannot specialize template
class e //cannot inherit from another class
{ //can not modify this class at all
    void Print()
    {
        std::cout << "You haxxor..." << std::endl;
    }
public:
    T g()
    {
        return&U::Print;
    }
};//You may make one class that can not inherit from this class, and no other classes.
//The class you make cannot print anything 

This is possible. Getting an instance is the first step, then you need the member function pointer, and finally, you have to call the function. Get ready for syntax headaches!
Last edited on
L B,

OK, I think I've solved your quiz :D

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
26
27
28
29
30
31
32
33
template<typename T, typename U>
class e 
{ 
    void Print()
    {
        std::cout << "You haxxor..." << std::endl;
    }
public:
    T g()
    {
        return&U::Print;
    }
};

struct temp
{
    void Print()
    {
        std::cout << "You haxxor..." << std::endl;
    }
}tmp;

typedef void (temp::*CALLBACK)();
typedef e<CALLBACK, temp> OBJECT;

int main()
{
	OBJECT hInstance;
	CALLBACK pHinst = hInstance.g();
	(tmp.*pHinst)();
	cin.ignore();
	return 0;
}
I was just going to use #define but I like the other solutions better.
CodKiddy, the class you make cannot print anything out. Sorry I forgot to mention that.
e<A, B> does not have access to private members of e<C, D>, thus for g() to compile, the second template argument U has to be of the same type as *this. That would lead to an infinite type and I have a feeling that even if there was a valid way to write one, C++ compilers aren't prepared to handle infinity..

I'm guessing you wanted #define class struct ?
No. I'll give a hint.
class e_er {public:void Print();}; and then

e<void (e<void (e_er::*)(), e_er>::*)(), e_er> E;
Now you have an instance, call g() and then call the function.
Last edited on
Like I said, same class name with different template arguments makes two unrelated classes that don't have access to each others privates.
http://ideone.com/X7jbB
A better error is given by MSVC++:
main.cpp(13): error C2248: 'e<T,U>::Print' : cannot access private member declared in class 'e<T,U>'
It compiles and runs here :p
Edit: VC++2008
Last edited on
Can someone explain to me the applicability of this.

#define private public

To me, it seems like you are shooting yourself in the foot.
@thepedestrian I know that defining private as public is horrible but this is just a fun quiz, no real danger in using #define private public :)
Last edited on
thepedestrian,
the applicability of #define private public is to break your own rules or to solve trick like this one which L B posted :D
It is not an allowed solution because you yourself said it was cheating ;p
Pages: 12