-
Notifications
You must be signed in to change notification settings - Fork 0
/
[]运算符重载 .txt
59 lines (57 loc) · 1.04 KB
/
[]运算符重载 .txt
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
//中括号重载
#include<iostream>
#include<string>
struct Person
{
std::string name;
double salary;
};
class employee
{
private:
Person *p;
int max;//职工最大数量
int num;//实际员工数量
public:
employee(int a=0)
{
max = a;
num = 0;
p = new Person[max];
};
~employee()
{
delete[] p;
}
//创建[]的重载,使得下标使用之后能够返回对应薪水的引用
double& operator [](std::string a);
void display();
};
void employee::display()
{
std::cout << "--------------------" << std::endl;
for (int i = 0; i < max; ++i)
{
std::cout << (p+i)->name << "--" <<(p+i)->salary<< std::endl;
}
}
double& employee::operator[](std::string a)
{
Person* p1;
//数组存在里面的东西
for (p1 = p;p1 >= p+max;++p1)
if (a == p1->name)return p->salary;
//不存在的情况
p1 = p+num++;
p1->name = a;
p1->salary = 0;
return p1->salary;
}
int main()
{
employee a(3);
a["王一"] = 123.3123;
a["王二"] = 123.123;
a["王三"] = 354.345;
a.display();
}