c语言operator,在c中重写operator <<

我正在为我在C的学校做一个项目

我有2节课:Employe和Teacher.

源自Employe的老师,其职能已被覆盖.

我们覆盖运算符

对于Employe,是5,对于Teacher,是20.

当我在main.cpp中创建教师时,我调用了operator

ostream& operator<

os << (pTeacher);

return os;

}

但是,该函数使用“ os

os << "Name: " << pEmploye.name_ << endl;

os << "Class: " << pEmploye.getClass() << endl;

os << "LevelAcces: " << pEmploye.getLevelAccess() << endl;

return os;

}

我试图将我的老师放到Employe中,但是当它显示消息时,LevelAcces是5(我想是20,因为我的Employe是一位老师).

我还尝试使用Employe :: operator

cout << Garry << endl;

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<

os << "Name: " << pEmploye.name_ << endl;

os << "Class: " << pEmploye.getClass() << endl;

os << "LevelAcces: " << pEmploye.getLevelAccess() << endl;

return os;

}

在Employe.h中:

private:

static const unsigned int LevelAccess_ = 5;

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<

os << (pTeacher);

return os;

}

这是Teacher.h:

static const unsigned int accesTeacher_ = 15;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值