c ++类成员函数_C ++编程中的数据成员和成员函数
c ++類(lèi)成員函數(shù)
C ++中的數(shù)據(jù)成員和成員函數(shù) (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.
“數(shù)據(jù)成員”和“成員函數(shù)”是類(lèi)成員的新名稱(chēng)/術(shù)語(yǔ),以C ++編程語(yǔ)言引入。
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.
通過(guò)使用任何基本數(shù)據(jù)類(lèi)型 (例如int,char,float等)或派生數(shù)據(jù)類(lèi)型(例如類(lèi),結(jié)構(gòu),指針等)在任何類(lèi)中聲明的變量稱(chēng)為數(shù)據(jù)成員 。 并且在公共部分的私有部分中聲明的函數(shù)稱(chēng)為成員函數(shù) 。
There are two types of data members/member functions in C++:
C ++中有兩種類(lèi)型的數(shù)據(jù)成員/成員函數(shù) :
Private members
私人會(huì)員
Public members
公眾成員
1)私人會(huì)員 (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.
在類(lèi)的私有部分中聲明的成員(使用private訪問(wèn)修飾符)稱(chēng)為私有成員。 私有成員也可以在聲明它們的同一類(lèi)中訪問(wèn)。
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.
在類(lèi)的公共部分聲明的成員(使用public access修飾符)被稱(chēng)為公共成員。 公共成員可以使用聲明它們的類(lèi)的對(duì)象名稱(chēng)在類(lèi)內(nèi)和類(lèi)外進(jìn)行訪問(wèn)。
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是私有數(shù)據(jù)成員, count是公共數(shù)據(jù)成員。 而getA()是私有成員函數(shù),而getB()是公共成員函數(shù)。
.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?
將演示如何在類(lèi)中聲明,定義和訪問(wèn)數(shù)據(jù)成員成員函數(shù)的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;
正如我們?cè)诔绦蛑锌吹降哪菢?#xff0c;可以通過(guò)使用句點(diǎn)(點(diǎn))運(yùn)算符(例如object_name.member_name; )直接在成員函數(shù)內(nèi)訪問(wèn)私有成員,并在main()函數(shù)內(nèi)(類(lèi)外部)訪問(wèn)成員函數(shù)。
翻譯自: https://www.includehelp.com/cpp-tutorial/data-members-and-member-functions.aspx
c ++類(lèi)成員函數(shù)
總結(jié)
以上是生活随笔為你收集整理的c ++类成员函数_C ++编程中的数据成员和成员函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c语言编程输入a是输出为a_C ++编程
- 下一篇: 数组copyWithin()方法以及Ja