C++语言-02-函数

普通函数

C++是在C语言的基础上增加了面向对象特性的语言,是C语言的超集
  • C++中的普通函数与C语言中的普通函数具有类似的性质。请参照以下博客:C语言-04-函数

与类相关的函数

C是一种OOP语言,类是OOP语言的核心,C++中的一部分函数与类相关。
  • 构造函数与拷贝构造函数

    • 构造函数
      • 是一种特殊的成员函数,在使用类创建对象时被执行,通常用于在创建对象时为某些成员变量设置初始值。

      • 构造函数的定义规范

        • 函数名与类名相同
        • 不返回任何值
      • 构造函数的种类

        • 无参的构造函数
        • 有参的构造函数
          • 可以使用初始化列表来初始化字段
      • 定义方式

        // 无参的构造函数
        ClassName() {
            // body of the function
        }
        // 有参的构造函数,使用初始化列表
        ClassName(MemberVariableType memberVariable) : variable(memberVariable) {
        	// body of the function
        }
        
    • 拷贝构造函数
      • 是一种特殊的构造函数,在创建对象,使用同一类中之前创建过的对象来初始化新创建的对象

      • 拷贝构造函数被调用的场景

        • 使用另一个同类型的对象来初始化新创建的对象
        • 赋值对象把它作为参数传递给函数
        • 赋值对象,并从函数返回这个对象
      • 注意

        • 若没有定义拷贝构造函数,编译器会自动定义一个。但是在一下情形下,必须定义一个拷贝构造函数
          • 类中有指针类型的成员变量,并且有动态内存分配
      • 定义方式

        ClassName(const ClassName &classInstance) {
        	variable = classInstance.variable
        }
        
  • 析构函数

    • 是一种特殊的成员函数,在类的象被销毁时被执行,通常用于在跳出程序前释放资源

    • 析构函数的定义规范

      • 函数名与类名相同,需要在函数名前加上波浪号(~)作为前缀
      • 不返回任何值,不带有任何参数
    • 定义方式

        ```
        ~ClassName() {
            // Release the resource of the classInstance
        }
        ```
      
  • 友元函数

    • 一种定义在类的外部,有权访问类的私有成员和保护成员的函数

    • 定义方式

      // 在类内部定义友元函数原型
      friend returnType functionName();
      // 在类外部定义友元函数的实现,不需要范围解析运算符 ::
      returnType functionName() {
          body of the function;
      }
      
    • 注意

      • 友元函数不是成员函数,不能使用this指针
  • 内联函数

    • 在编译阶段,编译器会将内联函数的代码副本复制到每一个调用该该内联函数的地方,使用inline关键字定义,如下

      inline returnType functionName() {
          // body of the function
      }
      
    • 内联函数的注意事项

      • 类定义中定义的函数,默认都是内俩函数,即使没有使用inline关键字
      • 每次修改内联函数,都必须重新编译该函数的所有调用代码(替换函数调用处旧的内联函数副本)
  • 示例

    • 定义Animal类

      class Animal {
      private:
          string name; // 名字
          int *age; // 年龄
      public:
          // set方法
          inline void setName(string animalName) {
              name = animalName;
          }
          void setAge(int animalAge) {
              *age = animalAge;
          }
          // 无参的构造函数
          Animal() {
              cout << "Normal constructor" << endl;
              // 为指针分配内存地址
              age = new int;
          }
          // 有参的构造函数
          Animal(string animalName, int animalAge) {
              cout << "Normal constructor with parameter" << endl;
              name = animalName;
              // 为指针分配内存地址
              age = new int;
              *age = animalAge;
          }
          // 拷贝构造函数
          Animal(const Animal &animal) {
              cout << "Copy constructor" << endl;
              name = animal.name;
              // 为指针分配内存地址
              age = new int;
              *age = *animal.age;
          }
          // 友元函数
          friend void printAnimal(const Animal animal);
          // 析构函数
          ~Animal() {
              cout << "Release the resource of the classInstance" << endl;
              delete age;
          }
      };
      
    • 定义Animal类的友元函数

      // 友元函数的定义
      void printAnimal(const Animal animal) {
         cout << "狗的名字:" << animal.name << " 年龄:" << *animal.age << endl;
      }
      
    • main()函数

      int main(int argc, const char * argv[]) {
          // 使用有参的构造函数创建Animal对象
          Animal dog1;
          dog1.setName("TOBY");
          dog1.setAge(5);
          // 使用友元函数打印dog1
          printAnimal(dog1);
          // 使用无参的构造函数创建Animal对象
          Animal dog2("MAX", 4);
          // 使用友元函数打印dog2
          printAnimal(dog2);
          // 使用拷贝构造函数创建Animal对象
          Animal dog3 = dog2;
          printAnimal(dog3);
          return 0;
      }
      
原文地址:https://www.cnblogs.com/theDesertIslandOutOfTheWorld/p/5211996.html