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()是公共成员函数。
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 members
private:
char name [MAX_CHAR];
int age;
//public member functions
public:
//function to get name and age
void get(char n[], int a)
{
strcpy(name , n);
age = a;
}
//function to print name and age
void put()
{
cout<< "Name: " << name <<endl;
cout<< "Age: " <<age <<endl;
}
};
//main function
int main()
{
//creating an object of person class
person PER;
//calling member functions
PER.get("Manju Tomar", 23);
PER.put();
return 0;
}
Output
输出量
Name: Manju Tomar
Age: 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 ++类成员函数