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

  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()是公共成员函数。

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545123.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一文学搞懂阿里开源的微服务新贵Nacos!

正式开始之前我们先来了解一下什么是 Nacos&#xff1f;Nacos 是阿里的一个开源产品&#xff0c;它是针对微服务架构中的 「服务发现」、「配置管理」、「服务治理」的综合性解决方案。官网给出的回答&#xff1a;“Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组…

ntfs安全权限和共享权限的区别

ntfs安全权限和共享权限的区别 win xp 最大分区32G,最大文件大小4G. 共享权限是为网络用户设置的&#xff0c;NTFS权限是对文件夹设置的。用户对文件夹有什么权限就是看NTFS权限的设置。如果一个文件夹设置成共享&#xff0c;其具体的权限还是在NTFS权限上面设置的&#xff0c;…

The connection to adb is down, and a severe error has occured.

转自&#xff1a;http://blog.csdn.net/yu413854285/article/details/7559333 &#xff08;感谢原文作者&#xff0c;问题解决&#xff09; 启动android模拟器时.有时会报The connection to adb is down, and a severe error has occured.的错误.在网友说在任务管理器上把所有…

kotlin 16进制_Kotlin程序将八进制数转换为十进制数

kotlin 16进制Given a number in octal number system format, we have to convert it into decimal number system format. 给定八进制系统格式的数字&#xff0c;我们必须将其转换为十进制系统格式。 Example: 例&#xff1a; Input:num 344Output:228在Kotlin中将八进制数…

线程池的7种创建方式,强烈推荐你用它...

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;根据摩尔定律所说&#xff1a;集成电路上可容纳的晶体管数量每 18 个月翻一番&#xff0c;因此 CPU 上的晶体管数量会越来越…

SQL调用C# dll(第一中DLL,没使用强名称密匙,默认是 safe)

https://msdn.microsoft.com/zh-cn/library/ms345106(es-es).aspx 1、新建项目名称SQLDllTest&#xff0c;类代码如下&#xff0c;没有用Using引用其他类&#xff1a; &#xff08;框架必须改为.NET3.5及3.5以下&#xff0c;因为SQL Server 2008只是支持.NET 3.5及一下&#xf…

Linux系统下启动MySQL的命令及相关知识

一、总结一下&#xff1a; 1.Linux系统下启动MySQL的命令&#xff1a; /ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令&#xff1a; /ect/init.d/mysql restart (前面为mysql的安装路径) 3.linux下关闭mysql的命令&#xff1a; /ect/init.d/mysql …

线性代数向量乘法_标量乘法属性1 | 使用Python的线性代数

线性代数向量乘法Prerequisite: Linear Algebra | Defining a Vector 先决条件&#xff1a; 线性代数| 定义向量 Linear algebra is the branch of mathematics concerning linear equations by using vector spaces and through matrices. In other words, a vector is a mat…

Synchronized 的 8 种使用场景!

blog.csdn.net/x541211190/article/details/106272922简介本文将介绍8种同步方法的访问场景&#xff0c;我们来看看这8种情况下&#xff0c;多线程访问同步方法是否还是线程安全的。这些场景是多线程编程中经常遇到的&#xff0c;而且也是面试时高频被问到的问题&#xff0c;所…

Python的threadpool模块

2019独角兽企业重金招聘Python工程师标准>>> Python的threadpool模块 这是一个使用python实现的线程池库。 安装 pip install threadpool 文档 http://gashero.yeax.com/?p44 http://www.chrisarndt.de/projects/threadpool/ 测试 使用一个20个线程的线程池进行测试…

MySql常用命令总结

1:使用SHOW语句找出在服务器上当前存在什么数据库&#xff1a;mysql> SHOW DATABASES;2:2、创建一个数据库MYSQLDATAmysql> CREATE DATABASE MYSQLDATA;3:选择你所创建的数据库mysql> USE MYSQLDATA; (按回车键出现Database changed 时说明操作成功&#xff01;)4:查看…

硬核Redis总结,看这篇就够了!

高清思维导图已同步Git&#xff1a;https://github.com/SoWhat1412/xmindfile总感觉哪里不对&#xff0c;但是又说不上来1、基本类型及底层实现1.1、String用途&#xff1a;适用于简单key-value存储、setnx key value实现分布式锁、计数器(原子性)、分布式全局唯一ID。底层&…

sql 数字减去null_减去两个16位数字| 8086微处理器

sql 数字减去nullProblem: Write a program to subtract two 16-bit numbers where starting address is 2000 and the numbers are at 3000 and 3002 memory address and store result into 3004 and 3006 memory address. 问题&#xff1a;编写一个程序以减去两个16位数字(起…

Java 解决采集UTF-8网页空格变成问号乱码

http://blog.csdn.net/bob007/article/details/27098875 使用此方法转换后&#xff0c;在列表中看到的正常&#xff0c;但是在详情页的文本框中查看到的就是 了&#xff0c;只好过滤掉所有的空格 html html.replaceAll(UTFSpace, " ");改为html html.replaceAll(UT…

linux中如何改IP

修改IP永久生效按以下方法vi /etc/sysconfig/network-scripts/ifcfg-eth0&#xff08;eth0&#xff0c;第一块网卡&#xff0c;如果是第二块则为eth1&#xff09;按如下修改ipDEVICEeth0&#xff08;如果是第二块刚为eth1&#xff09;BOOTPROTOstaticIPADDR192.168.0.11(改成要…

文件写入的6种方法,这种方法性能最好

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在 Java 中操作文件的方法本质上只有两种&#xff1a;字符流和字节流&#xff0c;而字节流和字符流的实现类又有很多&#x…

单位矩阵属性(I ^ k = I)| 使用Python的线性代数

Prerequisites: 先决条件&#xff1a; numpy.matmul( ) matrix multiplication numpy.matmul()矩阵乘法 Identity matrix 身份矩阵 In linear algebra, the identity matrix, of size n is the n n square matrix with ones on the main diagonal and zeros elsewhere. It is…

linux 更改文件权限(子文件夹)

加入-R 参数&#xff0c;就可以将读写权限传递给子文件夹例如chmod -R 777 /home/mypackage那么mypackage 文件夹和它下面的所有子文件夹的属性都变成了777.777是读、写、执行权限...

JavaScript中带有示例的Math.cos()方法

JavaScript | Math.cos()方法 (JavaScript | Math.cos() Method) Math.cos() is a function in math library of JavaScript that is used to find the value of cosine of a number and return the value in radians. Math.cos()是JavaScript数学库中的一个函数&#xff0c;用…

JDK 16 即将发布,新特性速览!

你还能追上 Java 的更新速度吗&#xff1f;当开发者深陷 Java 8 版本之际&#xff0c;这边下一版本 Java 16 有了最新的消息&#xff0c;与 Java 15 一样&#xff0c;作为短期版本&#xff0c;Oracle 仅提供 6 个月的支持。根据发布计划&#xff0c;JDK 16 将在 12 月 10 日和 …