C++——操作符重载

                类的操作符重载 :关键字operator再加上需要被重载的操作符,如下代码实现将两个Emploee类的对象进行相加。

                

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
using std::string;

class Emploee
{
public:
Emploee(string name,int age);
Emploee();
~Emploee();
Emploee operator+(const Emploee& temp)
{
Emploee result;
result.m_sname = this->m_sname + temp.m_sname;
result.m_nage = this->m_nage + temp.m_nage;
return result;
}
int getEmploeeage();
string getEmploeename();
private:
string m_sname;
int m_nage;
};

Emploee::Emploee(string name,int age)
{
m_sname = name;
m_nage = age;
}
Emploee::Emploee()
{
}
Emploee::~Emploee()
{
}

int Emploee::getEmploeeage()
{
return m_nage;
}
string Emploee::getEmploeename()
{
return m_sname;
}

int main()
{
Emploee *yuan = new Emploee("yuan",10);
Emploee *song = new Emploee("song",12);
Emploee result;
result = *yuan + *song;
cout << result.getEmploeeage() << "\n" << result.getEmploeename() << endl;
delete(yuan);
delete(song);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值