简体   繁体   中英

Abstract Class and Pure Virtual Method in C++

I have 4 C++ files, 2 headers, and 2 .cc files. This is just a proof of concept but I can't seem to get it right.

My first header looks like this:

#ifndef INT_LIST_H
#define INT_LIST_H
class IntList
{
  public:

     //Adds item to the end of the list
     virtual void pushBack(int item) = 0;
};
#endif

My second header uses the first and looks like this:

#ifndef ArrayIntList_H
#define ArrayIntList_H
#include "IntList.h"


class ArrayIntList : public IntList
{
    private:
        int* arrayList;
        int* arrayLength;

     public:
        //Initializes the list with the given capacity and length 0
        ArrayIntList(int capacity);

        //Adds item to the end of the list
        virtual void pushBack(int item) = 0;

};
#endif  

my first .cc file fills in the methods of the previous class:

#include <iostream>
#include "ArrayIntList.h"

ArrayIntList::ArrayIntList(int capacity)
{
    //make an array on the heap with size capacity
    arrayList = new int[capacity];
    //and length 0
    arrayLength = 0; 
}

void ArrayIntList::pushBack(int item)
{
    arrayList[*arrayLength] = item;
}

And this is my main function:

#include <iostream>
#include "ArrayIntList.h"

int main(int argc, const char * argv[])
{
    ArrayIntList s(5);
}

When I run this in Xcode I get an error that "Variable ArrayIntList is an abstract class" I don't understand how this can be since I defined it in my above .cc file. Any ideas?

On class ArrayIntList use this

virtual void pushBack(int item);

instead of this

virtual void pushBack(int item) = 0;

The reason is that when you assign a 0 to a function declaration, you are saying it is "pure", or not implemented. But you are doing that (implementing it) below.

You've got ArrayIntList::pushBack(int item) declared as a pure virtual function. That's what that = 0 does. Remove the = 0 from ArrayIntList.h.

Also: you're using an int pointer instead of an int to track your array length.

In your declaration of the ArrayIntList class, you need to remove the "= 0" from the method declaration. You probably also need to declare arrayLength to be an int rather than a pointer to an int. Finally, since you're allocating memory for the array in your constructor, you should declare a destructor to free the memory when the object is destroyed:

class ArrayIntList : public IntList
{
private:
    int* arrayList;
    int arrayLength;

public:
    //Initializes the list with the given capacity and length 0
    ArrayIntList(int capacity);

    virtual ~ArrayIntList() { delete arrayList; }

    //Adds item to the end of the list
    virtual void pushBack(int item);

};

Of course, the best way to handle the array list would be to use a std::vector<int> instead so you don't have to manually handle the memory allocation and deallocation

In class ArrayIntList you are declaring a pure-virtual "virtual void pushBack(int item) = 0;" which you have already declared in the abstract parent IntList. All you need to do is declare it as "virtual void pushBack(int item);".

An abstract base class cannot inherit from another Abstract base class, remove the

= 0;

from your equation in ArrayIntList:

virtual void pushBack(int item) = 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM