重载运算符 *笔记7

普通类型 => 类类型

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

 

需求:
    Boy boy1 = 10000;  // 薪资   构造函数Boy(int);

    Boy boy2 = "Rock"  // 姓名   构造函数Boy(char *);

 

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(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;  //初始值是0

 

Boy::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;  //根据需求来确定是否要拷贝id

    return *this;

}

 

bool Boy::operator>(const Boy& boy)

{

    // 设置比较规则:

    // 薪资 * 黑马系数 + (100-年龄)*100

    if (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 - 年龄) * 1000

    int 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 boy1(“Rock”, 28, 10000, 5);

int  power = boy1;   // power();

char *name = boy1;   // “Rock”

 

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(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;  //初始值是0

 

Boy::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;  //根据需求来确定是否要拷贝id

    return *this;

}

 

bool Boy::operator>(const Boy& boy)

{

    // 设置比较规则:

    // 薪资 * 黑马系数 + (100-年龄)*100

    if (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 - 年龄) * 1000

    int 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类型,转换为Man类型

 

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(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;  //初始值是0

 

Boy::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;  //根据需求来确定是否要拷贝id

    return *this;

}

 

bool Boy::operator>(const Boy& boy)

{

    // 设置比较规则:

    // 薪资 * 黑马系数 + (100-年龄)*100

    if (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 - 年龄) * 1000

    int 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;

}

posted @ 2020-06-20 21:23  CollisionDimension  阅读(90)  评论(0)    收藏  举报