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 ++中有两种类型的数据成员/成员函数

  1. Private members

    私人会员

  2. 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 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 ++类成员函数

c ++类成员函数_C ++编程中的数据成员和成员函数相关推荐

  1. 编程实现strcpy函数_C编程中的strcpy()

    编程实现strcpy函数 We earlier learned about strings in C programming in our Strings in C tutorial. Today, ...

  2. java sign函数_C / C ++中是否有标准的符号函数(signum,sgn)?

    我想要一个返回负数为-1和正数为+1的函数. http://en.wikipedia.org/wiki/Sign_function编写我自己的很容易,但是似乎应该放在标准库的某个地方. 编辑:具体来说 ...

  3. matlab path路径定义文件,MATLAB基本操作(七):有关文件路径的几个函数在编程中的作用...

    有关文件路径的几个函数在编程中的作用 Last Edit 2014/1/16 1,genpath: (gen=generate,顾名思义就是一个产生路径的函数,这个路径须存在) genpathgenp ...

  4. 算法训练 - P1101 ——有一份提货单,其数据项目有:商品名(MC)、单价(DJ)、数量(SL)。定义一个结构体prut,其成员是上面的三项数据。在主函数中定义一个prut类型的结构体数组,输入每

    问题描述 有一份提货单,其数据项目有:商品名(MC).单价(DJ).数量(SL).定义一个结构体prut,其成员是上面的三项数据.在主函数中定义一个prut类型的结构体数组,输入每个元素的值,计算并输 ...

  5. pandas使用itertuples函数迭代dataframe中的数据行并自定义修改行中的数值(update row while iterating over the rows)

    pandas使用itertuples函数迭代dataframe中的数据行并自定义修改行中的数值(update row while iterating over the rows in datafram ...

  6. python使用statsmodels包中的robust.mad函数以及pandas的apply函数计算dataframe中所有数据列的中位数绝对偏差(MAD)

    python使用statsmodels包中的robust.mad函数以及pandas的apply函数计算dataframe中所有数据列的中位数绝对偏差(MAD.Median Absolute Devi ...

  7. R语言使用edit函数在Rsudio中生成数据编辑器(在windows中生成编辑器)、在编辑器中输出需要的数据生成最终的dataframe

    R语言使用edit函数在Rsudio中生成数据编辑器(在windows中生成编辑器).在编辑器中输出需要的数据生成最终的dataframe 目录

  8. pandas使用replace函数替换dataframe中的值:replace函数对dataframe中指定数据列的值进行替换、替换具体数据列的相关值

    pandas使用replace函数替换dataframe中的值:replace函数对dataframe中指定数据列的值进行替换.替换具体数据列的相关值 目录

  9. 阿里面试题:Pandas中合并数据的5个函数,各有千秋!

    前几天在一个群里面,看到一位朋友,说到自己的阿里面试,被问了一些关于pandas的使用.其中一个问题是:pandas中合并数据的5中方法. 今天借着这个机会,就为大家盘点一下pandas中合并数据的5 ...

最新文章

  1. linux sshd cpu 过高 问题解决
  2. 操作系统:了解一下磁盘结构
  3. MySQL知识点复习
  4. 小米小爱蓝牙音箱_小米小爱蓝牙音箱——值否?
  5. 模板:线段树标记永久化
  6. jquery ajax mysql登录_ajax基础知识、用ajax做登录页面、用ajax验证用户名是否可用、ajax动态调用数据库...
  7. 简单的beego分页功能代码
  8. Linux 多线程编程使用pthread_creat()函数条件
  9. 使用 pyinstaller 打包 py 文件成 exe 程序
  10. 解决ubuntu20.04虚拟机无法上网的问题
  11. 【零经验】游戏制作全流程攻略(Unity、个人开发、找工作)
  12. 人、猕猴、小鼠静息态三重网络
  13. 单片机和嵌入式区别?
  14. 苦才是人生的经典美言
  15. CSS 渐变背景 之 线性渐变
  16. Android中使用AudioManager实现按键录音并保存以及点击播放功能
  17. 尝试“Android应用图标微技巧,8.0系统中应用图标的适配”
  18. Google文件系统
  19. 给她讲最爱的SpringBoot源码
  20. 根据用户给出的年份求天干、地支和生肖(Java源代码)

热门文章

  1. 西门子触摸屏修改ip地址_基于博途V15 西门子S7-1200与触摸屏一个简单的例子
  2. python stdin和stdout_stdin似乎比stdout(python)慢得多.为什么?
  3. linux上dig命令,Linux dig命令(示例代码)
  4. oracle的undo表空间不足,undo表空间不足,ORA-30036 unable to extend segment by ...
  5. 【干货】分享总结:MySQL数据一致性
  6. linux 标准 GPIO 操作
  7. 北京爱情故事-经典语录
  8. [置顶] 任务三 主题、样式
  9. 余宏德:Sun所有的核心技术都是开放的
  10. 网管日志-06.07.24