Can operator<< in derived class call another operator<< in base class in c++?

Posted by ivory on Stack Overflow See other posts from Stack Overflow or by ivory
Published on 2013-10-31T15:32:21Z Indexed on 2013/10/31 15:53 UTC
Read the original article Hit count: 332

In my code, Manager is derived from Employee and each of them have an operator<< override.

class Employee{
protected:
    int salary;
    int rank;
public:
    int getSalary()const{return salary;}
    int getRank()const{return rank;}
    Employee(int s, int r):salary(s), rank(r){};
};
ostream& operator<< (ostream& out, Employee& e){
    out << "Salary: " << e.getSalary() << " Rank: " << e.getRank() << endl;
    return out;
}

class Manager: public Employee{
public:
    Manager(int s, int r): Employee(s, r){};
};
ostream& operator<< (ostream& out, Manager& m){   
    out << "Manager: ";
    cout << (Employee)m << endl;  //can not compile, how to call function of Employee?
    return out;
}

I hoped cout << (Employee)m << endl; would call ostream& operator<< (ostream& out, Employee& e), but it failed.

© Stack Overflow or respective owner

Related posts about c++

Related posts about operator-overloading