我是靠谱客的博主 生动小甜瓜,这篇文章主要介绍C++类类型转换普通类型,现在分享给大家,希望可以做个参考。

main.cpp

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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("张华森",24,20000,10); int power = boy1; char*name = boy1; cout << name << endl; cout << power << endl; system("pause"); return 0; }

Boy.h

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once #include<iostream> #include<string> using namespace std; class Boy { public: Boy(const char*name =NULL, int age=0,int salary=0,int darkHorse=0); Boy(int salary); Boy(char *name); ~Boy(); Boy&operator=(const Boy&boy); bool operator>(const Boy&boy); bool operator < (const Boy&boy); bool operator==(const Boy&boy); //下标运算符重载 int operator[](string index); friend ostream& operator<<(ostream &os,const Boy&boy); friend istream & operator>>(istream&is, Boy&boy); operator int()const; operator char*()const; string description(); private: char*name; int age; int salary; int darkHorse; //黑马,潜力值 unsigned int id; //编号 static int LAST_ID; int power()const; };

Boy.cpp

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "Boy.h" #include<sstream> #define NUM 100 int Boy::LAST_ID = 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(int salary) { const char *defultName = "未命名"; this->name = new char[strlen(defultName) + 1]; strcpy_s(this->name, strlen(defultName) + 1, defultName); this->age = 0; this->salary = salary; this->darkHorse = 0; this->id = ++LAST_ID; } Boy::Boy(char * name) { this->name = new char[strlen(name) + 1]; strcpy_s(this->name, strlen(name) + 1, name); this->age = 0; this->salary =0; this->darkHorse = 0; 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; 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[](string index) { if (index=="age") { return age; } else if (index =="salary") { return salary; } else if (index =="darkHourse") { return darkHorse; } else if (index =="power") { return power(); } else { return -1; } } Boy::operator int() const { return power(); } Boy::operator char*() const { return name; } string Boy::description() { stringstream ret; ret << "ID:" << id << "t姓名:" << name << "t年龄:" << age << "t薪资:" << salary << "t黑马系数:" << darkHorse; return ret.str(); } int Boy::power() const { //薪资*黑马系数+(100-年龄)*100 int value = salary * darkHorse + (NUM - age)*NUM; return value; }

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

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

最后

以上就是生动小甜瓜最近收集整理的关于C++类类型转换普通类型的全部内容,更多相关C++类类型转换普通类型内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部