日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c ++类成员函数_C ++编程中的数据成员和成员函数

發布時間:2025/3/11 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c ++类成员函数_C ++编程中的数据成员和成员函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c ++類成員函數

C ++中的數據成員和成員函數 (Data members and Member functions in C++)

"Data Member" and "Member Functions" are the new names/terms for the members of a class, which are introduced in C++ programming language.

“數據成員”和“成員函數”是類成員的新名稱/術語,以C ++編程語言引入。

The variables which are declared in any class by using any fundamental data types (like int, char, float etc) or derived data type (like class, structure, pointer etc.) are known as Data Members. And the functions which are declared either in private section of public section are known as Member functions.

通過使用任何基本數據類型 (例如int,char,float等)或派生數據類型(例如類,結構,指針等)在任何類中聲明的變量稱為數據成員 。 并且在公共部分的私有部分中聲明的函數稱為成員函數 。

There are two types of data members/member functions in C++:

C ++中有兩種類型的數據成員/成員函數 :

  • Private members

    私人會員

  • Public members

    公眾成員

  • 1)私人會員 (1) Private members)

    The members which are declared in private section of the class (using private access modifier) are known as private members. Private members can also be accessible within the same class in which they are declared.

    在類的私有部分中聲明的成員(使用private訪問修飾符)稱為私有成員。 私有成員也可以在聲明它們的同一類中訪問。

    2)公眾成員 (2) Public members)

    The members which are declared in public section of the class (using public access modifier) are known as public members. Public members can access within the class and outside of the class by using the object name of the class in which they are declared.

    在類的公共部分聲明的成員(使用public access修飾符)被稱為公共成員。 公共成員可以使用聲明它們的類的對象名稱在類內和類外進行訪問。

    Consider the example:

    考慮示例:

    class Test {private:int a;float b;char *name;void getA() { a=10; }...;public:int count;void getB() { b=20; }...; };

    Here, a, b, and name are the private data members and count is a public data member. While, getA() is a private member function and getB() is public member functions.

    這里, a , b和name是私有數據成員, count是公共數據成員。 而getA()是私有成員函數,而getB()是公共成員函數。

    .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    C++ program that will demonstrate, how to declare, define and access data members an member functions in a class?

    將演示如何在類中聲明,定義和訪問數據成員成員函數的C ++程序?

    #include <iostream> #include <string.h> using namespace std;#define MAX_CHAR 30//class definition class person {//private data membersprivate: char name [MAX_CHAR];int age;//public member functionspublic: //function to get name and agevoid get(char n[], int a){strcpy(name , n);age = a;}//function to print name and agevoid put(){cout<< "Name: " << name <<endl;cout<< "Age: " <<age <<endl;} };//main function int main() {//creating an object of person classperson PER;//calling member functionsPER.get("Manju Tomar", 23);PER.put();return 0; }

    Output

    輸出量

    Name: Manju TomarAge: 23

    As we can see in the program, that private members are directly accessible within the member functions and member functions are accessible within in main() function (outside of the class) by using period (dot) operator like object_name.member_name;

    正如我們在程序中看到的那樣,可以通過使用句點(點)運算符(例如object_name.member_name; )直接在成員函數內訪問私有成員,并在main()函數內(類外部)訪問成員函數。

    翻譯自: https://www.includehelp.com/cpp-tutorial/data-members-and-member-functions.aspx

    c ++類成員函數

    總結

    以上是生活随笔為你收集整理的c ++类成员函数_C ++编程中的数据成员和成员函数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。