class MyClass
{
public:
// PUBLIC AREA MUST BE BLANK
protected:
// PROTECTED AREA MUST BE BLANK
private:
// PRIVATE AREA IS ALLOWED for Print functioin only
virtualvoid 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)
#include <iostream>
usingnamespace std;
class Base
{
public:
void PublicPrint()
{
Print();
}
private:
virtualvoid 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
virtualvoid 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)
#include <iostream>
#define private public
usingnamespace std;
class MyClass
{
public:
// PUBLIC AREA MUST BE BLANK
protected:
// PROTECTED AREA MUST BE BLANK
private:
// PRIVATE AREA IS ALLOWED for Print functioin only
virtualvoid Print()
{
cout << "MyClass" << endl;
}
};
int main()
{
MyClass test;
test.Print();
return 0;
}
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
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!
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..
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>'