覆盖操作符<<在c ++ [英] Overriding of operator<< in c++
问题描述
我正在使用C ++为我的学校制作一个专案
I'm working on a project for my school in C++
我有两个类别:Employe和Teacher。
从Employe派生的老师已覆盖其职能。
I have 2 class : Employe and Teacher. Teacher derived from Employe and has overrides of his functions.
我们覆盖运算符<<
打印雇员或教师的一些信息。
每个类都有一个 const int属性LevelAcces _
。
Employe 为 5 ,教师为 20 。
We override the operator <<
to print some information of the Employes or Teachers.
Each class has a const int attribute LevelAcces_
.
For Employe, it's 5 and for Teacher it's 20.
当我在main.cpp中创建一个Teacher时,我调用了operator<<的老师打印他的信息。
所以这个函数被调用:
when I create an Teacher in my main.cpp, I call the override of operator<< of Teacher to print his information. So this function is called :
ostream& operator<<(ostream& os, const Teacher& pTeacher){
os << (pTeacher);
return os;
}
但是,函数自身 os<<(pTeacher);
和会导致堆栈溢出的循环。
我想让行os
I want that the line "os << (pTeacher)"
calls the operator<< of my class Employe and not of my class Teacher.
覆盖操作符<< in Employe:
Override of operator<< in Employe :
ostream& operator<<(ostream& os, const Employe& pEmploye){
os << "Name: " << pEmploye.name_ << endl;
os << "Class: " << pEmploye.getClass() << endl;
os << "LevelAcces: " << pEmploye.getLevelAccess() << endl;
return os;
}
我试图将我的老师转为Employe,但是当它打印消息时,LevelAcces是5(我想要20,因为我的Employe是一个老师)。
I tried to cast my Teacher into Employe but when it prints the message, LevelAcces is 5 (and I want 20, because my Employe is a Teacher).
我也试图使用Employe :: operator<但是操作员<<不是Employe的成员,所以它不工作...
I also tried to use Employe::operator<< but operator<< is not a member of Employe so it doesn't work...
所以,这里是我的问题:
So, here is my question :
如何使用我的运算符<< of Employe in my operator<<
我也在想虚拟,但我们的教授告诉我们
I was also thinking of "virtual" but our professor tells us that there is not need to use this word.
提前感谢:)
这是一个更完整的代码:
Here is a more complete code :
main.cpp:
Teacher Garry("Garry");
cout << Garry << endl;
Employe.cpp:
Employe.cpp :
#include "Employe.h"
using namespace std;
Employe::Employe(){
name_ = "";
}
Employe::Employe(string pName){
name_ = pName;
}
string Employe::getName() const{
return name_;
}
unsigned int Employe::getLevelAccess() const{
return levelAccess_;
}
string Employe::getClass() const{
return typeid(*this).name();
}
ostream& operator<<(ostream& os, const Employe& pEmploye){
os << "Name: " << pEmploye.name_ << endl;
os << "Class: " << pEmploye.getClass() << endl;
os << "LevelAcces: " << pEmploye.getLevelAccess() << endl;
return os;
}
在Employe.h中:
With this in Employe.h :
private:
static const unsigned int LevelAccess_ = 5;
Teacher.cpp:
Teacher.cpp :
#include "teacher.h"
using namespace std;
Teacher::Teacher(string pName){
nom_ = pName;
}
unsigned int Teacher::getLevelAccess() const{
return(Employe::getLevelAccess() + accessTeacher_);
}
string Teacher::getClass() const{
return typeid(*this).name();
}
ostream& operator<<(ostream& os, const Teacher& pTeacher){
os << (pTeacher);
return os;
}
这是Teacher.h:
With this is Teacher.h :
static const unsigned int accesTeacher_ = 15;
推荐答案
我要做的是:define only一个
What I'd do is the following: define only one
ostream& operator<<(ostream& os, const Employe& pEmploye)
{
return pEmploye.display(os);
}
作为层次结构的基础,
在其中调用受每个派生类覆盖的受保护成员函数 virtual display()
,并且显示被委派给它。这有时称为NVI(非虚拟接口)成语。它的工作原理如下:
for the base of the hierarchy,
in which you call a protected member function virtual display()
that is overridden by each derived class and to which the display is being delegated. This is sometime call the NVI (non-virtual interface) idiom. It works like this:
class Employee
{
// ...
friend ostream& operator<<(ostream& os, const Employee& pEmployee)
{
return pEmployee.display(os);
}
protected:
virtual ostream& display(ostream& os) const
{
// implement it here
return os;
}
};
class Teacher: public Employee
{
// ....
protected:
ostream& display(ostream& os) const override
{
// implement it here
return os;
}
};
这篇关于覆盖操作符<<在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!