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: 23As 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 ++编程中的数据成员和成员函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言编程输入a是输出为a_C ++编程
- 下一篇: Log4j漏洞?一行代码都不改就能永久修