类的成员函数:
-
普通成员函数:
类的普通成员函数跟普通的函数没有太大的区别,也都是有参数的类型和形参,还有返回值之类的,唯一的区别就是 必须由该类的实例化对象去调用;
既然有类的普通成员函数,那么就有不普通的函数,那就是 类的静态成员函数。 -
inline 内联函数:
It is derived from the C language macro definition.
-
实际上inline内联函数是由C语言的宏发展而来
-
作用说明:
首先,需要明确:主函数调用其他函数时开销大,调用过程中需要将参数压栈等等一系列操作,所以,以下代码段A的执行效率,明显高于代码段B:
具体情况请参考:函数调用
//代码段A: int main() { int a = 5; ++a; int b = a+3; return 0; } //代码段B: void fun() { int a = 5; ++a; int b = a+3; } int main() { fun(); return 0; }
在C语言中可以使用简单的宏实现相对简单的函数:
#define MAX_NUM(x, y) (x > y ? x : y) int main() { int ret = MAX_NUM(9, 7); return 0; } //在此处宏只是在预编译时进行了简单的代码替换,避免函数调用,提升了函数的执行效率
调用的时候感觉 MAX_NUM 像个函数,但是他是个宏,宏跟函数的区别是,在编译阶段就将宏的代码展开直接替换调用宏的地方。所以省去了函数调用的压栈、出栈等开销。所以执行效率方面要比函数高。
但是宏定义写起来比较难度倒是不大,就是麻烦一些,而且代码的可阅读性会变差。所以C++中引入了inline内联函数这么个东西,用inline关键字声明的函数,可以在调用的时候,将函数的代码直接嵌入到调用的地方,所以大大的减少了函数调用的开销,提高了效率。 -
使用示例:
class Student { public: string name; int num; int age; private: char sex; inline int max_num(int x, int y) //对频繁被调用的函数,添加内敛函数关键字 { return x > y ? x : y; } public: int get_max_num(int a, int b, int c) { int max_ab = max_num(a, b); return max_ab > c ? max_ab : c; } void print_name() { cout << "name = " << name << endl; } }; //其实1:默认情况下,在类体中直接定义/实现的函数,C++会自动的将其作为inline内联函数来处理,所以类似上面的代码:max_num、get_max_num、print_name 函数都会被看成是 inline 内联函数。而在类体外部定义的函数C++则会将其作为普通的类的成员函数来处理。 //其实2:并非将所有的函数都声明成 inline 就是好事儿,如果函数的执行体很大,很耗时,那么就不适合作为 inline 内联函数,只有当函数的执行体很小,只有几行代码,而且会被频繁的调用的时候才适合作为 inline 内联函数。
-
-
类的声明和实现分离:
-
未分离时:
class Student { public: string name; int num; int age; private: char sex; int max_num(int x, int y) { return x > y ? x : y; } public: int get_max_num(int a, int b, int c) { int max_ab = max_num(a, b); return max_ab > c ? max_ab : c; } void print_name() { cout << "name = " << name << endl; } };
-
分离目的:便于阅读,使得类的结构层次清晰。
-
展示:
//类的声明 class Student { public: string name; int num; int age; private: char sex; int max_num(int x, int y); public: int get_max_num(int a, int b, int c); void print_name(); }; //类的成员函数的实现 int Student::max_num(int x, int y) { return x > y ? x : y; } int Student::get_max_num(int a, int b, int c) { int max_ab = max_num(a, b); return max_ab > c ? max_ab : c; } void Student::print_name() { cout << "name = " << name << endl; }
注意:注意类的作用域修饰:Student:: 这个不能丢,丢了就会报错(Error)
-
-
使用多文件分离类的声明和实现:
将类的声明放到 .h 头文件中,将类的实现放到 .cpp 实现文件中,谁要使用这个类,就 include 包含 .h 类的头文件就可以啦!
-
头文件:inc.h
// // Created by Today me on 2021/5/20. // #ifndef PRACTICE_C___INC_H #define PRACTICE_C___INC_H class Student //声明Student类 { private: std::string name; std::string num; int age; bool sex; public: void set_nnas(); void print_imt(); }; #endif //PRACTICE_C___INC_H
-
成员函数实现文件:export.cpp
#include <iostream> #include "inc.h" // // Created by Today me on 2021/5/20. // //成员函数的实现: void Student::set_nnas() { std::cout << "Please enter the \"name,num,age,sex\" successively\r" <<std::endl; std::cin >> name >> num >> age >>sex; } void Student::print_imt() { std::cout << "The information of student:\n\tName: " << name <<"\n\tNo.: "<<num<<"\n\tAge: "<<age<<"\n\tSex: "<<sex<<std::endl; }
-
主函数:main.h
#include <iostream> #include "inc.h" int main() { Student Thomas ; //实例化Student类为:Thomas Thomas.set_nnas(); Thomas.print_imt(); //调用设置函数(set_nnas)和显示函数(print_imt) return 0; }
-