jeudi 9 septembre 2021

Please help me out why I am getting error?

I can't figur it out why the Derivid class remain to be Abstract after overriding function fun().

Here is the error message:

error: invalid new-expression of abstract class type 'Derived' Base *t = new Derived(a);
error: no matching function for call to 'Base::fun(int&)'int i = t->fun(b);
#include <iostream>
using namespace std;

class Base
{
protected:
    int s;

public:
    Base(int i = 0) : s(i) {}
    virtual ~Base() {}
    virtual int fun() = 0;
    };

class Derived: public Base
{
public:
    Derived(int i) : Base(i) {}
    ~Derived() { cout << --s << " "; }
    int fun(int x) { return s * x; }
};

class Wrapper
{

public:
    void fun(int a, int b)
    {

        Base *t = new Derived(a);
        int i = t->fun(b);
        cout << i << " ";
        delete t;
    }
};

int main()
{
    int i, j;
    cin >> i >> j;
    Wrapper w;
    w.fun(i, j);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire