函数的作用域
在C语言中函数默认都是全局的,使用关键字static可以将函数声明为静态,函数定义为static就意味着这个函数只能在定义这个函数的文件中使用,在其他文件中不能调用,即使在其他文件中声明这个函数都没用。
内部函数
如果在一个源文件中定义的函数只能被本文件中的函数调用,而不能被同一源程序其他文件中的函数调用,这种函数称为内部函数。
内部函数的首部可写为:
static 类型说明符 函数名(形参表)
内部函数也称为静态函数。但此处静态static的含义已经不是存储方式,而是指对函数的调用范围只局限于本文件,因此在不同的源文件中定义的同名的静态函数不会引起混淆。
外部函数
如果在一个源文件中定义的函数既能被本文件中的函数调用,又能被同一源程序其他文件中的函数调用,这种函数称为外部函数
外部函数的首部可写为:
extern 类型说明符 函数名(形参表)
C语言规定,如果在定义函数时省略extern,则隐含为外部函数
外部函数在整个源程序中都有效,如果程序中的其他文件要使用另一文件中定义的函数,就需要在该文件中对该外部函数进行声明。
当被调用函数在另一个文件中时,在主调函数中都必须用extern声明被调用函数是外部函数。
注意:
- 允许在不同的函数中使用相同的变量名,它们代表不同的对象,分配不同的单元,互不干扰。
- 同一源文件中,允许全局变量和局部变量同名,在局部变量的作用域内,全局变量不起作用。
- 所有的函数默认都是全局的,意味着所有的函数都不能重名,但如果是staitc函数,那么作用域是文件级的,所以不同的文件static函数名是可以相同的。
函数占位参数
C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
void func(int)
{
cout<<"hello"<endl;
}
重载函数
C++允许使用同一个函数名定义多个函数,但这些函数的参数个数或参数类型必须有区别,在调用这些函数时,编译器会根据调用这些函数时的参数个数或类型不同调用不同的函数。这就是函数的重载。
作用:函数名可以相同,提高复用性
函数重载满足条件:
- 同一个作用域下
- 函数名称相同
- 函数参数类型不同 或者个数不同 或者顺序不同
注意:函数的返回值不可以作为函数重载的条件
运算符重载
在C++语言中,用户可以重载C++中大部分的内置运算符。使用运算符重载不但可以提高程序代码的可读性,还可以满足泛型算法的应用需求。C++运算符重载可以定义一些以运算符为名称的函数,然后可以在程序中按照使用运算符的方式调用这些函数。运算符重载函数的函数名由关键字operator和其后要重载的运算符符号构成的,运算符重载函数也有返回类型和参数列表。
例:+运算符:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person
{
public:
Person operator+ (Person& p)
{
Person temp{};
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
int m_A;
int m_B;
};
Person operator+(Person& p1, int num)//这里出现了函数重载
{
Person temp{};
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
int main()
{
Person p1{};
p1.m_A = 10;
p1.m_B = 10;
Person p2{};
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
Person p4 = p1 + 100;
cout << "p3的m_A:" << p3.m_A << endl;
cout << "p3的m_B:" << p3.m_B << endl;
cout << "p4的m_A:" << p4.m_A << endl;
cout << "p4的m_B:" << p4.m_B << endl;
system("pause");
return 0;
}
运行结果:
<<运算符:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(int a, int b)
{
m_A = a;
m_B = b;
}
private:
int m_A;
int m_B;
};
//只能利用全局函数重载左移运算符
ostream& operator<<(ostream& cout, Person& p)
{
cout << "m_A=" << p.m_A << " m_B=" << p.m_B;
return cout;
}
int main()
{
Person p1(10,10);
cout << p1 << " hello world" << endl;
system("pause");
return 0;
}
运行结果:
递增运算符:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符
MyInteger& operator++()
{
m_Num++;
return *this;
}
//重载后置++运算符
MyInteger operator++(int)
{
//先 记录当时结果
MyInteger temp = *this;
//后 递增
m_Num++;
//最后将记录结果做返回
return temp;
}
private:
int m_Num;
};
ostream& operator<<(ostream& cout, MyInteger myint)
{
cout << myint.m_Num;
return cout;
}
void test01()
{
MyInteger myint;
cout << ++(++myint) << endl;
}
void test02()
{
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
赋值运算符:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
m_Age = new int(age);
}
~Person()
{
delete m_Age;
m_Age = NULL;
}
Person& operator=(Person& p)
{
//先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
//深拷贝
m_Age = new int(*p.m_Age);
return *this;
}
int *m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1;
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
关系运算符
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
//重载 == 号
bool operator==(Person &p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
string m_Name;
int m_Age;
};
void test01()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2)
{
cout << "p1和p2是相等的!" << endl;
}
else
{
cout << "p1和p2是不相等的!" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}