Was burning time at work so might as well code something. Below are 4 classes. Each class has functions that print either a positive output or a negative output.
This is mostly for the intermediate to advance level c++ coders since it requires a good knowledge of Inheritance and Abstract Classes.
GOAL
With just one line of code get a positive output which is in this case CORRECT OUTPUT.
YOU CANNOT CHANGE ANY CODE only add one line where indicated in the main.
A line of code ends with a ';'.
I only came up with 2 possible ways to get the desired output. Good luck
CODE
Code:
#include <iostream>
#include <string>
using namespace std;
class A
{
protected:
string result;
public:
A() {
result = "CORRECT OUTPUT";
}
virtual void print() = 0;
void print2()
{
print();
}
};
class B : public A
{
private:
public:
B(){} ~B()
{
result = "NO NO NO!!!";
}
virtual void print2()
{
cout << result << endl;
}
};
class C : public B
{
private:
string result;
public:
C() : B() {
result = "CORRECT OUTPUT";
}
virtual void print() = 0;
void print2()
{
print();
}
};
class ABC : public A, public C
{
private:
string result;
public:
ABC() : A(), C() {
result = "DON'T OUTPUT ME!!!";
}
void print()
{
cout << result << endl;
}
};
int main(int argc, char **arg)
{
ABC * abc = new ABC();
//one line of code here
return 0;
}