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

c ++类成员函数

Let’s understand

让我们来了解

  1. What is data member?

    什么是数据成员?

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

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

  2. 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 object
	Number N;
	
	//setting value to public data member 
	N.num = 100;
	
	//printing value
	cout<<"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 ++类成员函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值