c++ cdi+示例
公开数据成员 (Public Data Members)
All variables of a class are known as "data members of a class" and the variables which are declared in the public section of a class are known as "public data members of a class".
类的所有变量称为“类的数据成员”,而在类的public部分声明的变量称为“类的公共数据成员” 。
Public data members can be accessed through object name directly outside of the class.
可以通过类外部的对象名称直接访问公共数据成员 。
Example:
例:
Here, we have a class named "Numbers" with two public data members a and b, we will provide the value to them inside the main() and access (print) them inside main() also.
在这里,我们有一个类名为“数字”有两个公共数据成员a和b,我们将提供价值给他们的main()和接入(打印)他们内部的main()也在里面。
Program:
程序:
#include <iostream>
using namespace std;
class Numbers
{
public:
int a;
int b;
};
//Main function
int main()
{
//declaring object to the class
Numbers Num;
//assign values to a and b
Num.a = 100;
Num.b = 200;
//print the values
cout<<"Value of Num.a: "<<Num.a<<endl;
cout<<"Value of Num.b: "<<Num.b<<endl;
return 0;
}
Output
输出量
Value of Num.a: 100
Value of Num.b: 200
翻译自: https://www.includehelp.com/cpp-programs/example-of-public-data-members.aspx
c++ cdi+示例