函数提高
函数默认参数
在C++中,函数的形参列表中的形参是可以有默认值的。
语法:返回值类型 函数名 (参数 = 默认值){}
**注意事项:
- 如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须都要有默认值
- 如果函数声明有默认值,函数实现的时候就不能有默认参数**
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| int func(int a, int b = 10, int c = 10) { return a + b + c; }
int func2(int a = 10, int b = 10); int func2(int a, int b) { return a + b; }
int main() {
cout << "ret = " << func(20, 20) << endl; cout << "ret = " << func(100) << endl;
system("pause");
return 0; }
|
函数重载
函数重载概述
作用:函数名可以相同,提高复用性
函数重载满足条件:
● 同一个作用域下
● 函数名称相同
● 函数参数类型不同 或者 个数不同 或者 顺序不同
注意: 函数的返回值不可以作为函数重载的条件(在编程语言中,函数重载是根据函数的参数列表来区分不同的函数实现的,而不是根据函数的返回值。换句话说,不能仅仅因为两个函数的返回值类型不同,就认为它们是重载的。)
示例:
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
| void func() { cout << "func 的调用!" << endl; } void func(int a) { cout << "func (int a) 的调用!" << endl; } void func(double a) { cout << "func (double a)的调用!" << endl; } void func(int a ,double b) { cout << "func (int a ,double b) 的调用!" << endl; } void func(double a ,int b) { cout << "func (double a ,int b)的调用!" << endl; }
int main() {
func(); func(10); func(3.14); func(10,3.14); func(3.14 , 10); return 0; }
|
函数重载注意事项
● 引用作为重载条件(常量引用作为参数重载)
● 函数重载碰到函数默认参数(调用会找到多个入口,引发歧义)
示例:
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
| #include<iostream> using namespace std;
void func(int &a) { cout << "func (int &a) 调用 " << endl; }
void func(const int &a) { cout << "func (const int &a) 调用 " << endl; }
void func2(int a, int b = 10) { cout << "func2(int a, int b = 10) 调用" << endl; }
void func2(int a) { cout << "func2(int a) 调用" << endl; }
int main() {
int a = 10; func(a); func(10);
return 0; }
|
类和对象
C++面向对象的三大特性为:封装、继承、多态
C++认为万事万物都皆为对象,对象上有其属性和行为
例如:
人可以作为对象,属性有姓名、年龄、身高、体重…,行为有走、跑、跳、吃饭、唱歌…
车也可以作为对象,属性有轮胎、方向盘、车灯…,行为有载人、放音乐、放空调…
具有相同性质的对象,我们可以抽象称为类,人属于人类,车属于车类
封装
封装的意义
封装是C++面向对象三大特性之一
封装的意义:
● 将属性和行为作为一个整体,表现生活中的事物
● 将属性和行为加以权限控制
封装意义一:
在设计类的时候,属性和行为写在一起,表现事物
语法: class 类名{ 访问权限: 属性 / 行为 };
示例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
| const double PI = 3.14;
class Circle { public:
int m_r;
double calculateZC() { return 2 * PI * m_r; } };
int main() {
Circle c1; c1.m_r = 10;
cout << "圆的周长为: " << c1.calculateZC() << endl;
system("pause");
return 0; }
|
示例2:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
示例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
| class Student { public: void setName(string name) { m_name = name; } void setID(int id) { m_id = id; }
void showStudent() { cout << "name:" << m_name << " ID:" << m_id << endl; } public: string m_name; int m_id; };
int main() {
Student stu; stu.setName("德玛西亚"); stu.setID(250); stu.showStudent(); return 0; }
|
封装意义二:
类在设计时,可以把属性和行为放在不同的权限下,加以控制
访问权限有三种:
1. public 公共权限 类内可以访问 类外可以访问
2. protected 保护权限 类内可以访问 类外不可以访问
3. private 私有权限 类内可以访问 类外不可以访问
示例:
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
|
class Person { public: string m_Name;
protected: string m_Car;
private: int m_Password;
public: void func() { m_Name = "张三"; m_Car = "拖拉机"; m_Password = 123456; } };
int main() {
Person p; p.m_Name = "李四";
system("pause");
return 0; }
|