c ++类成员函数_仅使用C ++创建具有公共数据成员的类
c ++類成員函數(shù)
Let’s understand
讓我們來了解
What is data member?
什么是數(shù)據(jù)成員?
Any variable declared inside the class in known as 'data member' of the class.
在類內(nèi)部聲明的任何變量,稱為類的“數(shù)據(jù)成員”。
What is public data member?
什么是公共數(shù)據(jù)成員?
A variable declared inside the 'public' section of the class is known as 'public data member'.
在類的“公共”部分內(nèi)聲明的變量稱為“公共數(shù)據(jù)成員”。
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.
在此示例中,我們將使用公共數(shù)據(jù)成員創(chuàng)建一個(gè)類,可以使用對(duì)象名稱在該類之外訪問公共數(shù)據(jù)成員,并且可以設(shè)置公共數(shù)據(jù)成員的值并使用其對(duì)象名在該類之外。
So, here in this example,
因此,在此示例中,
Number is the class name.
數(shù)字是班級(jí)名稱。
num is an integer data member, which is declared in the public section of the class - so it is a public data member.
num是一個(gè)整數(shù)數(shù)據(jù)成員,它在該類的public部分中聲明-因此它是一個(gè)公共數(shù)據(jù)成員。
In the main, N is the object to class Number.
基本上, N是Number類的對(duì)象。
N.num is using to set and then get the value.
N.num用于設(shè)置然后獲取值。
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 ++類成員函數(shù)
總結(jié)
以上是生活随笔為你收集整理的c ++类成员函数_仅使用C ++创建具有公共数据成员的类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蚂蝗多少钱一斤啊?
- 下一篇: c中将数组传递给子函数_在C ++中将对