欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

c ++类成员函数_仅使用C ++创建具有公共数据成员的类

发布时间:2023/12/1 编程问答 55 豆豆
生活随笔 收集整理的这篇文章主要介绍了 c ++类成员函数_仅使用C ++创建具有公共数据成员的类 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

c ++类成员函数

Let’s understand

让我们来了解

  • What is data member?

    什么是数据成员?

    Any variable declared inside the class in known as 'data member' of the class.

    在类内部声明的任何变量,称为类的“数据成员”。

  • What is public data member?

    什么是公共数据成员?

    A variable declared inside the 'public' section of the class is known as 'public data member'.

    在类的“公共”部分内声明的变量称为“公共数据成员”。

  • In this example, we are going to create a class with the pubic data members, a public data member can be accessed outside of the class with object name, and public data member’s value can be set and get outside of the class with its object name.

    在此示例中,我们将使用公共数据成员创建一个类,可以使用对象名称在该类之外访问公共数据成员,并且可以设置公共数据成员的值并使用其对象名在该类之外。

    So, here in this example,

    因此,在此示例中,

    • Number is the class name.

      数字是班级名称。

    • num is an integer data member, which is declared in the public section of the class - so it is a public data member.

      num是一个整数数据成员,它在该类的public部分中声明-因此它是一个公共数据成员。

    • In the main, N is the object to class Number.

      基本上, N是Number类的对象。

    • N.num is using to set and then get the value.

      N.num用于设置然后获取值。

    Example:

    例:

    #include <iostream> using namespace std;//class declaration class Number{public:int num; };//Main function int main() {//creating objectNumber N;//setting value to public data member N.num = 100;//printing valuecout<<"value of N.num = "<<N.num<<endl;return 0; }

    Output

    输出量

    value of N.num = 100

    翻译自: https://www.includehelp.com/cpp-programs/create-a-class-with-public-data-members-only-in-cpp.aspx

    c ++类成员函数

    总结

    以上是生活随笔为你收集整理的c ++类成员函数_仅使用C ++创建具有公共数据成员的类的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。