-4

Here's the abstract class

class number_sort{
    public:
        int num[1024];
        int qww(int a);

    public:
        bool status;
        int number=0;
        virtual bool compare(int a, int b);
        virtual void sort();
        virtual bool test(int a[]);

        virtual void setSet(int a[]);

};    

Here's the derived class

class big_to_small:public number_sort{
    public: int finish[1024];

    void setSet(int a[]){
        for(int i=1;i<=1024;i++){
            if(a[i]!=0) {
                num[i]=a[i];
                number++;
            }
        }
    }

    bool compare(int a, int b) {
        int retur = 0;
        if (a >= b) {
            retur = a;
            status = true;
        }
        if (b >= a) {
            retur = b;
            status = false;
        }
        if (a == b) {
            retur = a;
            status = true;
        }
        return status;
    }

    bool test(int a[]) {
        int numm = 0;
        bool retur = true;
        for (int i = 1; i <= 1024; i++) {
            if (a[i] == 0) {
                numm = i;
                break;
            }
        }
        for (int i = 2; i <= numm; i++) {
            if (a[i - 1] < a[i]) {
                retur = false;
                break;
            }
        }
        return retur;
    }

    void sort() {
        for (int i = 2; i <= number; i++) {
            if (!compare(num[i - 1], num[i])) {
                int k = num[i - 1];
                num[i - 1] = num[i];
                num[i] = k;
            }
        }
        if (!test(num))
            sort();
    }

};

I wrote the abstract class and its derivative in seperate cpp and hpp. In the main I declare the class using big_to_small bigg; but it keep showing build fail.

enter image description here

I haven't used C++ in a very long time, and I have been using java for the past few years. Is this some kind of declaration error or I wrote my abstract class wrong?

sirandy
  • 1,834
  • 5
  • 27
  • 32
  • 3
    Personally, I find the choice of style very difficult to read. – François Andrieux Nov 23 '18 at 18:34
  • Where is `number_sort` implementation? This is required. Also the destructor should be virtual. – Matthieu Brucher Nov 23 '18 at 18:35
  • Did you define num and qww somewhere ? – willll Nov 23 '18 at 18:54
  • 1
    How is the base class abstract? –  Nov 23 '18 at 18:59
  • Prefer to use `std::vector`. When passing an array, you need the address of the first element, *as well as the capacity* of the array. In C++, the capacity of the array is not passed as the parameter. – Thomas Matthews Nov 23 '18 at 20:23
  • In your `compare` function, the variable `retur` is local and its value will disappear when execution leaves the function. You don't use the value of `retur`, so you should remove the variable and all instances in the function. – Thomas Matthews Nov 23 '18 at 20:25
  • You can replace the content of your `compare` function with: `return a <= b;`. – Thomas Matthews Nov 23 '18 at 20:26
  • I recommend the style of one statement per line. The compiler ignores whitespace, such as newlines, so there is no reason to put multiple statements on one line. One statement per line is easier to read. Code that is easy to read has fewer defects than obfuscated code. – Thomas Matthews Nov 23 '18 at 20:30

1 Answers1

1

Try using:

class number_sort{
public:
int num[1024];
int qww(int a);

public:
bool status;
int number=0;
virtual bool compare(int a, int b) = 0;
virtual void sort() = 0;
virtual bool test(int a[]) = 0;

virtual void setSet(int a[]) = 0;
virtual ~number_sort() = default;
};

And then use override for your inherited class overriden methods.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62