C++:04.操作符重载和类的继承

1. 操作符重载

一般来讲我们定义在类的里面

格式:返回值 operate需要重载的操作符(参数){} 例如:void operator++(){}

1.1 加减操作符

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
class Vector {
public:
Vector(int x, int y) {
this->x = x;
this->y = y;
}

Vector(const Vector &vector) {
this->x = vector.x;
this->y = vector.y;
cout << "拷贝构造函数" << endl;
}

private:
int x;
int y;

public:
void setX(int x) {
this->x = x;
}

void setY(int y) {
this->y = y;
}

int getX() {
return this->x;
}

int getY() {
return this->y;
}

// 重载减号运算符
// 为什么要用引用,为了防止重新创建对象
// const 关键常量,为了防止去修改值
Vector operator-(const Vector &vector) {
int x = this->x - vector.x;
int y = this->y - vector.y;
Vector res(x, y);
return res; // 不建议返回引用
}

Vector operator + (const Vector &vector) {
int x = this->x + vector.x;
int y = this->y + vector.y;
Vector vector(x,y);
return vector;
}
};

int main() {
Vector vector1(2, 3);
Vector vector2(2, 3);

// java 中 string + string;

// char* str = "123" + "456";

// 重载运算符 -
Vector vector = vector1 - vector2;
cout << vector.getX() << " , " << vector.getY() << endl;

getchar();
}

打印结果

1
0,0

1.2 自增减运算符

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
class Vector {
public:
Vector(int x, int y) {
this->x = x;
this->y = y;
}

Vector(const Vector &vector) {
this->x = vector.x;
this->y = vector.y;
cout << "拷贝构造函数" << endl;
}

private:
int x;
int y;

public:
void setX(int x) {
this->x = x;
}

void setY(int y) {
this->y = y;
}

int getX() {
return this->x;
}

int getY() {
return this->y;
}

// 自增运算符
void operator++() { // ++X
this->x = ++this->x;
this->y = ++this->y;
}

void operator++(int) { // X++
this->x = this->x++;
this->y = this->y++;
}
};

int main() {

Vector vector(1, 2);
// vector++;
++vector;

cout << vector.getX() << " , " << vector.getY() << endl;

getchar();
}

打印结果:

1
2 , 3

1.3 输出运算符

定义:

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
class Vector {
public:
Vector(int x, int y) {
this->x = x;
this->y = y;
}

Vector(const Vector &vector) {
this->x = vector.x;
this->y = vector.y;
cout << "拷贝构造函数" << endl;
}

private:
int x;
int y;

public:
void setX(int x) {
this->x = x;
}

void setY(int y) {
this->y = y;
}

int getX() {
return this->x;
}

int getY() {
return this->y;
}

// 输出运算符
friend ostream &operator<<(ostream &_Ostr, const Vector vector) {
_Ostr << vector.x << " , " << vector.y << endl;
return _Ostr;
}
};

int main() {

Vector vector(1, 2);

// 这么使用
cout << vector << vector;

getchar();
}

打印结果:

1
2
3
4
拷贝构造函数
拷贝构造函数
1 , 2
1 , 2

1.4 条件运算符

1
2
3
4
// 条件运算符
bool operator==(const Vector &vector) {
return (this->x == vector.x && this->y == vector.y);
}

1.5 括号运算符

1
2
3
4
// 操作符[]
int operator[](int index) {
return this->array[index];
}

2. 类继承

2.1 变量修饰符和继承修饰符

变量修饰符:

  1. private :本类中使用
  2. protected :子类中能使用(默认)
  3. public :公开,任何地方都可以

类继承修饰符:

  1. private :本类中使用
  2. protected :子类中能使用(默认)
  3. public :公开,任何地方都可以
1
2
3
4
// 加了 public 才可以访问 Person 中的公有属性和方法
class Student : public Person {
...
};

2.2 初始化父类属性

不光可以给父类初始化属性,还可以给本类的属性进行初始化,用 , 隔开即可

1
2
3
Student(char *name, int age, char *course) : Person(name, age), course(course) {
cout << "Student 构造函数" << endl;
}

2.3 初始化属性和函数重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Person {
//变量修饰符
private:// 本类中使用
// protected: 子类中能使用(默认)
// public:公开,任何地方都可以
char *name;
int age;
public:
Person(char *name, int age) {
this->name = name;
this->age = age;
cout << "Person 构造函数" << endl;
}

public:
void print() {
cout << this->name << " , " << this->age << endl;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Student : public Person {
private:
char *course;
public:
// :Person(name,age) 调用构造函数初始化父类的属性
// 不光可以给父类初始化属性,还可以给本类的属性进行初始化,用 , 隔开即可
Student(char *name, int age, char *course) : Person(name, age), course(course) {
cout << "Student 构造函数" << endl;
}

void print() {
cout << "course: " << course << endl;
}
};

int main(){
Student stu("eastrise",25,"数学");
stu.print();
}

打印结果:

1
2
3
Person 构造函数
Student 构造函数
course: 数学

2.4 多继承

, 号 隔开

1
2
3
class Student : public Person , public Vector{

};
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!
0%