c++ cdi+示例_示例中C ++中Private和Protected之间的区别

c++ cdi+示例

In the concept of C++ class objects, there are three access modifiers: 1) private, 2) protected and 3) public. They are used based on their properties. Here, we will understand what are private and protected members and what are the differences between them?

在C ++类对象的概念中,有三种访问修饰符:1) private ,2) protected和3) public 。 根据其属性使用它们。 在这里,我们将了解什么是私有成员和受保护成员,它们之间有什么区别?

1)私人会员 (1) Private members)

Private members are declared with the keyword private followed by the colon (:) character in the class and they are accessible within the class only in which they are declared. Private members cannot be accessed outside of the class.

私有成员宣布与关键字私人后跟冒号(:))的阶级性, 他们只能在声明它们的类中访问 。 班级之外无法访问私人成员。

Private members can be data members (variables) and member functions (functions) and both can be accessed within the same class, they can be used within the private/public member functions of the same class.

私有成员可以是数据成员(变量)和成员函数(函数),并且两者都可以在同一类中访问,它们可以在同一类的私有/公共成员函数中使用。

Consider the below example, it contains two private members name and city and they are being used within the public member functions getPerson() and dispPerson().

考虑下面的示例,它包含两个私有成员名称和城市,并且它们在公共成员函数getPerson()和dispPerson()中使用 。

// example of private members

#include <iostream>
using namespace std;

class person {
    // private memebers
private:
    string name;
    int age;

    // public memebers
public:
    void getPerson()
    {
        cout << "Enter name: ";
        cin >> name;
        cout << "Enter age: ";
        cin >> age;
    }
    void dispPerson()
    {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

// main function
int main()
{
    //creating object
    person per;
    per.getPerson();
    per.dispPerson();

    return 0;
}

Output

输出量

Enter name: Shivang
Enter age: 21
Name: Shivang
Age: 21

2)受保护的成员 (2) Protected members)

Protected members are declared with the keyword protected followed by the colon (:) character in the class and they are accessible within the class in which they are declared and also accessible in the derived or subclass. Protected members are used in the concept of inheritance. Just like Private Members Protected members cannot be accessed outside of the class (except in the derived class).

受保护成员声明为保护的关键字,随后冒号(:)在类字符和它们在它们所声明的类内访问并在所导出的或亚类也可访问的 。 受保护的成员用于继承的概念。 就像私有成员一样,不能在类之外(在派生类中除外)访问受保护的成员。

Protected members can be data members (variables) and member functions (functions) and both can be accessed within the same class and derived or subclass or child class.

受保护的成员可以是数据成员(变量)和成员函数(函数),并且两者都可以在同一类,派生类,子类或子类中访问。

Consider the below example, there are two classes person which is a base class and person2 which a derived class, person class contains two private members name and city, one protected member roll_no and private members are being used within the public member functions getPerson() and dispPerson().

考虑下面的示例,有两个类, person是基类, person2是派生类, person类包含两个私有成员name和city ,一个受保护成员roll_no和私有成员在public成员函数getPerson()中使用和dispPerson() 。

In the person1 class, there is one private member city and three public member functions set_roll_no() – which is using to set the value of roll_no that is a protected member in person class, getPerson1() – which is calling the getPerson() member function of person class and also getting the input of city. Similarly, dispPerson1() – which is calling the dispPerson() member function and also printing the value of roll_no and city.

在person1类中,有一个私有成员城市和三个公共成员函数set_roll_no() –用于设置在个人类中受保护的成员roll_no的值, getPerson1() –调用getPerson()成员人类的功能,并得到城市的输入。 同样, dispPerson1() –调用dispPerson()成员函数,并打印roll_no和city的值。

// example of protected members

#include <iostream>
using namespace std;

// base class
class person {
    //private members
private:
    string name;
    int age;

    // protected member
protected:
    int roll_no;

    //public members
public:
    void getPerson()
    {
        cout << "Enter name: ";
        cin >> name;
        cout << "Enter age: ";
        cin >> age;
    }
    void dispPerson()
    {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

// derived class
class person1 : public person {
    // private members
private:
    string city;

    // public members
public:
    void set_roll_no(int r)
    {
        // here roll_no is the protected member of person
        // class, it is accessible here
        roll_no = r;
    }
    void getPerson1()
    {
        // calling getPerson() to read basic details
        getPerson();
        // input city
        cout << "Enter city: ";
        cin >> city;
    }
    void dispPerson1()
    {
        // displaying roll_no
        cout << "Roll No.: " << roll_no << endl;
        // calling dispPerson() to print basic details
        dispPerson();
        // displaying city also
        cout << "City: " << city << endl;
    }
};

// main function
int main()
{
    //creating object
    person1 per;
    // setting roll number
    per.set_roll_no(101);
    // getting all details
    per.getPerson1();
    // printing all details
    per.dispPerson1();

    return 0;
}

Output

输出量

Enter name: Shivang
Enter age: 21
Enter city: Indore
Roll No.: 101
Name: Shivang
Age: 21
City: Indore

私人成员与受保护成员之间的区别 (Difference between private and protected members)

Private membersProtected members
Private members are declared with the keyword private followed by a colon (:) character.Protected members are declared with the keyword protected followed by a colon (:) character.
Private members are accessible within the same class in which they are declared.Protected members are accessible within the same class and within the derived/sub/child class.
Private members can also be accessed through the friend function.Protected members cannot be accessed through the friend function.
私人会员 受保护的成员
私有成员宣布与关键字私人后跟一个冒号(:)字符。 受保护成员的声明与关键字保护的后面跟着冒号(:)字符。
私有成员可以在声明它们的同一类中访问。 受保护的成员可以在同一类中以及在派生/子/子类中访问。
也可以通过朋友功能访问私人成员。 无法通过好友功能访问受保护的成员。

翻译自: https://www.includehelp.com/cpp-tutorial/private-vs-protected-in-cpp.aspx

c++ cdi+示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值