函数和运算符重载

函数的作用域

在C语言中函数默认都是全局的,使用关键字static可以将函数声明为静态,函数定义为static就意味着这个函数只能在定义这个函数的文件中使用,在其他文件中不能调用,即使在其他文件中声明这个函数都没用。

内部函数

如果在一个源文件中定义的函数只能被本文件中的函数调用,而不能被同一源程序其他文件中的函数调用,这种函数称为内部函数。

内部函数的首部可写为:

static 类型说明符 函数名(形参表)

内部函数也称为静态函数。但此处静态static的含义已经不是存储方式,而是指对函数的调用范围只局限于本文件,因此在不同的源文件中定义的同名的静态函数不会引起混淆。

外部函数

如果在一个源文件中定义的函数既能被本文件中的函数调用,又能被同一源程序其他文件中的函数调用,这种函数称为外部函数

外部函数的首部可写为:

extern 类型说明符 函数名(形参表)

C语言规定,如果在定义函数时省略extern,则隐含为外部函数

外部函数在整个源程序中都有效,如果程序中的其他文件要使用另一文件中定义的函数,就需要在该文件中对该外部函数进行声明。

当被调用函数在另一个文件中时,在主调函数中都必须用extern声明被调用函数是外部函数。

注意:

 

  1. 允许在不同的函数中使用相同的变量名,它们代表不同的对象,分配不同的单元,互不干扰。
  2. 同一源文件中,允许全局变量和局部变量同名,在局部变量的作用域内,全局变量不起作用。
  3. 所有的函数默认都是全局的,意味着所有的函数都不能重名,但如果是staitc函数,那么作用域是文件级的,所以不同的文件static函数名是可以相同的。

函数占位参数

C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置

1 void func(int)
2 {
3     cout<<"hello"<endl;
4 }

重载函数

C++允许使用同一个函数名定义多个函数,但这些函数的参数个数或参数类型必须有区别,在调用这些函数时,编译器会根据调用这些函数时的参数个数或类型不同调用不同的函数。这就是函数的重载。

作用:函数名可以相同,提高复用性

函数重载满足条件:

  • 同一个作用域下
  • 函数名称相同
  • 函数参数类型不同 或者个数不同 或者顺序不同

注意:函数的返回值不可以作为函数重载的条件

运算符重载

在C++语言中,用户可以重载C++中大部分的内置运算符。使用运算符重载不但可以提高程序代码的可读性,还可以满足泛型算法的应用需求。C++运算符重载可以定义一些以运算符为名称的函数,然后可以在程序中按照使用运算符的方式调用这些函数。运算符重载函数的函数名由关键字operator和其后要重载的运算符符号构成的,运算符重载函数也有返回类型和参数列表。

例:+运算符:

 1 #define  _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 using namespace std;
 4 
 5 class Person
 6 {
 7 public:
 8     Person operator+ (Person& p)
 9     {
10         Person temp{};
11         temp.m_A = this->m_A + p.m_A;
12         temp.m_B = this->m_B + p.m_B;
13         return temp;
14     }
15 
16     int m_A;
17     int m_B;
18 };
19 
20 Person operator+(Person& p1, int num)//这里出现了函数重载
21 {
22     Person temp{};
23     temp.m_A = p1.m_A + num;
24     temp.m_B = p1.m_B + num;
25     return temp;
26 }
27 
28 int main()
29 {
30     Person p1{};
31     p1.m_A = 10;
32     p1.m_B = 10;
33     Person p2{};
34     p2.m_A = 10;
35     p2.m_B = 10;
36 
37     Person p3 = p1 + p2;
38     Person p4 = p1 + 100;
39     
40     cout << "p3的m_A:" << p3.m_A << endl;
41     cout << "p3的m_B:" << p3.m_B << endl;
42     cout << "p4的m_A:" << p4.m_A << endl;
43     cout << "p4的m_B:" << p4.m_B << endl;
44 
45     system("pause");
46     return 0;
47 }

运行结果:


<<运算符:

 1 #define  _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 using namespace std;
 4 
 5 class Person
 6 {
 7     friend ostream& operator<<(ostream& cout, Person& p);
 8 public:
 9     Person(int a, int b)
10     {
11         m_A = a;
12         m_B = b;
13     }
14 private:
15     int m_A;
16     int m_B;
17 };
18 
19 //只能利用全局函数重载左移运算符
20 ostream& operator<<(ostream& cout, Person& p)
21 {
22     cout << "m_A=" << p.m_A << " m_B=" << p.m_B;
23     return cout;
24 }
25 
26 int main()
27 {
28     Person p1(10,10);
29     cout << p1 << " hello world" << endl;
30 
31     system("pause");
32     return 0;
33 }

 运行结果:


递增运算符:

 1 #define  _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 using namespace std;
 4 
 5 class MyInteger
 6 {
 7     friend ostream& operator<<(ostream& cout, MyInteger myint);
 8 public:
 9     MyInteger()
10     {
11         m_Num = 0;
12     }
13 
14     //重载前置++运算符
15     MyInteger& operator++()
16     {
17         m_Num++;
18         return *this;
19     }
20 
21     //重载后置++运算符
22     MyInteger operator++(int)
23     {
24         //先 记录当时结果
25         MyInteger temp = *this;
26         //后 递增
27         m_Num++;
28         //最后将记录结果做返回
29         return temp;
30     }
31 
32 private:
33     int m_Num;
34 };
35 
36 ostream& operator<<(ostream& cout, MyInteger myint)
37 {
38     cout << myint.m_Num;
39     return cout;
40 }
41 
42 void test01()
43 {
44     MyInteger myint;
45 
46     cout << ++(++myint) << endl;
47 }
48 
49 void test02()
50 {
51     MyInteger myint;
52 
53     cout << myint++ << endl;
54     cout << myint << endl;
55 }
56 
57 int main()
58 {
59     test01();
60     test02();
61 
62     system("pause");
63     return 0;
64 }

 

 赋值运算符:

 1 #define  _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 using namespace std;
 4 
 5 class Person
 6 {
 7 public:
 8     Person(int age)
 9     {
10         m_Age = new int(age);
11     }
12     ~Person()
13     {
14         delete m_Age;
15         m_Age = NULL;
16     }
17     Person& operator=(Person& p)
18     {
19         //先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
20         if (m_Age != NULL)
21         {
22             delete m_Age;
23             m_Age = NULL;
24         }
25         //深拷贝
26         m_Age = new int(*p.m_Age);
27 
28         return *this;
29     }
30     int *m_Age;
31 };
32 
33 void test01()
34 {
35     Person p1(18);
36     Person p2(20);
37     Person p3(30);
38 
39     p3 = p2 = p1;
40 
41     cout << "p1的年龄为:" << *p1.m_Age << endl;
42     cout << "p2的年龄为:" << *p2.m_Age << endl;
43     cout << "p3的年龄为:" << *p3.m_Age << endl;
44 }
45 
46 int main()
47 {
48     test01();
49 
50     system("pause");
51     return 0;
52 }


关系运算符

 1 #define  _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 5 
 6 class Person
 7 {
 8 public:
 9     Person(string name, int age)
10     {
11         m_Name = name;
12         m_Age = age;
13     }
14     
15     //重载 == 号
16 
17     bool operator==(Person &p)
18     {
19         if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
20         {
21             return true;
22         }
23         return false;
24     }
25 
26     string  m_Name;
27     int m_Age;
28 };
29 
30 void test01()
31 {
32     Person p1("Tom", 18);
33     Person p2("Tom", 18);
34 
35     if (p1 == p2)
36     {
37         cout << "p1和p2是相等的!" << endl;
38     }
39     else
40     {
41         cout << "p1和p2是不相等的!" << endl;
42     }
43 }
44 
45 int main()
46 {
47     test01();
48 
49     system("pause");
50     return 0;
51 }
 
posted @ 2023-01-27 17:46  永生辉皇  阅读(87)  评论(0)    收藏  举报