1

I have a problem with polymorphism and inheritance. The parent class is:

class IIR_filter {

private:
    double* a_;
    double* b_;
    unsigned int order_;

    void reverseArray(double* source, double* destination, unsigned int size);

public:
    IIR_filter();
    IIR_filter(double* a, double* b, unsigned int order);   //c'tor
    IIR_filter(const IIR_filter& filter);                   //copy c'tor
    virtual ~IIR_filter();                                          //d'tor

    virtual void filter(double* inputX, double* outputY, unsigned int size);

    virtual void zeroPhaseFilter(double* inputX, double* outputY, unsigned int size);

    friend ostream& operator<<(ostream& lhs, const IIR_filter& rhs);

protected:
    double filterSample(unsigned int indexY, double* outputY,double newSample, Queue<double>& filter);
    double getElementA(unsigned int index) const;
    double getElementB(unsigned int index) const;
    unsigned int getOrder() const;
    virtual void print(ostream& lhs, const IIR_filter* rhs)const;
};

and the cpp relevant functions are:

ostream& operator<<(ostream& lhs, const IIR_filter& rhs){
    std::cout << "IIR filter: " << std::endl;
    rhs.print(lhs, &rhs);
    return lhs;
    }

and the derived class is:

class FIR_filter : public IIR_filter {

public:
    FIR_filter(double* b, unsigned int order);              //c'tor
    friend ostream& operator<<(ostream& lhs, FIR_filter* rhs);


};

And the cpp function is:

ostream& operator<<(ostream& lhs, FIR_filter* rhs){
lhs << "FIR filter: \n";
rhs->print(lhs, rhs);
return lhs;
}

And of course there is the print function in the IIR_filter class, but it's too long and irrelevant for my opinion.

The problem is: When I call the printing function (operator<<) from an object of the derived class, the print is done from the parent class, so instead of the header "FIR filter" i get the header "IIR filter". I have tried many ways but have no success. Please help. Thank, Ran.

Ran G
  • 21
  • 5
  • Don't you have an override of `print()` in the derived class ?? – Christophe Jun 21 '16 at 21:10
  • This is incorrect: " When I call the printing function (operator<<) from an object of the derived class, the print is done from the parent class". That would evoke the `operator<<` for the derived class, if the argument was a pointer as it should be to match your declaration. Voting to close as lacking reproducible example. – Cheers and hth. - Alf Jun 21 '16 at 21:40
  • 2
    That said, *most likely* the problem is `FIR_filter* rhs`; change that to `FIR_filter const& rhs`. – Cheers and hth. - Alf Jun 21 '16 at 21:43

2 Answers2

1

You should move the writing of a header into a stream inside the virtual print function, and create a suitable override in the derived class. You might also want to refactor the code to have a virtual print_header function called inside a non-virtual print, since you're currently not overriding it in the derived class.

By the way, you have inconsistency - one operator<< is accepting a reference to a const object, and the other is accepting a pointer. But even if you unified your convention, the operator<< is still not virtual, so it cannot determine in runtime if the reference (or pointer) to the base type indeed points to a derived object. It means, that in current state, it will (and should) always print the "IIR filter" header.

pzelasko
  • 2,082
  • 1
  • 16
  • 24
0

You can't derive friend ship. See this question for more details: friend class with inheritance

Also, like that was mentioned before, You should have one overloaded operator<< in base class which just returns only output of print function. Than in print implementations You can do all printing and other stuff for all Your classes.

Community
  • 1
  • 1
Piotrek
  • 88
  • 11