C++运算符重载

27 篇文章 1 订阅

这一篇文章作为笔记使用,所有的运算符重载都在代码中,有详细的注释

ps:代码可以直接运行

boy.h

#pragma once
#include <string>
#include <iostream>
#include <ostream>
#include <istream>

#define GET_AGE "age"
#define GET_SALARY "salary"
#define GET_POTENTIAL "potential"

typedef enum {
	getage,
	getsalary,
	getpotential
}cf;

using namespace std;

class boy {
public:
	boy(const char* name, int age, int salary, int potential);
	~boy();
	std::string description();

	boy(const boy& boy);	//拷贝构造函数
	boy& operator=(const boy& boy1); //赋值构造函数
	bool operator>(const boy& boy1);
	bool operator<(const boy& boy1);
	bool operator==(const boy& boy1);

	int operator[](std::string intdex) const;
	int operator[](int intdex) const;

	//使用类的成员函数作为<<重载运算符(不推荐)
	//ostream& operator<<(ostream& os) const;
	//istream& operator>>(istream& is);
	// 
	//使用友元函数作为<<重载运算符(推荐)
	friend ostream& operator<<(ostream& os, const boy& boys);
	friend istream& operator>>(istream& is, boy& boys);

	//重载类型运算符 普通类型 => 类类型
	boy(const int salary);
	boy(const char* name);

	//重载类型运算符 类类型 => 普通类型
	operator int() const;
	operator char* () const;
private:
	char* name;
	int age;
	int salary;
	int potential; //潜力

	int synthesize() const;

	unsigned int id;
	static int ID;
};

man.h

#pragma once
#include <ostream>

using namespace std;
class boy;

class man {

public:
	man(const char* name, int age, int salary);
	~man();

	man(const boy& boys);

	friend ostream& operator<<(ostream& os, man& mans);
private:
	char* name;
	int age;
	int salary;
};

ostream& operator<<(ostream& os, man& mans);

boy.cpp

#include "boy.h"
#include <sstream>
#include <string>
using namespace std;

int boy::ID = 0;

boy::boy(const char* name, int age, int salary, int potential)
{
	if (!name) {
		name = "未命名";
	}

	this->name = new char[strlen(name) + 1];
	strcpy_s(this->name, strlen(name) + 1, name);

	this->age = age;
	this->salary = salary;
	this->potential = potential;
	this->id = ++ID;
}

boy::boy(const int salary)
{
	this->salary = salary;

	const char* d1 = "未命名";
	name = new char[strlen(d1) + 1];
	strcpy_s(name, strlen(d1) + 1, d1);

	this->age = 0;
	this->potential = 0;
	this->id = ++ID;
}
boy::boy(const char* name) {
	this->name = new char[strlen(name) + 1];
	strcpy_s(this->name, strlen(name) + 1, name);

	this->age = 0;
	this->salary = 0;
	this->potential = 0;
	this->id = ++ID;
}

boy::operator int() const{
	return salary;
}

boy::operator char* () const{
	return name;
}

boy::boy(const boy& boy)
{
	if (name) {
		delete name;
	}
	name = new char[strlen(boy.name) + 1];
	strcpy_s(name, strlen(boy.name) + 1, boy.name);

	age = boy.age;
	salary = boy.salary;
	potential = boy.potential;

}


boy::~boy()
{
	if (name) {
		delete name;
	}
}

boy& boy::operator=(const boy& boy1) {
	if (name) {
		delete name;
	}
	name = new char[strlen(boy1.name) + 1];
	strcpy_s(name, strlen(boy1.name) + 1, boy1.name);

	this->age = boy1.age;
	this->salary = boy1.salary;
	this->potential = boy1.potential;
	return *this;
}

bool boy::operator>(const boy& boy1)
{
	if (synthesize() > boy1.synthesize()) {
		return true;
	}
	else {
		return false;
	}
}

bool boy::operator<(const boy& boy1)
{
	if (synthesize() < boy1.synthesize()) {
		return true;
	}
	else {
		return false;
	}
}

bool boy::operator==(const boy& boy1) {
	if (synthesize() == boy1.synthesize()) {
		return true;
	}
	else {
		false;
	}
}

int boy::operator[](string intdex) const
{
	if (intdex == "potential") {
		return potential;
	}
	else if (intdex == "age") {
		return age;
	}
	else if (intdex == "salary") {
		return salary;
	}
}
int boy::operator[](int intdex) const
{
	if (intdex == 0) {
		return age;
	}
	else if (intdex == 1) {
		return salary;
	}
	else if (intdex == 2) {
		return potential;
	}
}

//istream& boy::operator>>(istream& is)
//{
//	string name2;
//	is >> name2 >> age >> salary;
//	name = new char[(name2.length() + 1) / sizeof(char)];
//	//name = (char*)malloc((name2.length() + 1) / sizeof(char));
//	strcpy_s(name, (name2.length() + 1), name2.c_str());
//
//	return is;
//}

//ostream& boy::operator<<(ostream& os) const
//{
//	os << id << " 姓名:" << name << "\t" << "年纪:" << age << "\t" << "薪资:" << salary << "\t" << "潜力:" << potential;
//	return os;
//}

//ostream& boy::operator<<(ostream& os) const
//{
//	os << id << " 姓名:" << name << "\t" << "年纪:" << age << "\t" << "薪资:" << salary << "\t" << "潜力:" << potential;
//	return os;
//}

string boy::description() {
	stringstream ret;
	ret << id << " 姓名:" << name << "\t" << "年纪:" << age << "\t" << "薪资:" << salary << "\t" << "潜力:" << potential;
	return ret.str();
}

int boy::synthesize() const {
	int tmp = salary + ((age - 100) * 100) + (potential * 100);
	return tmp;
}


ostream& operator<<(ostream& os, const boy& boys) {
	os << boys.id << " 姓名:" << boys.name << "\t" << "年纪:" << boys.age << "\t" << "薪资:" << boys.salary << "\t" << "潜力:" << boys.potential;
	return os;
}

istream& operator>>(istream& is, boy& boys) {
	string name2;
	is >> name2 >> boys.age >> boys.salary;
	boys.name = new char[(name2.length() + 1) / sizeof(char)];
	strcpy_s(boys.name, name2.length() + 1, name2.c_str());

	return is;
}

man.cpp

#include "man.h"
#include "boy.h"
#include <iostream>

using namespace std;

man::man(const char* name, int gae, int salary) {
	if (!name) {
		name = "未命名";
	}
	name = new char[strlen(name) + 1];
	strcpy_s(this->name,strlen(name)+1,name);

	this->age = age;
	this->salary = salary;
}

man::~man()
{
	if(name){
		delete name;
	}
}

man::man(const boy& boys)
{
	int len = strlen((char*)boys) + 1;
	name = new char[len];
	strcpy_s(name,len,(char*)boys);

	this->age = boys[getage];
	this->salary = boys[getsalary];
}

ostream& operator<<(ostream& os, man& mans)
{
	os << "姓名:" << mans.name << "年龄:" << mans.age << "薪资:" << mans.salary;
	return os;
}

main.cpp

#include <iostream>
#include <Windows.h>
#include <string>
#include "boy.h"
#include "man.h"

using namespace std;
int main(void) {
	/*
		调用=赋值构造函数
	boy f1("胡",19,50000,80);
	boy f2;
	boy f3;
	f3 = f2 = f1;
	cout << f1.description() << endl;
	cout << f2.description() << endl;
	cout << f3.description() << endl;
	*/
	/*		调用>运算符重载
	boy s1("胡", 19, 60000, 75);
	boy s2("胡", 18, 50000, 80);

	if (s1 > s2) {
		cout << s1.description() << "更好" << endl;
	}
	else {
		cout << s2.description() << "更好" << endl;
	}
	*/
	/*		调用<运算符重载
	boy s1("胡", 19, 60000, 75);
	boy s2("胡", 18, 50000, 80);

	if (s1 < s2) {
		cout << s2.description() << "更好" << endl;
	}
	else {
		cout << s1.description() << "更好" << endl;
	}
	*/
	/*		调用==运算符重载
	boy s1("胡", 19, 60000, 80);
	boy s2("胡", 19, 60000, 80);

	if (s1 == s2) {
		cout << s1.description() << "和" << s2.description() << "一样好" << endl;
	}
	else if(s1>s2){
		cout << s1.description() << "更好" << endl;
	}
	else if (s1 < s2) {
		cout << s2.description() << "更好" << endl;
	}
	*/
	/*		使用[]下标运算符重载
	boy t1("胡", 19, 60000, 80);
	//使用[]下标字符串进+宏定义行对比访问
	cout << "年纪" << t1[GET_AGE] << "薪资:" << t1[GET_SALARY] << "潜力:" << t1[GET_POTENTIAL] << endl;
	//使用[]下标+枚举进行对比访问
	cout << "年纪" << t1[getage] << "薪资:" << t1[getsalary] << "潜力:" << t1[getpotential] << endl;
	*/
	/*
		//使用<< >>重载运算符
	boy s1("胡", 19, 60000, 75);
	boy s2("胡", 18, 50000, 80);

	//使用类成员函数作为<< >>重载运算符的调用方法

		<<重载运算符的使用方法
		s1<<cout;
		s2<<cout;
		>>重载运算符的使用方法
		s1<<cin;
		s2<<cin;
	
	cout << s1 << endl;
	cout << s2 << endl;

	cin >> s1;
	cin >> s2;

	cout << s1 << endl;
	cout << s2 << endl;
	*/ 
	//	重载类型运算符 普通类型 => 类类型
	boy boys1 = 100000;
	boy boys = "喜羊羊";

	cout << boys1 << endl;
	cout << boys << endl;

	//重载类型运算符 类类型 => 普通类型
	int salary = boys1;
	char* name = boys;

	cout << name << endl;
	cout << salary << endl;

	//重载类型运算符 类类型 => 类类型
	boy boy1("胡",19,100000,90);
	man man1 = boy1;

	cout << boy1 << endl;
	cout << man1 << endl;

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值