There is a base abstract class and there are two inherited from it. In each inherited class there are declarations of a friendly function which takes the cout object as the first argument, and the second refers to the object itself and this function displays the fields of this class. The problem is that the function can only display fields of the class in which it is declared, and how then can I display the fields of the base class? How is such a problem solved?

here is the base class

class AcctDMA { private: char *label; int rating; protected: char* get_label() { return label; } int get_rating() { return rating; } public: AcctDMA(const char * l = "null", int r = 0); AcctDMA(const AcctDMA &rs); virtual ~AcctDMA() = 0; AcctDMA & operator=(const AcctDMA &rs); friend std::ostream & operator<<(std::ostream & os,const AcctDMA & rs); }; 

here is an inherited class

 class hasDMA : public AcctDMA { private: char *style; public: hasDMA(const char * s = "none", const char *l = "null", int r = 0); hasDMA(const char * s, const AcctDMA &rs); hasDMA(const hasDMA &hs); ~hasDMA(); hasDMA & operator=(const hasDMA &rs); friend std::ostream & operator<<(std::ostream & os, const hasDMA & rs); }; 

the question was about the last function in the hasDma class

here is its implementation

 std::ostream & operator<<(std::ostream & os, const lacksDMA & rs) { os << "color: " << rs.color << std::endl; return os; } 

I tried to do so, it does not work

 std::ostream & operator<<(std::ostream & os, const lacksDMA & rs) { os << AcctDMA::get_label << std::endl; os << AcctDMA::get_rating << std::endl; os << "color: " << rs.color << std::endl; return os; } 
  • You are deceiving us. The friendly function takes hasDMA , and the implementation of lacksDMA is yrHeTaTeJlb
  • And when you figure out where you have some function, try rs.get_label() - yrHeTaTeJlb
  • Class members declared private not available in derived classes. operator<< is friends with the derived class and sees only what the derived class itself sees. To access private fields, use open or protected (in your case) getters - acade
  • Sorry, I confused the class. There are lacks DMA, but there is the same problem with a friendly function. I tried the method via rs.get_label () but the compiler swears. The only way I see is to create in the inherited class one more protected methods that call the protected methods of the base class, but will it not be a crutch or a wrong move? - Nikolay 2
  • What is AcctDMA::get_label ? Functions in C ++ are called using the () operator. And not AcctDMA::get_label , but rs.get_label() . You need to learn the basics of the language. Also: descriptions in the style "but the compiler swears" are useless here and are not interesting to anyone. Give a detailed description of what the compiler says. - AnT 2:19

0