The respective functions of the keywords public, private, and protected, as well as the definition of the constructor

Table of contents

1. The role of keywords public, private, protected

Second, the definition of the constructor


1. The role of keywords public, private, protected

In C++, keywords public, privateand protectedare used to define the access rights of member variables and member functions of a class. They work as follows:

  • public: A public member that can be accessed by any function, object, or outside of the class.
  • private: Private member, which can only be accessed by member functions of the current class, and cannot be directly accessed outside the class.
  • protected: Protected members, which can only be accessed by member functions of the current class and its subclasses, and cannot be directly accessed outside the class.

The following is a sample code with detailed comments for each access keyword:

#include <iostream>

class MyClass {
public:
    // 公共成员函数,可以被任意对象和类外部访问
    void publicFunc() {
        std::cout << "This is a public function" << std::endl;
    }

protected:
    // 受保护的成员变量,只能被当前类及其子类访问
    int protectedVar;

    // 受保护的成员函数,只能被当前类及其子类访问
    void protectedFunc() {
        std::cout << "This is a protected function" << std::endl;
    }

private:
    // 私有成员变量,只能被当前类的成员函数访问
    int privateVar;

    // 私有成员函数,只能被当前类的成员函数访问
    void privateFunc() {
        std::cout << "This is a private function" << std::endl;
    }
};

class MyDerivedClass : public MyClass {
public:
    // 子类的公共成员函数,可以被任意对象和类外部访问
    void derivedPublicFunc() {
        std::cout << "This is a derived public function" << std::endl;

        // 可以访问基类的保护成员变量
        protectedVar = 10;

        // 可以调用基类的保护成员函数
        protectedFunc();
    }
};

int main() {
    MyClass myObj;

    // 可以调用公共成员函数
    myObj.publicFunc();

    // 下面两行代码都会导致编译错误,因为 private 和 protected 成员无法在类外部访问
    // myObj.privateVar;
    // myObj.privateFunc();

    MyDerivedClass derivedObj;

    // 可以调用公共成员函数
    derivedObj.derivedPublicFunc();

    // 下面两行代码都会导致编译错误,因为 private 成员无法在子类中访问
    // derivedObj.privateVar;
    // derivedObj.privateFunc();

    // 可以访问基类的保护成员变量
    derivedObj.protectedVar = 20;

    // 可以调用基类的保护成员函数
    derivedObj.protectedFunc();

    return 0;
}

Second, the definition of the constructor

A constructor is a special function used to initialize member variables of an object or perform some other initialization operations when creating an object. The constructor has the same name as the class and does not require a return type and cannot be called manually. When creating a new object, the compiler will automatically call the corresponding constructor of the object.

Here is a simple example showing how to define and use a constructor:

#include <iostream>

// 定义名为 MyClass 的类
class MyClass {
public:
    // 构造函数,接受一个 int 类型参数 value
    MyClass(int value) {
        _value = value;
        // 输出一条消息,表示一个新的对象被创建了,并打印出对象的值
        std::cout << "A new object is created with value " << _value << std::endl;
    }
    // 成员函数,用于打印出对象的值
    void printValue() {
        std::cout << "The value of this object is " << _value << std::endl;
    }
private:
    int _value; // 私有变量 _value,用于存储对象的值
};

// 主函数
int main() {
    // 创建一个 MyClass 类型的对象,传入参数值为 10
    MyClass myObj(10);
    // 调用对象的成员函数 printValue(),输出对象的值
    myObj.printValue();
    return 0;
}

In the above example, MyClassthe class defines a constructor MyClass(int value)that initializes member variables _value. When a new MyClassobject is created, the constructor is automatically called and the parameters passed in are assigned to it _value. In mainthe function, a new MyClassobject is created and its value is output.

Guess you like

Origin blog.csdn.net/qq_50635297/article/details/130027985