在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第十篇博客。
本篇博客介绍了C++的函数的进阶知识,以及类和对象。
本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。
目录
函数进阶
函数默认参数
函数的占位参数
函数重载
类和对象
封装
学生类
访问权限
struct和class
成员属性私有化
函数进阶
函数默认参数
C++中,函数的形参是可以有默认值的。相关语法为:
返回值类型 函数名(参数1,参数2,参数3 = 默认值...){...}
如果传参时传入数据,那么形参是传入的值,否则为默认值。
如果形参列表中,某个位置有默认参数,那么这个位置开始,右侧每个参数都必须有默认值。
如果函数声明中有默认参数,那么函数实现就不能有默认参数。
#includeusing namespace std; void show(int a, int b = 15, int c = 10); int main(void) { int num = 10; show(10, num); return 0; } void show(int a, int b, int c) { cout << a + b + c << endl; }
形参列表中b有默认值,则c必须有默认值。在show函数中,a b c的值都是10。
程序的输出是:
30
函数的占位参数
C++中,函数的形参列表里可以有占位参数,用于占位,在调用函数时必须填补该位置。语法是:
返回值类型 函数名(数据类型){...}
#includeusing namespace std; void show(int n, int); int main(void) { show(10, 5); return 0; } void show(int n, int) { cout << "This is a function" << endl; }
传参时必须传入两个参数。
程序的输出是
This is a function
函数重载
C++中函数名可以相同,提高代码的复用性。
函数重载要满足的条件是:
同一个作用域,函数名称相同,函数参数的类型或个数或顺序不同。
函数的返回值不作为函数重载的条件。
#includeusing namespace std; void func(void); void func(int n); void func(double d); void func(int a, int b); void func(double a, double b); int main(void) { func(); func(10); func(3.1416 + 2.71828); func(6, 8); func(3.1416, 2.71828); return 0; } void func(void) { cout << "void" << endl; } void func(int n) { cout << "The number is " << n << endl; } void func(double d) { cout << "The number is " << d << endl; } void func(int a, int b) { cout << "The number is " << a << " and " << b << endl; } void func(double a, double b) { cout << "The number is " << a << " and " << b << endl; }
这段代码中所有的func函数构成了重载。
程序的输出是
void
The number is 10
The number is 5.85988
The number is 6 and 8
The number is 3.1416 and 2.71828
引用可以作为函数重载的条件。
传参时传递常量和const会进入有const修饰的函数中(如果有)。
遇到默认参数时,重载应满足去掉默认参数后的参数不能相同。
类和对象
类中包含有属性和行为,作为一个整体。class表示类。
C++认为一切皆为对象,对象中有属性和行为。C++面向对象的三大特征是:封装,继承和多态。
封装
封装将属性和行为作为一个整体,表现生活中的事物。封装会对属性和行为加以权限控制。
设计类的格式是:
class 类名{
访问权限:
属性1
属性2
属性3
...
行为1
行为2
行为3
...
}
属性用变量表示,行为用函数表示。
声明一个类的对象的格式是: 类名 对象名
对象名.成员名访问对象的某个成员。
#includeusing namespace std; #define PI 3.14 class Circle { public: int r; double circles() { return 2 * PI * r; } }; int main(void) { Circle c; c.r = 10; cout << "The round is " << c.r << endl; cout << "The circle is " << c.circles() << endl; return 0; }
程序创建了Circle类,类中有一个int类型成员r,有一个成员函数circles,计算圆的周长。main函数中创建了这个类的一个对象,并进行了访问。
public修饰权限表示公共,可以被外界访问。
程序的输出是:
The round is 10
The circle is 62.8
学生类
#include
#include
using namespace std;
class student {
public:
string name;
int id;
void setname(string toname) {
name = toname;
}
void setid(int toid) {
id = toid;
}
void show() {
cout << "The name is " << name << endl;
cout << "The id is " << id << endl;
}
};
int main(void)
{
student s1, s2;
s1.name = "Bonnie";
s1.id = 20220204;
s1.show();
s2.setname("Otto");
s2.setid(20162216);
s2.show();
return 0;
}
程序创建了一个student类,默认权限全部为public。有两个成员变量,string类的name表示名字,int类型的id表示学号。类中有setname类和setid类用于给对应成员赋值。show方法用于输出信息。main函数中创建了两个对象,分别用直接赋值和set函数赋值。
程序的输出是:
The name is Bonnie
The id is 20220204
The name is Otto
The id is 20162216
访问权限
类在设计时,也可以把属性和行为放在不同权限下。
public是公共权限,类内类外都可以访问。protected是保护权限,类内可以访问,类外不可以访问。private是私有权限,类内可以访问,类外不可以访问。
#include#include using namespace std; class information { public: int id = 204; string name = "Douglas"; protected: int age = 26; private: int password = 19962020; public: void show() { cout << "The name is " << name < 程序创建了information类,类中的成员id和name的权限是public,成员age是protected权限,成员password是private权限。类中还有public权限的成员show函数,访问类中的变量并进行输出。
程序的输出是:
The name is Douglas
The id is 116
The age is 26main函数修改了成员id的值,但是不能修改age和password的值。
struct和class
C++中,struct默认权限为public,class的默认权限是private。如果需要自定义权限需要注明。
成员属性私有化
将所有成员设置为private,可以自己控制读写权限,并且能检测数据的有效性。
#include#include using namespace std; class information { string name; int age; public: void setname(string toname) { name = toname; } string getname() { return name; } void setage(int toage) { age = toage; } int getage() { return age; } void show() { cout << "The name is " << name << endl; cout << "The age is " << age << endl; } }; int main(void) { information inform; inform.setname("Blas"); string name = inform.getname(); cout << "The name is " << name << endl; inform.setage(24); int age = inform.getage(); cout << "The age is " << age << endl; return 0; } 程序创建了information类,成员name和age是private,同时为这两个变量提供了对应的set和get函数,分别用于设置值和获取值,权限为public。
程序的输出是:
The name is Blas
The age is 24