技术

C++ 面向对象编程(OOP)


中文English
面向对象编程(OOP) 是一种程序设计范式,强调封装、继承、多态。Object-Oriented Programming (OOP) is a programming paradigm emphasizing encapsulation, inheritance, and polymorphism.
类和对象Classes and Objects
类是用户自定义类型,包含属性和方法;对象是类的实例。A class is a user-defined type containing attributes and methods; an object is an instance of a class.
封装Encapsulation
将数据和操作封装在类内,通过访问控制符(public、private、protected)实现访问限制。Binds data and operations inside a class; access control (public, private, protected) restricts access.
继承Inheritance
子类继承父类成员,实现代码复用,可重写基类函数。Subclasses inherit members from base classes, enabling code reuse and function overriding.
多态Polymorphism
不同对象对同一接口有不同实现,静态多态通过重载和模板实现,动态多态通过虚函数实现。Different objects implement the same interface differently; static polymorphism via overloading/templates, dynamic via virtual functions.
构造函数和析构函数Constructors and Destructors
构造函数初始化对象,析构函数负责资源释放。Constructors initialize objects; destructors release resources.
抽象类和接口Abstract Classes and Interfaces
包含纯虚函数的类称为抽象类,不能实例化,用作接口定义。A class with pure virtual functions is abstract, cannot be instantiated, used to define interfaces.
运算符重载Operator Overloading
允许自定义类对象使用操作符,实现类间操作的自然表达。Allows custom operators for classes, enabling natural syntax for object operations.
友元类和友元函数Friend Classes and Friend Functions
友元可以访问类的私有成员,通常用于特殊场景。Friends can access private members of a class, usually for special cases.

| 二义性(Ambiguity) | | Ambiguity |
| 多重继承时,如果多个基类含相同成员,访问时会发生二义性。 | | In multiple inheritance, if base classes have identical members, ambiguity occurs during access. |
| 需用作用域运算符明确调用基类成员。 | | Use scope resolution operator to specify which base member to call. |


| 类型判断(Type Identification) | | Type Identification |
| C++提供 typeid 运算符,用于获取对象的实际类型信息。 | | C++ provides the typeid operator to get the actual type info of an object. |
| 需要开启运行时类型信息(RTTI)。 | | Requires enabling Runtime Type Information (RTTI). |


| 动态类型转换(Dynamic Type Casting) | | Dynamic Type Casting |
| 使用 dynamic_cast 安全转换指针或引用,适用于多态类型。 | | Use dynamic_cast for safe pointer/reference conversions, applicable to polymorphic types. |
| 转换失败返回 nullptr(指针)或抛异常(引用)。 | | Returns nullptr (for pointers) or throws exception (for references) on failure. |
| 性能开销较大,尽量减少使用。 | | Has performance overhead; minimize usage. |


| 虚函数(Virtual Functions)和重载函数(Overloaded Functions)实例 | | Virtual Functions and Overloaded Functions Example |

class Base {

public:

    virtual void speak() { std::cout << “Base speak\n”; }

    void speak(int) { std::cout << “Base speak with int\n”; }

};

class Derived : public Base {

public:

    void speak() override { std::cout << “Derived speak\n”; }

};

int main() {

    Base* b = new Derived();

    b->speak();       // 输出 Derived speak(动态绑定)

    b->speak(10);     // 调用 Base 重载函数(静态绑定)

    delete b;

    return 0;

}


| 友元类和函数的概念和实例 | | Friend Classes and Functions Concept and Example |

class A {

private:

    int secret = 42;

    friend class B;

};

class B {

public:

    void revealSecret(A& a) {

        std::cout << a.secret << std::endl;

    }

};