0

Class A is abstract class. I would like to call the same method from different subclasses. There are no errors from compiler, but program crashes.

class A
{
    protected:

        MA* array1[10]; //static array of pointers to some other class
        int st;

    public:
    ...
        virtual string ToString() const
        {
            stringstream ss;

            for(int i=0;i<st;i++)
            {
                ss<<array1[i]->getname()<<",";
            }

            return ss.str();
        }
};

ToString method works in both classes A and B but doesn't work in class C

class B: public A
{
    private:
    ...

    public:

        string ToString() const
        {
            return A::ToString();
        }
};

class C: public A
{
    private:
    ...

    public:

        string ToString() const
        {
            return A::ToString();    
        }
};

This is main:

A* P[5];
P[0]=new B();
P[1]=new C();
P[0]->ToString();
P[1]->ToString();
Thomas
  • 6,291
  • 6
  • 40
  • 69
  • 1
    `st` is never initialized in this code... Where are you initializing it? `ToString` can't possibly work unless it has a value < 10. (And if it's non-zero, where are you initializing `array1`?) – Cameron May 05 '14 at 16:19

2 Answers2

0

I'm pretty sure it works in neither A nor B.

The only reason it seems to would be Undefined Behaviour.

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
0

Here is the issue:

First, your derived classes (B and C) are inheriting Class A's public member functions (I know this is obvious looking at the code - but, we have to start somewhere); however, the derived classes are overriding your Parent class's (Class A) member functions by utilizing the same function identifier. The issue here is that the Parent does not expect the inheriting - derived - classes to override the parent classes, which they are doing. The Parent only expects the derived classes to inherit; therefore, when you call the function of Class B and Class C, something weird is happening.

Two issues arise as a result: first (1), that which we discuss above (see code, below); second (2), the derived classes do not have access to the Parent class's member function definition unless called via an object of the parent class (which would not normally be the case, here, were your functions properly declared).

So, what can you do? Use the override keyword in your Parent declaration and in your child declaration.

For instance:

Class A
{
    private:
    ....
    public:
    virtual returntype functionIndentifier(parameters) override;
};

Now, that has to do with inheritance; however, in C++, were your class actually abstract, you would need to use a pure virtual function (by setting the function equal to 0), not defining the function in the abstract class, leaving out the override keyword, and ensuring each child class implements - and defines - the function.

Class A
{
    private:
    ....
    public:
    virtual returntype functionIndentifier(parameters) = 0;
};

pure virtual functions force a child class to both inherit and implement - define - a member function to the child class's needs. Here, if the pure virtual class were not defined in your child's class definition, then - I believe - you would in fact encounter a compile-time error.

Thomas
  • 6,291
  • 6
  • 40
  • 69