C++入门Step12【 类的成员函数】

类的成员函数:

  1. 普通成员函数:

    类的普通成员函数跟普通的函数没有太大的区别,也都是有参数的类型和形参,还有返回值之类的,唯一的区别就是 必须由该类的实例化对象去调用
    既然有类的普通成员函数,那么就有不普通的函数,那就是 类的静态成员函数

  2. inline 内联函数: It is derived from the C language macro definition.

    1. 实际上inline内联函数是由C语言的宏发展而来

    2. 作用说明:

      首先,需要明确:主函数调用其他函数时开销大,调用过程中需要将参数压栈等等一系列操作,所以,以下代码段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关键字声明的函数,可以在调用的时候,将函数的代码直接嵌入到调用的地方,所以大大的减少了函数调用的开销,提高了效率。

    3. 使用示例:

      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 内联函数。
      
  3. 类的声明和实现分离:

    1. 未分离时:

      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;
          }
      };
      
    2. 分离目的:便于阅读,使得类的结构层次清晰。

    3. 展示:

      //类的声明
      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)

  4. 使用多文件分离类的声明和实现:

    将类的声明放到 .h 头文件中,将类的实现放到 .cpp 实现文件中,谁要使用这个类,就 include 包含 .h 类的头文件就可以啦!

    1. 头文件: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
      
      
    2. 成员函数实现文件: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;
      }
      
      
      
    3. 主函数: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;
      }
      

部分话术引自VC驿站:https://www.cctry.com/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值