虚函数无法正常工作

时间:2018-04-23 07:58:00

标签: c++ inheritance virtual-functions

我需要使用添加和继承的虚函数编写程序。 我写了这3个类,但我的虚函数没有显示结果。 它应该写出decimal de("10004")decimal deci("12");的总和,但它只显示Array->

我怎么可能解决这个问题?

#include <string>
#include <iostream>
#include <cassert>
using namespace std;


int rev(int);

class Array
{
private:
    string arr[100];
    size_t size;
    string str;
public:
    Array();
    Array(string s)
    {
        str = s;
        cout << str << endl;
    }
    /*{
        cin >> size;
        for (int i = size - 1; i >= 0; i--)
        {
            arr[i] = '0';
            cout << arr[i];
        }
        cout << endl;
    }*/
    string& operator[] (const int index);
    Array operator +(Array ar)
    {
        int x = stoi(str);
        int y = stoi(ar.str);
        int d = x + y;
        string s = to_string(d);
        return s;
    }
    virtual void plus( Array *b)
    {
        cout << "Array -> " << str + b->str << endl;
    }
};

string& Array::operator[] (const int index)
{
    assert(index >= 0 && index < size);
    return arr[index];
}


class decimal : public Array
{
private:
    unsigned char dec[100];
    size_t size;
public:
    decimal() : size{ 0 } {}
    decimal(const char* get)
    {
        size = strlen(get);
        for (int i = size - 1; i >= 0; i--, get++)
        {
            dec[i] = *get;
            cout << dec[i];
        }
        cout << endl;
    }
    void plus(decimal* b);
    friend decimal operator + (decimal const &, decimal const &);
    friend decimal operator - (decimal const &, decimal const &);
    friend decimal operator * (decimal const &, decimal const &);
    friend decimal operator / (decimal const &, decimal const &);
    friend decimal operator % (decimal const &, decimal const &);
};

decimal operator + (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) + rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

decimal operator - (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) - rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

decimal operator * (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) * rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

decimal operator / (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) / rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

decimal operator % (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) % rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

void decimal::plus( decimal* b)
{
    cout << "Array -> " << atoi((char*)dec) + atoi((char *)b->dec) << endl;
}

int rev(int n)
{
    int  r = 0, remainder;
    while (n != 0)
    {
        remainder = n % 10;
        r = r * 10 + remainder;
        n /= 10;
    }
    return r;
}


class BitString : public Array
{
    char *string;
public:
    BitString() = default;
    BitString(char *str)
    {
        string = new char[strlen(str) + 1]; strcpy_s(string, strlen(str) + 1, str);
    }



    void operator ~()
    {
        for (int i = 0; i<strlen(string); i++)
            string[i] == '1' ? string[i] = '0' : string[i] = '1';
    }

    void operator &(BitString a)
    {
        if (strlen(string) != strlen(a.string))
            cout << "Different lengths" << endl;
        else
        {
            for (int i = 0; i<strlen(a.string); i++)
                if (string[i] == '1' && a.string[i] == '1')
                    string[i] = '1';
                else
                    string[i] = '0';
        }
    }

    void operator +(BitString a)
    {
        if (strlen(string) != strlen(a.string))
            cout << "Different lengths" << endl;
        else
        {
            for (int i = 0; i<strlen(a.string); i++)
                if (string[i] == '1' || a.string[i] == '1')
                    string[i] = '1';
                else
                    string[i] = '0';
        }
        for (int i = 0; i < strlen(string); i++)
            cout << string[i];
        cout << endl;
    }

    void operator ^(BitString a)
    {
        if (strlen(string) != strlen(a.string))
            cout << "Different lengths";
        else
        {
            for (int i = 0; i<strlen(a.string); i++)
                if ((string[i] == '1' && a.string[i] == '0') || (string[i] == '0' && a.string[i] == '1'))
                    string[i] = '1';
                else
                    string[i] = '0';
        }
    }

    void Show()
    {
        cout << string << endl;
    }

    void plus(BitString *b)
    {
        cout << "bitstring -> " << atoi(string) + atoi(b->string) << endl;
    }
};




int main()
{
    int a;
    char str[80];
    Array *arr;
    Array *arr1;
    BitString obj("1010101");
    BitString obj1("1110001");
    decimal de("10004");
    decimal deci("12");
    arr = &de;
    arr1 = &deci;
    arr->plus(arr1);



    system("Pause");

}

编辑:

#include <string>
#include <iostream>
#include <cassert>
using namespace std;


int rev(int);

class Array
{
public:
    string arr[100];
    size_t size;
    string str;
public:
    Array() {  };
    Array(string s)
    {
        str = s;
        cout << str << endl;
    }

string& operator[] (const int index);
Array operator +(Array ar)
{
    int x = stoi(str);
    int y = stoi(ar.str);
    int d = x + y;
    string s = to_string(d);
    return s;
}
virtual void plus(Array *b)
{
    cout << "Array -> " << str + b->str << endl;
}
};

string& Array::operator[] (const int index)
{
    assert(index >= 0 && index < size);
    return arr[index];
}


class decimal : public Array
{
private:
    unsigned char dec[100];
    size_t size;
public:
    decimal() : size{ 0 } {}
    decimal(const char* get)
    {
        size = strlen(get);
        for (int i = size - 1; i >= 0; i--, get++)
        {
            dec[i] = *get;
            cout << dec[i];
        }
        cout << endl;
    }
    void plus(Array* b);
    friend decimal operator + (decimal const &, decimal const &);
    friend decimal operator - (decimal const &, decimal const &);
    friend decimal operator * (decimal const &, decimal const &);
    friend decimal operator / (decimal const &, decimal const &);
    friend decimal operator % (decimal const &, decimal const &);
};

decimal operator + (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) + rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

decimal operator - (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) - rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

decimal operator * (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) * rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

decimal operator / (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) / rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

decimal operator % (decimal const &a, decimal const &b)
{
    int x = atoi((char *)a.dec);
    int y = atoi((char *)b.dec);
    int d = rev(x) % rev(y);
    string s = to_string(d);
    return decimal(s.c_str());
}

void decimal::plus(Array* b)
{
    cout << "Array -> " << atoi((char*)dec) + stoi(b->str) << endl;
}

int rev(int n)
{
    int  r = 0, remainder;
    while (n != 0)
    {
        remainder = n % 10;
        r = r * 10 + remainder;
        n /= 10;
    }
    return r;
}


class BitString : public Array
{
    char *string;
public:
    BitString() = default;
    BitString(char *str)
    {
        string = new char[strlen(str) + 1]; strcpy_s(string, strlen(str) + 1, str);
    }

    void operator ~()
    {
        for (int i = 0; i<strlen(string); i++)
            string[i] == '1' ? string[i] = '0' : string[i] = '1';
    }

    void operator &(BitString a)
    {
        if (strlen(string) != strlen(a.string))
            cout << "Different lengths" << endl;
        else
        {
            for (int i = 0; i<strlen(a.string); i++)
                if (string[i] == '1' && a.string[i] == '1')
                    string[i] = '1';
                else
                    string[i] = '0';
        }
    }

    void operator +(BitString a)
    {
        if (strlen(string) != strlen(a.string))
            cout << "Different lengths" << endl;
        else
        {
            for (int i = 0; i<strlen(a.string); i++)
                if (string[i] == '1' || a.string[i] == '1')
                    string[i] = '1';
                else
                    string[i] = '0';
        }
        for (int i = 0; i < strlen(string); i++)
            cout << string[i];
        cout << endl;
    }

    void operator ^(BitString a)
    {
        if (strlen(string) != strlen(a.string))
            cout << "Different lengths";
        else
        {
            for (int i = 0; i<strlen(a.string); i++)
                if ((string[i] == '1' && a.string[i] == '0') || (string[i] == '0' && a.string[i] == '1'))
                    string[i] = '1';
                else
                    string[i] = '0';
        }
    }

    void Show()
    {
        cout << string << endl;
    }

    void plus(Array *b)
    {
        cout << "bitstring -> " << atoi(string) + stoi(b->str) << endl;
    }
};




int main()
{
    int a;
    char str[80];
    Array *arr;
    Array *arr1;
    BitString obj("1010101");
    BitString obj1("1110001");
    decimal de("10004");
    decimal deci("12");
    arr = &de;
    arr1 = &deci;
    arr->plus(arr1);
    arr = &obj;
    arr1 = &obj1;
    arr->plus(arr1);

    system("Pause");

}

2 个答案:

答案 0 :(得分:1)

尝试在string中替换名为BitString的变量。它是命名空间<string>中的std库中定义的类型,并且您将其与using namespace std;一起使用。命名存在混淆。例如,在此示例中,我将string重命名为stringMine

class BitString : public Array
{
    char *stringMine;
public:
    BitString() = default;
    BitString(char *str)
    {
        stringMine = new char[strlen(str) + 1]; strcpy_s(stringMine, strlen(str) + 1, str);
    }



    void operator ~()
    {
        for (int i = 0; i<strlen(stringMine); i++)
            stringMine[i] == '1' ? stringMine[i] = '0' : stringMine[i] = '1';
    }

    void operator &(BitString a)
    {
        if (strlen(stringMine) != strlen(a.stringMine))
            cout << "Different lengths" << endl;
        else
        {
            for (int i = 0; i<strlen(a.stringMine); i++)
                if (stringMine[i] == '1' && a.stringMine[i] == '1')
                    stringMine[i] = '1';
                else
                    stringMine[i] = '0';
        }
    }

    void operator +(BitString a)
    {
        if (strlen(stringMine) != strlen(a.stringMine))
            cout << "Different lengths" << endl;
        else
        {
            for (int i = 0; i<strlen(a.stringMine); i++)
                if (stringMine[i] == '1' || a.stringMine[i] == '1')
                    stringMine[i] = '1';
                else
                    stringMine[i] = '0';
        }
        for (int i = 0; i < strlen(stringMine); i++)
            cout << stringMine[i];
        cout << endl;
    }

    void operator ^(BitString a)
    {
        if (strlen(stringMine) != strlen(a.stringMine))
            cout << "Different lengths";
        else
        {
            for (int i = 0; i<strlen(a.stringMine); i++)
                if ((stringMine[i] == '1' && a.stringMine[i] == '0') || (stringMine[i] == '0' && a.stringMine[i] == '1'))
                    stringMine[i] = '1';
                else
                    stringMine[i] = '0';
        }
    }

    void Show()
    {
        cout << stringMine << endl;
    }

    void plus(BitString *b)
    {
        cout << "bitstring -> " << atoi(stringMine) + atoi(b->stringMine) << endl;
    }
};

您还需要正确覆盖void plus(Array *b) void decimal::plus( decimal* b);不会覆盖它是一个新函数,它必须是

void decimal::plus( Array* b)

覆盖。或者语法很好:

virtual void decimal::plus( Array* b) override

关于BitString方法,正确的覆盖方式是:

virtual void plus(Array *b) override
    {
        cout << "bitstring -> " << atoi(stringMine) + atoi(b->stringMine) << endl;
    }

但毕竟你需要在基类中声明两个变量来修复编译错误:

class Array
{
public:
    char *stringMine;
    unsigned char dec[100];

您可以在基类的派生类中调用这些变量。

答案 1 :(得分:0)

您的参数类型不同,您不会覆盖派生类,只是隐藏基类定义。

应该说

    class decimal : public Array
    {
    public:
        void plus(Array* b);
    };

    class BitString : public Array
    {
    public:
        void plus(Array* b);
    };