-4

I already fill the blanks lines(line1, line2 line3) but I am not getting any output.

Note - only need to edit those three lines

Code -

#include <iostream>
#include <cstring>

using namespace std;

class Base
{
protected:
    string s;

public:
    Base(string c) : s(c) {}

    virtual ~Base() {}   // line 1

    virtual string fun(string a) = 0; // line 2 
};

class Derived : public Base
{
public:
    Derived(string c) : Base(c) {}
    ~Derived();
    string fun(string x)
    {
        return s + x;
    }
};
class Wrapper
{
public:
    void fun(string a, string b)
    {
        Base *t = (Base *) &a; // LINE-3
        string i = t->fun(b);
        cout << i << " ";
        delete t;
    }
};

Derived::~Derived() { cout << s << " "; }

int main()
{
    string i, j;
    cin >> i >> j;
    Wrapper w;
    w.fun(i, j);

    return 0;
}

More details and input/outputs -

input - o k 
expected output - ok o

input - c ++
expected output - c++ c

Details about these Q - I don't know what to write here so please forgive for not writing it.

Chris
  • 26,361
  • 5
  • 21
  • 42
Adi
  • 1

1 Answers1

2

That's a puzzle which teaches a bad practice of patchwork coding.

Line 3 should involve new because later in code delete t; is present. Whatever was created, would have method fun but cannot be new Base, because Base is abstract. Because fun is virtual, it can be:

Base *t = new Derived(a);

Derived uses string a for an initialization and prints it in destructor, which matches required output.

PS. There is no reason why that line cannot be Derived *t = new Derived(a); here.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42