类型转换

普通类型转换

实现 a = 1000;自动转换为类成员 salary = 1000
a = “lemn”; 自动转换成类成员 name = “lemn”

以类型转换得角度来看构造函数

	Boy(int age);
	Boy(const char* name);
Boy::Boy(int age) {
	const char* defaultName = "未命名";		//先定义一个新变量存储未命名这个值,因为原先定义的name是const类型
	name = new char[strlen(defaultName)+1];	//位原先的name开辟新的空间
	strcpy_s(name, strlen(defaultName) + 1, defaultName);//把"未命名"复制进去

	this->age = age;
	salary = 0;
	darkHorse = 0;
	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;
	salary = 0;
	darkHorse = 0;
	id = ++LAST_ID;

}

int main(void) {
	
	Boy boy1 = 1000;
	Boy boy2 = "rock";

	cout << boy1 << endl;
	cout << boy2 << endl;
	system("pause");
	return 0;
}

类类型 == > 普通类型

调用特殊的运算符重载函数,类型转换函数,不需要写返回类型

类型转换函数: operator 普通类型();

需求:
Boy boy1(“lemn”,20,20000,9);
int power = boy1 //相当于int power = boy1.power();
char *name = boy1;

//类类型 ==> 普通类型 
	//类型运算符的重载,不需要返回类型
	operator int() const;
	operator char* () const;

	char* name;

Boy::operator int()const {
	return power();
}

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


int main(void) {
	
	Boy boy1("lemn", 20, 20000, 9);
	int power = boy1;
	char* name = boy1;

	cout << boy1 << endl;
	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 "age"
#define SALARY "salary"
#define DARKHORSE "darkHorse"
#define POWER "power"

typedef enum {
	AGE_KEY,
	SALARY_KEY,
	DARKHORSE_KEY,
	POWER_KEY
}BOY_KEY_TYPE;

using namespace std;

class Boy
{
public:
	Boy(const char* name, int age, int salary, int darkHouse);
	~Boy();

	//重载赋值运算符
	Boy& operator=(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, Boy& boy);

	//输入运算符重载
	friend istream& operator>>(istream& is, Boy& boy);

	//实现 boy1 = 1000	boy2 = "rock"	类似构造函数
	Boy(const int age);
	Boy(const char* name);

	//类类型 ==> 普通类型 
	//类型运算符的重载,不需要返回类型
	operator int() const;
	operator char* () const;

	

private:
	char* name;
	int age;
	unsigned int id;
	static int LAST_ID;	//LAST_ID = 0 非法
	int salary;
	int darkHorse;//黑马系数
	int power() const;		//综合实力,内部调用
};

istream& operator>>(istream& is, Boy& boy);
ostream& operator<<(ostream& os, Boy& boy);

Boy.cpp

#include "Boy.h"
#include <sstream>

int Boy::LAST_ID = 0;


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

	this->name = new char[strlen(name) + 1]; //分配空间,+1 是字符串结束符
	strcpy_s(this->name, strlen(name) + 1, name);

	this->age = age;
	this->salary = salary;
	this->darkHorse = darkHouse;
	this->id = ++LAST_ID;
}

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

//返回当前对象的引用
Boy& Boy::operator=(Boy& boy) {
	if (name) {
		delete name;	//释放原来内存
	}
	name = new char[strlen(boy.name) + 1];
	strcpy_s(this->name, strlen(boy.name) + 1, boy.name);

	this->age = boy.age;
	this->salary = boy.salary;
	this->darkHorse = boy.darkHorse;
	//this->id = boy.id		取决于需求

	return *this;
}



//std::string Boy::description() {
//	std::stringstream ret;
//	ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age
//		<< "\t薪资:" << salary << "\t黑马系数:" << darkHorse;
//
//	return ret.str();
//}


bool Boy::operator<(const Boy& boy) {
	//设置比较规则
	//薪资 * 黑马系数 + (100-年龄) * 1000
	if (power() < boy.power()) {
		return true;
	}
	else
	{
		return false;
	}

}

bool Boy::operator==(const Boy& boy) {
	//设置比较规则
	//薪资 * 黑马系数 + (100-年龄) * 1000
	if (power() == boy.power()) {
		return true;
	}
	else
	{
		return false;
	}

}

bool Boy::operator>(const Boy& boy) {
	//设置比较规则
	//薪资 * 黑马系数 + (100-年龄) * 1000
	if (power() > boy.power()) {
		return true;
	}
	else
	{
		return false;
	}

}

int Boy::power() const {
	int ret;
	ret = salary * darkHorse + (100 - age) * 1000;
	return ret;
}

int Boy::operator[](std::string index)const {
	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)const
{
	if (index == AGE_KEY) {
		return age;
	}
	else if (index == SALARY_KEY) {
		return salary;
	}
	else if (index == DARKHORSE_KEY) {
		return darkHorse;
	}
	else if (index == POWER_KEY) {
		return power();
	}
	else {
		return - 1;
	}
}

ostream& Boy::operator<<(ostream& os) const
{
	//os 相当于cout
	os << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age
		<< "\t薪资:" << salary << "\t黑马系数:" << darkHorse;

	return os;
}

Boy::Boy(const int age) {
	const char* defaultName = "未命名";		//先定义一个新变量存储未命名这个值,因为原先定义的name是const类型
	name = new char[strlen(defaultName)+1];	//位原先的name开辟新的空间
	strcpy_s(name, strlen(defaultName) + 1, defaultName);//把"未命名"复制进去

	this->age = age;
	salary = 0;
	darkHorse = 0;
	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;
	salary = 0;
	darkHorse = 0;
	id = ++LAST_ID;

}

Boy::operator int()const {
	return power();
}

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

ostream& operator<<(ostream& os, 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());//把name2 c语言的字符串拷贝到 boy.name中
	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);
	ostream& operator<<(ostream& os);
	friend ostream& operator<<(ostream& os, Man& man);
	~Man();

private:
	char* name;
	int age;
	int salary;
};

ostream& operator<<(ostream& os,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)
{
	//(char*)boy 是之前写的类型转换,转换成名字
	int len = strlen((char*)boy) + 1;
	this->name = new char[len];
	strcpy_s(name, len, (char*)boy);

	age = boy[AGE_KEY];//常量对象只能调用常量方法
	salary = boy[salary];
}

ostream& operator<<(ostream& os,Man& man) 
{
	os << "[Man]\t姓名:" << man.name << "\t年龄:" << man.age << "\t薪资:" << man.salary << endl;
	return os;
}

Man::~Man() {
	if (!name) {
		delete name;
	}
}

main.cpp

#include<iostream>
#include"Boy.h"
#include"Man.h"



int main(void) {
	
	Boy boy("lemn", 20, 20000, 9);
	Man man = boy;

	cout << boy<<endl;
	cout << man<<endl;

	system("pause");
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值