C++系列教程P5:‘类与对象’三部曲——[对象和特殊成员]

1. 对象的概念

在C++中,对象是类的一个实例。对象可以包含数据和函数,实现不同的行为。对象是面向对象程序设计中的核心概念之一。

2. 特殊成员函数

在C++中,有一些特殊的成员函数,它们被自动调用,与类的其他成员函数不同。这些特殊成员函数包括:

  • 构造函数
  • 拷贝构造函数
  • 移动构造函数
  • 拷贝赋值运算符
  • 移动赋值运算符
  • 析构函数

接下来,我们将分别介绍这些特殊成员函数的功能和使用方法。

(1) 构造函数

构造函数是一种特殊的成员函数,它负责初始化类的对象,并在创建对象时自动调用。构造函数的名称与类名相同,没有返回值类型,可以重载。

以下是一个示例:

c++Copy Code
class Animal{ private: string name; int age; public: Animal(){ name = "Unknown"; age = 0; } Animal(string n, int a){ name = n; age = a; } };

上述代码中,Animal类有两个构造函数,一个使用默认参数,另一个使用用户指定参数。

(2) 拷贝构造函数

拷贝构造函数是一种特殊的构造函数,它负责将一个对象的值复制到另一个对象中。拷贝构造函数的名称为类名,参数为指向本类对象的引用。

以下是一个示例:

c++Copy Code
class Animal{ private: string name; int age; public: Animal(const Animal& other){ name = other.name; age = other.age; } };

上述代码中,Animal类实现了一个拷贝构造函数,用于将一个Animal对象的值复制到另一个Animal对象中。

(3) 移动构造函数

移动构造函数是一种特殊的构造函数,它负责将一个对象的资源所有权转移到另一个对象中。移动构造函数的名称为类名,参数为指向本类对象的引用。

以下是一个示例:

c++Copy Code
class Animal{ private: string* name; public: Animal(){ name = new string("Unknown"); } Animal(Animal&& other){ name = other.name; other.name = nullptr; } ~Animal(){ delete name; } };

上述代码中,Animal类实现了一个移动构造函数,用于将一个Animal对象的资源所有权转移到另一个Animal对象中。

(4) 拷贝赋值运算符

拷贝赋值运算符是一种特殊的成员函数,用于将一个对象的值赋给另一个对象。拷贝赋值运算符的名称为"=",参数为指向本类对象的引用。

以下是一个示例:

c++Copy Code
class Animal{ private: string name; int age; public: Animal& operator=(const Animal& other){ name = other.name; age = other.age; return *this; } };

上述代码中,Animal类实现了一个拷贝赋值运算符,用于将一个Animal对象的值赋给另一个Animal对象。

(5) 移动赋值运算符

移动赋值运算符是一种特殊的成员函数,用于将一个对象的资源所有权转移到另一个对象中。移动赋值运算符的名称为"=",参数为指向本类对象的引用。

以下是一个示例:

c++Copy Code
class Animal{ private: string* name; public: Animal& operator=(Animal&& other){ if(this != &other){ delete name; name = other.name; other.name = nullptr; } return *this; } ~Animal(){ delete name; } };

上述代码中,Animal类实现了一个移动赋值运算符,用于将一个Animal对象的资源所有权转移到另一个Animal对象中。

(6) 析构函数

析构函数是一种特殊的成员函数,它在对象销毁时自动调用,用于清理对象所占用的资源。析构函数的名称为"~类名",没有返回类型,也不需要参数。

以下是一个示例:

c++Copy Code
class Animal{ private: string* name; public: Animal(){ name = new string("Unknown"); } ~Animal(){ delete name; } };

上述代码中,Animal类实现了一个析构函数,用于在对象销毁时清理对象所占用的资源。

3. 使用场景

对象和特殊成员函数是C++中面向对象程序设计的核心概念之一。它们提供了一种更加灵活的编程方式,可以帮助我们更好地管理程序的复杂性。

下面是一个使用对象和特殊成员函数的示例:

c++Copy Code
#include <iostream> #include <string> using namespace std; class Person{ private: string name; int age; public: Person(){ name = "Unknown"; age = 0; cout << "Default constructor called." << endl; } Person(string n, int a){ name = n; age = a; cout << "Constructor with parameters called." << endl; } Person(const Person& other){ name = other.name; age = other.age; cout << "Copy constructor called." << endl; } Person(Person&& other){ name = other.name; other.name = ""; age = other.age; cout << "Move constructor called." << endl; } ~Person(){ cout << "Destructor called." << endl; } Person& operator=(const Person& other){ name = other.name; age = other.age; cout << "Copy assignment operator called." << endl; return *this; } Person& operator=(Person&& other){ if(this != &other){ name = other.name; age = other.age; other.name = ""; cout << "Move assignment operator called." << endl; } return *this; } void display(){ cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main(){ // 使用默认构造函数 Person p1; p1.display(); cout << endl; // 使用带参数的构造函数 Person p2("Tom", 20); p2.display(); cout << endl; // 使用拷贝构造函数 Person p3 = p2; p3.display(); cout << endl; // 使用移动构造函数 Person p4 = move(p2); p4.display(); cout << endl; // 使用拷贝赋值运算符 p3 = p1; p3.display(); cout << endl; // 使用移动赋值运算符 p4 = move(p1); p4.display(); cout << endl; return 0; }

上述代码中,我们定义了一个Person类,使用对象和特殊成员函数实现了不同的功能。在main函数中,我们对这些功能进行了测试,并输出了相应的信息。

4. 总结

本文介绍了C++中的对象和特殊成员函数,并通过示例演示了它们的使用方法。对于需要使用面向对象程序设计的开发者来说,掌握这些知识非常重要。