栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++运算符重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++运算符重载

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

ps:代码可以直接运行

boy.h

#pragma once
#include 
#include 
#include 
#include 

#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 

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 
#include 
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 

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 
#include 
#include 
#include "boy.h"
#include "man.h"

using namespace std;
int main(void) {
	
	
	
	
	
	 
	//	重载类型运算符 普通类型 => 类类型
	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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/982398.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号