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 ++创建具有公共数据成员的类的全部內容,希望文章能夠幫你解決所遇到的問題。