重载赋值运算符

Boy.h

#pragma once
#include <string>class Boy
{public:Boy(const char* name=NULL, int age=0, int salary=0, int darkHorse=0);~Boy();Boy& operator=(const Boy& boy);std::string description(void);
private:char* name;int age;int salary;int darkHorse; //黑马值,潜力系数unsigned int id; // 编号static int LAST_ID;
};

Boy.cpp

#include "boy.h"
#include <string.h>
#include <sstream>int Boy::LAST_ID = 0;  //初始值是0Boy::Boy(const char* name, int age, int salary, int darkHorse)
{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->darkHorse = darkHorse;this->id = ++LAST_ID;
}Boy::~Boy()
{if (name) {delete name;}
}// 注意返回类型 和参数类型
Boy& Boy::operator=(const Boy& boy)
{if (name) {delete name;  //释放原来的内存}name = new char[strlen(boy.name) + 1]; //分配新的内存strcpy_s(name, strlen(boy.name)+1, boy.name);this->age = boy.age;this->salary = boy.salary;this->darkHorse = boy.darkHorse;//this->id = boy.id;  //根据需求来确定是否要拷贝idreturn *this;
}std::string Boy::description(void)
{std::stringstream ret;ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"<< salary << "\t黑马系数:" << darkHorse;return ret.str();
}

main.cpp

#include <iostream>
#include "boy.h"int main(void) {Boy boy1("Rock", 38, 58000, 10);Boy boy2, boy3;std::cout << boy1.description() << std::endl;std::cout << boy2.description() << std::endl;std::cout << boy3.description() << std::endl;boy3 = boy2 = boy1;std::cout << boy2.description() << std::endl;std::cout << boy3.description() << std::endl;system("pause");return 0;
}

注意:
注意赋值运算符重载的返回类型 和参数类型。
返回引用类型,便于连续赋值
参数使用引用类型, 可以省去一次拷贝
参数使用const, 便于保护实参不被破坏。

重载关系运算符>、<、==

Boy.h

#pragma once
#include <string>class Boy
{public:Boy(const char* name=NULL, int age=0, int salary=0, int darkHorse=0);~Boy();Boy& operator=(const Boy& boy);bool operator>(const Boy& boy);bool operator<(const Boy& boy);bool operator==(const Boy& boy);std::string description(void);
private:char* name;int age;int salary;int darkHorse; //黑马值,潜力系数unsigned int id; // 编号static int LAST_ID;int power() const; //综合能力值
};

Boy.cpp

#include "boy.h"
#include <string.h>
#include <sstream>int Boy::LAST_ID = 0;  //初始值是0Boy::Boy(const char* name, int age, int salary, int darkHorse)
{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->darkHorse = darkHorse;this->id = ++LAST_ID;
}Boy::~Boy()
{if (name) {delete name;}
}Boy& Boy::operator=(const Boy& boy)
{if (name) {delete name;  //释放原来的内存}name = new char[strlen(boy.name) + 1]; //分配新的内存strcpy_s(name, strlen(boy.name)+1, boy.name);this->age = boy.age;this->salary = boy.salary;this->darkHorse = boy.darkHorse;//this->id = boy.id;  //根据需求来确定是否要拷贝idreturn *this;
}bool Boy::operator>(const Boy& boy)
{// 设置比较规则:// 薪资 * 黑马系数 + (100-年龄)*100if (power() > boy.power()) {return true;}else {return false;}
}bool Boy::operator<(const Boy& boy)
{if (power() < boy.power()) {return true;}else {return false;}
}bool Boy::operator==(const Boy& boy)
{if (power() == boy.power()) {return true;}else {return false;}
}std::string Boy::description(void)
{std::stringstream ret;ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"<< salary << "\t黑马系数:" << darkHorse;return ret.str();
}int Boy::power() const
{// 薪资* 黑马系数 + (100 - 年龄) * 1000int value = salary * darkHorse + (100 - age) * 100;return value;
}

main.cpp

#include <iostream>
#include "boy.h"int main(void) {Boy boy1("Rock", 38, 58000, 5);Boy boy2("Jack", 25, 50000, 10);if (boy1 > boy2) {std::cout << "选择boy1" << std::endl;}else if (boy1 == boy2) {std::cout << "难以选择" << std::endl;}else {std::cout << "选择boy2" << std::endl;}system("pause");return 0;
}

重载运算符[ ]

Boy.h

#pragma once
#include <string>class Boy
{public:Boy(const char* name=NULL, int age=0, int salary=0, int darkHorse=0);~Boy();Boy& operator=(const Boy& boy);bool operator>(const Boy& boy);bool operator<(const Boy& boy);bool operator==(const Boy& boy);int operator[](std::string index);int operator[](int index);std::string description(void);
private:char* name;int age;int salary;int darkHorse; //黑马值,潜力系数unsigned int id; // 编号static int LAST_ID;int power() const; //综合能力值
};

Boy.cpp

#include "boy.h"
#include <string.h>
#include <sstream>int Boy::LAST_ID = 0;  //初始值是0Boy::Boy(const char* name, int age, int salary, int darkHorse)
{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->darkHorse = darkHorse;this->id = ++LAST_ID;
}Boy::~Boy()
{if (name) {delete name;}
}Boy& Boy::operator=(const Boy& boy)
{if (name) {delete name;  //释放原来的内存}name = new char[strlen(boy.name) + 1]; //分配新的内存strcpy_s(name, strlen(boy.name)+1, boy.name);this->age = boy.age;this->salary = boy.salary;this->darkHorse = boy.darkHorse;//this->id = boy.id;  //根据需求来确定是否要拷贝idreturn *this;
}bool Boy::operator>(const Boy& boy)
{// 设置比较规则:// 薪资 * 黑马系数 + (100-年龄)*100if (power() > boy.power()) {return true;}else {return false;}
}bool Boy::operator<(const Boy& boy)
{if (power() < boy.power()) {return true;}else {return false;}
}bool Boy::operator==(const Boy& boy)
{if (power() == boy.power()) {return true;}else {return false;}
}int Boy::operator[](std::string index)
{if (index == "age") {return age;}else if (index == "salary") {return salary;}else if (index == "darkHorse") {return darkHorse;}else if (index == "power") {return power();}else {return -1;}
}int Boy::operator[](int index)
{if (index == 0) {return age;}else if (index == 1) {return salary;}else if (index == 2) {return darkHorse;}else if (index == 3) {return power();}else {return -1;}
}std::string Boy::description(void)
{std::stringstream ret;ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"<< salary << "\t黑马系数:" << darkHorse;return ret.str();
}int Boy::power() const
{// 薪资* 黑马系数 + (100 - 年龄) * 1000int value = salary * darkHorse + (100 - age) * 100;return value;
}

main.cpp

#include <iostream>
#include "boy.h"int main(void) {Boy boy1("Rock", 38, 58000, 5);Boy boy2("Jack", 25, 50000, 10);std::cout << "age:" << boy1["age"] << std::endl;std::cout << "salary:" << boy1["salary"] << std::endl;std::cout << "darkHorse:" << boy1["darkHorse"] << std::endl;std::cout << "power:" << boy1["power"] << std::endl;std::cout << "[0]:" << boy1[0] << std::endl;std::cout << "[1]:" << boy1[1] << std::endl;std::cout << "[2]:" << boy1[2] << std::endl;std::cout << "[3]:" << boy1[3] << std::endl;system("pause");return 0;
}

重载<<和>>

为什么要重载<< 和 >>
为了更方便的实现复杂对象的输入和输出。

方式1(使用成员函数, 不推荐,该方式没有实际意义)

Boy.h

#pragma once
#include <string>
#include <iostream>using namespace std;class Boy
{public:Boy(const char* name = NULL, int age = 0, int salary = 0, int darkHorse = 0);~Boy();Boy& operator=(const Boy& boy);bool operator>(const Boy& boy);bool operator<(const Boy& boy);bool operator==(const Boy& boy);int operator[](std::string index);int operator[](int index);ostream& operator<<(ostream& os) const;std::string description(void);private:char* name;int age;int salary;int darkHorse; //黑马值,潜力系数unsigned int id; // 编号static int LAST_ID;int power() const; //综合能力值
};

Boy.cpp

#include "boy.h"
#include <string.h>
#include <sstream>int Boy::LAST_ID = 0;  //初始值是0Boy::Boy(const char* name, int age, int salary, int darkHorse)
{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->darkHorse = darkHorse;this->id = ++LAST_ID;
}Boy::~Boy()
{if (name) {delete name;}
}Boy& Boy::operator=(const Boy& boy)
{if (name) {delete name;  //释放原来的内存}name = new char[strlen(boy.name) + 1]; //分配新的内存strcpy_s(name, strlen(boy.name) + 1, boy.name);this->age = boy.age;this->salary = boy.salary;this->darkHorse = boy.darkHorse;//this->id = boy.id;  //根据需求来确定是否要拷贝idreturn *this;
}bool Boy::operator>(const Boy& boy)
{// 设置比较规则:// 薪资 * 黑马系数 + (100-年龄)*100if (power() > boy.power()) {return true;}else {return false;}
}bool Boy::operator<(const Boy& boy)
{if (power() < boy.power()) {return true;}else {return false;}
}bool Boy::operator==(const Boy& boy)
{if (power() == boy.power()) {return true;}else {return false;}
}int Boy::operator[](std::string index)
{if (index == "age") {return age;}else if (index == "salary") {return salary;}else if (index == "darkHorse") {return darkHorse;}else if (index == "power") {return power();}else {return -1;}
}int Boy::operator[](int index)
{if (index == 0) {return age;}else if (index == 1) {return salary;}else if (index == 2) {return darkHorse;}else if (index == 3) {return power();}else {return -1;}
}ostream& Boy::operator<<(ostream& os) const
{os << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"<< salary << "\t黑马系数:" << darkHorse;return os;
}std::string Boy::description(void)
{std::stringstream ret;ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"<< salary << "\t黑马系数:" << darkHorse;return ret.str();
}int Boy::power() const
{// 薪资* 黑马系数 + (100 - 年龄) * 1000int value = salary * darkHorse + (100 - age) * 100;return value;
}

main.cpp

#include <iostream>
#include "boy.h"int main(void) {Boy boy1("Rock", 38, 58000, 5);Boy boy2("Jack", 25, 50000, 10);// 调用: boy1.operator<<(cout);boy1 << cout;// 先调用 boy1.operator<<(cout)// 再调用 boy2.operator<<(cout)boy2 << (boy1 << cout);system("pause");return 0;
}

这样大家会发现很不习惯,平时我们的cout都是放到前面的,现在放在后面用起来超级不方便,所以成员函数对<<的重载不推荐使用,一点都不方便,所以我们一起来看看第二种方式。

方式2:使用友元函数

Boy.h

#pragma once
#include <string>
#include <iostream>#define AGE_KEY            "age"
#define SALARY_KEY      "salary"
#define DARK_HORSE_KEY  "darkHorse"
#define POWER_KEY       "power"typedef enum {AGE,SALARY,DARK_HORSE,POWER
}BOY_KEY_TYPE;using namespace std;class Boy
{public:Boy(const char* name = NULL, int age = 0, int salary = 0, int darkHorse = 0);~Boy();Boy& operator=(const Boy& boy);bool operator>(const Boy& boy);bool operator<(const Boy& boy);bool operator==(const Boy& boy);// 下标运算符的重载int operator[](std::string index);int operator[](int index);// 该方式不适合//ostream& operator<<(ostream& os) const;friend ostream& operator<<(ostream& os, const Boy& boy);friend istream& operator>>(istream& is, Boy& boy);std::string description(void);
private:char* name;int age;int salary;int darkHorse; //黑马值,潜力系数unsigned int id; // 编号static int LAST_ID;int power() const; //综合能力值
};

Boy.cpp 【不变】

#include "boy.h"
#include <string.h>
#include <sstream>int Boy::LAST_ID = 0;  //初始值是0Boy::Boy(const char* name, int age, int salary, int darkHorse)
{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->darkHorse = darkHorse;this->id = ++LAST_ID;
}Boy::~Boy()
{if (name) {delete name;}
}Boy& Boy::operator=(const Boy& boy)
{if (name) {delete name;  //释放原来的内存}name = new char[strlen(boy.name) + 1]; //分配新的内存strcpy_s(name, strlen(boy.name) + 1, boy.name);this->age = boy.age;this->salary = boy.salary;this->darkHorse = boy.darkHorse;//this->id = boy.id;  //根据需求来确定是否要拷贝idreturn *this;
}bool Boy::operator>(const Boy& boy)
{// 设置比较规则:// 薪资 * 黑马系数 + (100-年龄)*100if (power() > boy.power()) {return true;}else {return false;}
}bool Boy::operator<(const Boy& boy)
{if (power() < boy.power()) {return true;}else {return false;}
}bool Boy::operator==(const Boy& boy)
{if (power() == boy.power()) {return true;}else {return false;}
}int Boy::operator[](std::string index)
{if (index == AGE_KEY) {return age;}else if (index == SALARY_KEY) {return salary;}else if (index == DARK_HORSE_KEY) {return darkHorse;}else if (index == POWER_KEY) {return power();}else {return -1;}
}int Boy::operator[](int index)
{if (index == 0) {return age;}else if (index == 1) {return salary;}else if (index == 2) {return darkHorse;}else if (index == 3) {return power();}else {return -1;}
}//ostream& Boy::operator<<(ostream& os) const
//{//  os << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"
//      << salary << "\t黑马系数:" << darkHorse;
//  return os;
//}std::string Boy::description(void)
{std::stringstream ret;ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"<< salary << "\t黑马系数:" << darkHorse;return ret.str();
}int Boy::power() const
{// 薪资* 黑马系数 + (100 - 年龄) * 1000int value = salary * darkHorse + (100 - age) * 100;return value;
}

main.cpp

#include <iostream>
#include "Boy.h"using namespace std;ostream& operator<<(ostream& os, const Boy& boy) {os << "ID:" << boy.id << "\t姓名:" << boy.name << "\t年龄:" << boy.age << "\t薪资:"<< boy.salary << "\t黑马系数:" << boy.darkHorse;return os;
}istream& operator>>(istream& is, Boy& boy)
{string name2;is >> name2 >> boy.age >> boy.salary >> boy.darkHorse;boy.name = (char*)malloc((name2.length()+1) * sizeof(char));strcpy_s(boy.name, name2.length() + 1, name2.c_str());return is;
}int main(void) {Boy boy1("Rock", 38, 58000, 5);Boy boy2("Jack", 25, 50000, 10);cout << boy1 << endl;cin >> boy1;cout << boy1;system("pause");return 0;
}

重载类型运算符

普通类型 => 类类型
调用对应的只有一个参数【参数的类型就是这个普通类型】的构造函数

Boy.h

/*
需求:Boy boy1 = 10000;  // 薪资   构造函数Boy(int);Boy boy2 = "Rock"  // 姓名   构造函数Boy(char *);
*/#pragma once
#include <string>
#include <iostream>#define AGE_KEY            "age"
#define SALARY_KEY      "salary"
#define DARK_HORSE_KEY  "darkHorse"
#define POWER_KEY       "power"typedef enum {AGE,SALARY,DARK_HORSE,POWER
}BOY_KEY_TYPE;using namespace std;class Boy
{public://Boy(const char* name = NULL, int age = 0, int salary = 0, int darkHorse = 0);Boy(const char* name, int age, int salary, int darkHorse);~Boy();Boy(int salary);Boy(const char* name);Boy& operator=(const Boy& boy);bool operator>(const Boy& boy);bool operator<(const Boy& boy);bool operator==(const Boy& boy);// 下标运算符的重载int operator[](std::string index);int operator[](int index);// 该方式不适合//ostream& operator<<(ostream& os) const;friend ostream& operator<<(ostream& os, const Boy& boy);friend istream& operator>>(istream& is, Boy& boy);std::string description(void);
private:char* name;int age;int salary;int darkHorse; //黑马值,潜力系数unsigned int id; // 编号static int LAST_ID;int power() const; //综合能力值
};

Boy.cpp

#include "boy.h"
#include <string.h>
#include <sstream>int Boy::LAST_ID = 0;  //初始值是0Boy::Boy(const char* name, int age, int salary, int darkHorse)
{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->darkHorse = darkHorse;this->id = ++LAST_ID;
}Boy::~Boy()
{if (name) {delete name;}
}Boy::Boy(int salary)
{const char *defaultName = "未命名";name = new char[strlen(defaultName) + 1];strcpy_s(name, strlen(defaultName) + 1, defaultName);age = 0;this->salary = salary;darkHorse = 0;this->id = ++LAST_ID;
}Boy::Boy(const char* name) {this->name = new char[strlen(name) + 1];strcpy_s(this->name, strlen(name) + 1, name);age = 0;this->salary = 0;darkHorse = 0;this->id = ++LAST_ID;
}Boy& Boy::operator=(const Boy& boy)
{if (name) {delete name;  //释放原来的内存}name = new char[strlen(boy.name) + 1]; //分配新的内存strcpy_s(name, strlen(boy.name) + 1, boy.name);this->age = boy.age;this->salary = boy.salary;this->darkHorse = boy.darkHorse;//this->id = boy.id;  //根据需求来确定是否要拷贝idreturn *this;
}bool Boy::operator>(const Boy& boy)
{// 设置比较规则:// 薪资 * 黑马系数 + (100-年龄)*100if (power() > boy.power()) {return true;}else {return false;}
}bool Boy::operator<(const Boy& boy)
{if (power() < boy.power()) {return true;}else {return false;}
}bool Boy::operator==(const Boy& boy)
{if (power() == boy.power()) {return true;}else {return false;}
}int Boy::operator[](std::string index)
{if (index == AGE_KEY) {return age;}else if (index == SALARY_KEY) {return salary;}else if (index == DARK_HORSE_KEY) {return darkHorse;}else if (index == POWER_KEY) {return power();}else {return -1;}
}int Boy::operator[](int index)
{if (index == 0) {return age;}else if (index == 1) {return salary;}else if (index == 2) {return darkHorse;}else if (index == 3) {return power();}else {return -1;}
}std::string Boy::description(void)
{std::stringstream ret;ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"<< salary << "\t黑马系数:" << darkHorse;return ret.str();
}int Boy::power() const
{// 薪资* 黑马系数 + (100 - 年龄) * 1000int value = salary * darkHorse + (100 - age) * 100;return value;
}

main.cpp

#include <iostream>
#include "Boy.h"using namespace std;ostream& operator<<(ostream& os, const Boy& boy) {os << "ID:" << boy.id << "\t姓名:" << boy.name << "\t年龄:" << boy.age << "\t薪资:"<< boy.salary << "\t黑马系数:" << boy.darkHorse;return os;
}istream& operator>>(istream& is, Boy& boy)
{string name2;is >> name2 >> boy.age >> boy.salary >> boy.darkHorse;boy.name = (char*)malloc((name2.length() + 1) * sizeof(char));strcpy_s(boy.name, name2.length() + 1, name2.c_str());return is;
}int main()
{Boy boy1 = 10000;Boy boy2 = "Rock";cout << boy1 << endl;cout << boy2 << endl;boy1 = 20000; //boy1 = Boy(20000);cout << boy1 << endl;return 0;
}


类类型 => 普通类型
调用特殊的运算符重载函数,类型转换函数,不需要写返回类型
类型转换函数:operator 普通类型 ( )

Boy.h

/*需求:Boy boy1(“Rock”, 28, 10000, 5);int  power = boy1;   // power();char *name = boy1;   // “Rock”
*/#pragma once
#include <string>
#include <iostream>#define AGE_KEY            "age"
#define SALARY_KEY      "salary"
#define DARK_HORSE_KEY  "darkHorse"
#define POWER_KEY       "power"typedef enum {AGE,SALARY,DARK_HORSE,POWER
}BOY_KEY_TYPE;using namespace std;class Boy
{public://Boy(const char* name = NULL, int age = 0, int salary = 0, int darkHorse = 0);Boy(const char* name, int age, int salary, int darkHorse);~Boy();Boy(int salary);Boy(const char* name);Boy& operator=(const Boy& boy);bool operator>(const Boy& boy);bool operator<(const Boy& boy);bool operator==(const Boy& boy);// 下标运算符的重载int operator[](std::string index);int operator[](int index);// 该方式不适合//ostream& operator<<(ostream& os) const;friend ostream& operator<<(ostream& os, const Boy& boy);friend istream& operator>>(istream& is, Boy& boy);// 特殊的运算符重载:类型转换函数,不需要写返回类型operator int() const;operator char* () const;std::string description(void);
private:char* name;int age;int salary;int darkHorse; //黑马值,潜力系数unsigned int id; // 编号static int LAST_ID;int power() const; //综合能力值
};

Boy.cpp

#include "boy.h"
#include <string.h>
#include <sstream>int Boy::LAST_ID = 0;  //初始值是0Boy::Boy(const char* name, int age, int salary, int darkHorse)
{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->darkHorse = darkHorse;this->id = ++LAST_ID;
}Boy::~Boy()
{if (name) {delete name;}
}Boy::Boy(int salary)
{const char *defaultName = "未命名";name = new char[strlen(defaultName) + 1];strcpy_s(name, strlen(defaultName) + 1, defaultName);age = 0;this->salary = salary;darkHorse = 0;this->id = ++LAST_ID;
}Boy::Boy(const char* name) {this->name = new char[strlen(name) + 1];strcpy_s(this->name, strlen(name) + 1, name);age = 0;this->salary = 0;darkHorse = 0;this->id = ++LAST_ID;
}Boy& Boy::operator=(const Boy& boy)
{if (name) {delete name;  //释放原来的内存}name = new char[strlen(boy.name) + 1]; //分配新的内存strcpy_s(name, strlen(boy.name) + 1, boy.name);this->age = boy.age;this->salary = boy.salary;this->darkHorse = boy.darkHorse;//this->id = boy.id;  //根据需求来确定是否要拷贝idreturn *this;
}bool Boy::operator>(const Boy& boy)
{// 设置比较规则:// 薪资 * 黑马系数 + (100-年龄)*100if (power() > boy.power()) {return true;}else {return false;}
}bool Boy::operator<(const Boy& boy)
{if (power() < boy.power()) {return true;}else {return false;}
}bool Boy::operator==(const Boy& boy)
{if (power() == boy.power()) {return true;}else {return false;}
}int Boy::operator[](std::string index)
{if (index == AGE_KEY) {return age;}else if (index == SALARY_KEY) {return salary;}else if (index == DARK_HORSE_KEY) {return darkHorse;}else if (index == POWER_KEY) {return power();}else {return -1;}
}int Boy::operator[](int index)
{if (index == 0) {return age;}else if (index == 1) {return salary;}else if (index == 2) {return darkHorse;}else if (index == 3) {return power();}else {return -1;}
}Boy::operator int() const
{return power();
}Boy::operator char* () const
{return name;
}std::string Boy::description(void)
{std::stringstream ret;ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"<< salary << "\t黑马系数:" << darkHorse;return ret.str();
}int Boy::power() const
{// 薪资* 黑马系数 + (100 - 年龄) * 1000int value = salary * darkHorse + (100 - age) * 100;return value;
}

main.cpp

#include <iostream>
#include "Boy.h"using namespace std;ostream& operator<<(ostream& os, const Boy& boy) {os << "ID:" << boy.id << "\t姓名:" << boy.name << "\t年龄:" << boy.age << "\t薪资:"<< boy.salary << "\t黑马系数:" << boy.darkHorse;return os;
}istream& operator>>(istream& is, Boy& boy)
{string name2;is >> name2 >> boy.age >> boy.salary >> boy.darkHorse;boy.name = (char*)malloc((name2.length() + 1) * sizeof(char));strcpy_s(boy.name, name2.length() + 1, name2.c_str());return is;
}int main()
{Boy boy1("Rock", 28, 10000, 5);Boy boy2("Rock");int power = boy1;char* name = boy2;cout << power << endl;cout << name << endl;system("pause");return 0;
}



类类型A => 类类型B
调用对应的只有一个参数【参数的类型就是类类型A】的构造函数
也可以使用类型转换函数,但是使用对应的构造函数更合适。

Boy.h

//把Boy类型,转换为Man类型
#pragma once
#include <string>
#include <iostream>#define AGE_KEY            "age"
#define SALARY_KEY      "salary"
#define DARK_HORSE_KEY  "darkHorse"
#define POWER_KEY       "power"typedef enum {AGE,SALARY,DARK_HORSE,POWER
}BOY_KEY_TYPE;using namespace std;class Boy
{public://Boy(const char* name = NULL, int age = 0, int salary = 0, int darkHorse = 0);Boy(const char* name, int age, int salary, int darkHorse);~Boy();Boy(int salary);Boy(const char* name);Boy& operator=(const Boy& boy);bool operator>(const Boy& boy);bool operator<(const Boy& boy);bool operator==(const Boy& boy);// 下标运算符的重载int operator[](std::string index) const;int operator[](int index) const;// 该方式不适合//ostream& operator<<(ostream& os) const;friend ostream& operator<<(ostream& os, const Boy& boy);friend istream& operator>>(istream& is, Boy& boy);// 特殊的运算符重载:类型转换函数,不需要写返回类型operator int() const;operator char* () const;std::string description(void);
private:char* name;int age;int salary;int darkHorse; //黑马值,潜力系数unsigned int id; // 编号static int LAST_ID;int power() const; //综合能力值
};ostream& operator<<(ostream& os, const Boy& boy);
istream& operator>>(istream& is, Boy& boy);

Boy.cpp

#include "boy.h"
#include <string.h>
#include <sstream>int Boy::LAST_ID = 0;  //初始值是0Boy::Boy(const char* name, int age, int salary, int darkHorse)
{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->darkHorse = darkHorse;this->id = ++LAST_ID;
}Boy::~Boy()
{if (name) {delete name;}
}Boy::Boy(int salary)
{const char *defaultName = "未命名";name = new char[strlen(defaultName) + 1];strcpy_s(name, strlen(defaultName) + 1, defaultName);age = 0;this->salary = salary;darkHorse = 0;this->id = ++LAST_ID;
}Boy::Boy(const char* name) {this->name = new char[strlen(name) + 1];strcpy_s(this->name, strlen(name) + 1, name);age = 0;this->salary = 0;darkHorse = 0;this->id = ++LAST_ID;
}Boy& Boy::operator=(const Boy& boy)
{if (name) {delete name;  //释放原来的内存}name = new char[strlen(boy.name) + 1]; //分配新的内存strcpy_s(name, strlen(boy.name) + 1, boy.name);this->age = boy.age;this->salary = boy.salary;this->darkHorse = boy.darkHorse;//this->id = boy.id;  //根据需求来确定是否要拷贝idreturn *this;
}bool Boy::operator>(const Boy& boy)
{// 设置比较规则:// 薪资 * 黑马系数 + (100-年龄)*100if (power() > boy.power()) {return true;}else {return false;}
}bool Boy::operator<(const Boy& boy)
{if (power() < boy.power()) {return true;}else {return false;}
}bool Boy::operator==(const Boy& boy)
{if (power() == boy.power()) {return true;}else {return false;}
}int Boy::operator[](std::string index) const
{if (index == AGE_KEY) {return age;}else if (index == SALARY_KEY) {return salary;}else if (index == DARK_HORSE_KEY) {return darkHorse;}else if (index == POWER_KEY) {return power();}else {return -1;}
}int Boy::operator[](int index)const
{if (index == 0) {return age;}else if (index == 1) {return salary;}else if (index == 2) {return darkHorse;}else if (index == 3) {return power();}else {return -1;}
}Boy::operator char* () const
{return name;
}std::string Boy::description(void)
{std::stringstream ret;ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"<< salary << "\t黑马系数:" << darkHorse;return ret.str();
}int Boy::power() const
{// 薪资* 黑马系数 + (100 - 年龄) * 1000int value = salary * darkHorse + (100 - age) * 100;return value;
}ostream& operator<<(ostream& os, const Boy& boy) {os << "ID:" << boy.id << "\t姓名:" << boy.name << "\t年龄:" << boy.age << "\t薪资:"<< boy.salary << "\t黑马系数:" << boy.darkHorse;return os;
}istream& operator>>(istream& is, Boy& boy)
{string name2;is >> name2 >> boy.age >> boy.salary >> boy.darkHorse;boy.name = (char*)malloc((name2.length() + 1) * sizeof(char));strcpy_s(boy.name, name2.length() + 1, name2.c_str());return is;
}

Man.h

#pragma once
#include <iostream>using namespace std;class Boy;class Man
{public:Man(const char *name, int age, int salary);Man(const Boy& boy);~Man();friend ostream& operator<<(ostream &os, const Man& man);
private:char* name;int age;int salary;
};ostream& operator<<(ostream &os, const Man& man);

Man.cpp

#include "Man.h"
#include "Boy.h"
#include <string.h>Man::Man(const char* name, int age, int salary)
{if (!name) {name = "未命名";}this->name = new char[strlen(name) + 1];strcpy_s(this->name, strlen(name) + 1, name);this->age = age;this->salary = salary;
}Man::Man(const Boy& boy)
{int len = strlen((char*)boy) + 1;name = new char[len];strcpy_s(name, len, (char*)boy);age = boy[AGE];salary = boy[SALARY];
}Man::~Man() {delete name;
}ostream& operator<<(ostream &os, const Man& man) {os  << "【男人】姓名:" << man.name<< "\t年龄 : " << man.age<< "\t薪资 : " << man.salary;return os;
}

main.cpp

#include <iostream>
#include "Boy.h"
#include "Man.h"using namespace std;int main()
{Boy boy("Rock", 28, 10000, 5);Man man = boy;cout << boy << endl;cout << man << endl;system("pause");return 0;
}

小结:
说白了,类类型的转换就是通过构造函数来完成的。

万物可运算——运算符重载(四)相关推荐

  1. C++基础之运算符重载

    前言 一.友元 二.运算符重载 三.特殊运算符重载 四.建议 前言 个人学习笔记 一.友元 一般来说,类的私有成员只能在类的内部访问,类之外是不能访问它们的.但如果将其他类或函数设置为类的友元(fri ...

  2. 【C++实验】运算符重载(两个矩阵相加)

    运算符重载 运算符重载概念 对已有的运算符赋予新的含义,用一个运算符表示不同功能的运算,从而适用于用户自定义类型的数据(比如复数.矩阵等)之间的运算 运算符重载方法 定义一个重载运算符函数,在需要时系 ...

  3. C++学习笔记:(四)运算符重载 类型转换

    目录 6.运算符重载 6.1运算符重载的基本概念 6.2成员函数重载运算符 6.3友元函数重载运算符 6.4成员函数重载运算符和友元函数重载运算符比较 6.5类型转换 6.运算符重载 面向对象程序设计 ...

  4. c++中运算符重载(加号运算,左移运算,前置后置++运算符,赋值运算,关系运算,函数运算)

    运算符重载注意 重载的运算符要易读 内置的数据类型的表达式的运算符是不可以改变的 不要重载&& 和 | | 运算符 =,[]和->运算符只能通过成员函数进行重载 << ...

  5. python次方运算_neg__python 魔术方法1 运算符重载

    python中存在一些特殊的方法,这些方法通常采用格式:__method__().这些方法会在特定的情况下自动调用.例如:__new__().__init__().__del__() 等生命周期方法. ...

  6. C++复数的运算、运算符重载

    文章目录 一. 复数的运算 二.利用重载运算符+来实现复数的加法运算 一. 复数的运算 1.算法详解[在注释中] #include <iostream> using namespace s ...

  7. C#编程(四十)----------运算符重载

    运算符重载 所谓的运算符重载是指允许用户使用用户定义的类型编写表达式的能力. 例如,通常需要编写类似与以下内容的代码,入江两个数字相加,很明显,sum是两个数字之和. int i=5,j=4; int ...

  8. 新标准C++(郭炜)第四章细节问题小结(1):运算符重载(一)

    一.运算符重载的概念和原理(P65-P66) 运算符重载的目的:使得C++中的运算符也能用来操作对象. ---------------->运算符重载的实质是编写以运算符作为名称的函数 运算符函数 ...

  9. 定义分数类中和运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

    /* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:main.cpp *作    者:李德彪 *完成时间:2016年6月15日 * ...

  10. C++运算符重载(复数中除法(“/”)数学运算公式)

    假设x为实部,y为虚部. 定义复数A,复数B,复数t: t.x=((A.x*B.x)+(A.y*B.y))/((B.x*B.x)+(B.y*B.y));     t.y=((A.y*B.x)-(A.x ...

最新文章

  1. 一周最新示例代码回顾 (5/7–5/13)
  2. python游戏编程书籍-《Python游戏编程快速上手》一1.3 如何使用本书
  3. php get 返回源码,php源码 fsockopen获取网页内容实例详解
  4. (转)TinyXML Tutorial 中文指南
  5. yota3墨水屏设置_汉阳环卫工节前给道路隔音屏“洗澡”
  6. 中国农业大学营养与健康研究院诚聘博士后
  7. Classification Example
  8. [ES6] 细化ES6之 -- Class关键字
  9. java的可变参数介绍_Java基础 可变参数介绍(转载)
  10. 如何解决远程windows服务器安装matlab出现License Manager Error-103问题
  11. Pr入门系列之五:熟悉时间轴操作
  12. mysql二级软件_全国计算机等级考试二级MySQL练习软件
  13. 01 #pragma once用法总结
  14. 辩证的看待IDE工具(Java与Python学习通法)
  15. 平行空间怎么设置32位_10月微信新花样!微信情侣空间怎么解除 情侣空间取消情侣关系设置方法...
  16. windows日志捕获工具-DebugView使用教程
  17. kali linux怎样下载全部工具,Kali Linux工具大全
  18. MTK平台 获取本机的SIM卡中IMSI号
  19. 江西省九江市瑞昌市高考成绩查询2021,2021九江重点高中学校排名榜
  20. IT审计质量控制评价和改进实践

热门文章

  1. FZU1892接水管游戏-BFS加上简单的状态压缩和位运算处理
  2. 【Matlab图像处理】自动报靶系统(重弹孔)【含GUI源码 973期】
  3. 高频故障-桌面图标变成白纸图标的恢复方案
  4. TFP-161/100/6MM/6MM/MPU
  5. mysql添加索引报错1170 -BLOB/TEXT column ‘xx‘ used in key specification without a key length分析及解决
  6. smartforms 黑底白字的标签logo制作
  7. Apache commons-exec的使用
  8. 【CSS】笔记4-浮动、切图、学成在线
  9. 在VB中如何使IE窗口最大化
  10. 获取ie窗口的IHTMLDocument2对象