c++ cdi+示例_演示C ++中的公共数据成员示例

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+示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值