《深度探索c++对象模型》第三章笔记
发布人:shili8
发布时间:2025-03-09 07:58
阅读次数:0
**深度探索C++对象模型**
**第三章:类、继承和多态**
###3.1 类的定义在 C++ 中,类是用来描述一个集合数据成员和函数的模板。类的定义遵循以下格式:
cppclass 类名 { // 成员变量 // 成员函数};
例如,我们可以定义一个简单的 `Person` 类,如下所示:
cppclass Person { public: std::string name; int age; void sayHello() { std::cout << "Hello, my name is " << name << " and I'm " << age << " years old." << std::endl; } };
###3.2 类的成员变量和成员函数类的成员变量是存储在类实例中的数据,例如 `name` 和 `age`。成员函数是用于操作这些数据的函数,例如 `sayHello()`。
cppclass Person { public: std::string name; int age; void sayHello() { // 成员变量可以直接访问 std::cout << "Hello, my name is " << name << " and I'm " << age << " years old." << std::endl; } };
###3.3 继承继承是指一个类从另一个类继承属性和方法的机制。子类可以继承父类的成员变量和成员函数,并且可以在子类中添加新的成员变量和成员函数。
cppclass Person { public: std::string name; int age; void sayHello() { // 成员变量可以直接访问 std::cout << "Hello, my name is " << name << " and I'm " << age << " years old." << std::endl; } }; class Student : public Person { public: int studentId; void sayHello() override { // 子类重写父类的成员函数 std::cout << "Hello, my name is " << name << " and I'm " << age << " years old. My student ID is " << studentId << "." << std::endl; } };
###3.4 多态多态是指一个类实例可以表现出多种形态的能力。例如,一个 `Person` 对象也可以表现出 `Student` 的行为。
cppclass Person { public: virtual void sayHello() { std::cout << "Hello, my name is " << name << " and I'm " << age << " years old." << std::endl; } }; class Student : public Person { public: int studentId; void sayHello() override { // 子类重写父类的成员函数 std::cout << "Hello, my name is " << name << " and I'm " << age << " years old. My student ID is " << studentId << "." << std::endl; } }; int main() { Person person; Student student; // 多态的使用 person.sayHello(); student.sayHello(); return0; }
###3.5 虚函数和纯虚函数虚函数是可以在子类中重写的成员函数。纯虚函数是没有实现的虚函数。
cppclass Person { public: virtual void sayHello() =0; // 纯虚函数}; class Student : public Person { public: int studentId; void sayHello() override { std::cout << "Hello, my name is " << name << " and I'm " << age << " years old. My student ID is " << studentId << "." << std::endl; } };
###3.6 类的构造函数和析构函数类的构造函数是用于初始化类实例的函数。类的析构函数是用于释放类资源的函数。
cppclass Person { public: Person() { std::cout << "Person constructor called." << std::endl; } ~Person() { std::cout << "Person destructor called." << std::endl; } };
###3.7 类的拷贝构造函数和赋值运算符类的拷贝构造函数是用于复制类实例的函数。类的赋值运算符是用于将一个类实例赋给另一个类实例的运算符。
cppclass Person { public: Person(const Person& other) { std::cout << "Person copy constructor called." << std::endl; } Person& operator=(const Person& other) { std::cout << "Person assignment operator called." << std::endl; return *this; } };
###3.8 类的移动构造函数和移动赋值运算符类的移动构造函数是用于移动类实例的函数。类的移动赋值运算符是用于将一个类实例移动给另一个类实例的运算符。
cppclass Person { public: Person(Person&& other) noexcept { std::cout << "Person move constructor called." << std::endl; } Person& operator=(Person&& other) noexcept { std::cout << "Person move assignment operator called." << std::endl; return *this; } };
###3.9 类的友元函数类的友元函数是可以访问类私有成员的函数。
cppclass Person { public: friend void printName(Person& person) { std::cout << "Person's name is " << person.name << "." << std::endl; } };
###3.10 类的友元类类的友元类是可以访问类私有成员的类。
cppclass Person { public: friend class Printer; // Printer 是 Person 的友元类}; class Printer { public: void printName(Person& person) { std::cout << "Person's name is " << person.name << "." << std::endl; } };
###3.11 类的静态成员类的静态成员是属于整个类而不是类实例的成员。
cppclass Person { public: static int count; // 静态成员变量}; int Person::count =0; // 初始化静态成员变量
###3.12 类的静态成员函数类的静态成员函数是属于整个类而不是类实例的成员函数。
cppclass Person { public: static void printCount() { // 静态成员函数 std::cout << "Person count is " << count << "." << std::endl; } };
###3.13 类的枚举类的枚举是用于定义一组相关值的枚举。
cppclass Person { public: enum Color { RED, GREEN, BLUE }; // 枚举}; Person person; person.Color = Person::RED; // 使用枚举
###3.14 类的类型别名类的类型别名是用于定义一个新的类型名称。
cppclass Person { public: using NameType = std::string; // 类型别名}; Person person; person.NameType name = "John"; // 使用类型别名
###3.15 类的常量成员类的常量成员是不能修改的成员。
cppclass Person { public: static const int MAX_AGE =100; // 常量成员变量}; Person person; person.MAX_AGE =120; // 错误,MAX_AGE 是常量
###3.16 类的constexpr成员类的constexpr成员是可以在编译时计算的成员。
cppclass Person { public: static constexpr int MAX_AGE =100; // constexpr 成员变量}; Person person; person.MAX_AGE =120; // 错误,MAX_AGE 是constexpr
###3.17 类的decltype成员类的decltype成员是用于定义一个类型。
cppclass Person { public: decltype(auto) getName() { return name; } // decltype 成员函数}; Person person; auto name = person.getName(); // 使用decltype
###3.18 类的auto成员类的auto成员是用于定义一个类型。
cppclass Person { public: auto getName() -> std::string { return name; } // auto 成员函数}; Person person; std::string name = person.getName(); // 使用auto
###3.19 类的if constexpr成员类的if constexpr成员是用于定义一个条件编译。
cppclass Person { public: if constexpr (std::is_same_v<decltype(name), std::string>) { // if constexpr 成员变量 name = "John"; } }; Person person; person.name = "Jane"; // 使用if constexpr
###3.20 类的template