【C++ grammar】抽象、封装与this指针

目录

  • 1、Abstraction and Encapsulation(抽象与封装)
    • 1. Data Field Encapsulation (数据域封装)
    • 2. Accessor and Mutator (访问器与更改器)
      • 2.1. To read/write private data, we need get/set function (为读写私有数据,需要get/set函数)
      • 2.2. Signature of get function (General form) (get函数的一般原型)
      • 2.3. Signature of get function (Bool type) (布尔型get函数的原型)
      • 2.4. Signature of set function (set函数的原型)
    • 3. Class Abstraction and Encapsulation (类抽象与封装)
      • 3.1. Class abstraction (类抽象)
      • 3.2. Class encapsulation (类封装)
      • 3.3. 总结
  • 2、The Scope of Members & "this" pointer(成员作用域与this指针)
    • 1. The Scope of Data Members in Class (数据成员的作用域)
    • 2. Hidden by same name (同名屏蔽)
    • 3. The this Pointer (this指针)
    • 4. Simple way to avoid name hidden (避免重名屏蔽的简单方法)这是一种比this指针更加简单的方法
    • 5. 编码规范
    • 6、需要注意的地方

1、Abstraction and Encapsulation(抽象与封装)

1. Data Field Encapsulation (数据域封装)

数据域采用public的形式有2个问题
(1) First, data may be tampered. ( 数据会被类外的方法篡改)
(2) Second, it makes the class difficult to maintain and vulnerable to bugs. ( 使得类难于维护,易出现bug)

class Circle {
public:double radius;//……
};
// main() {circle1.radius=5; //类外代码可修改public数据

将radius放入私有区域进行封装,使得从外部不能直接访问radius。
在这里插入图片描述

如果我们想对radius进行赋值或者读取radius就需要使用到访问器与更改器。

2. Accessor and Mutator (访问器与更改器)

2.1. To read/write private data, we need get/set function (为读写私有数据,需要get/set函数)

(1) get function is referred to as a getter (获取器,or accessor),
(2) set function is referred to as a setter (设置器,or mutator).

2.2. Signature of get function (General form) (get函数的一般原型)

returnType getPropertyName()

2.3. Signature of get function (Bool type) (布尔型get函数的原型)

bool isPropertyName()

2.4. Signature of set function (set函数的原型)

void setPropertyName(dataType propertyValue)、

3. Class Abstraction and Encapsulation (类抽象与封装)

3.1. Class abstraction (类抽象)

The process of removing physical, spatial, or temporal details or attributes in the study of objects or systems in order to more closely attend to other details of interest ( 在研究对象或系统时,为了更加专注于感兴趣的细节,去除对象或系统的物理或时空细节/ 属性的过程叫做抽象)

3.2. Class encapsulation (类封装)

A language mechanism for restricting direct access to some of the object’s components.( 一种限制直接访问对象组成部分的语言机制)
A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data ( 一种实现数据和函数绑定的语言构造块)

3.3. 总结

抽象: 提炼目标系统中我们关心的核心要素的过程
封装: 绑定数据和函数的语言构造块,以及限制访问目标对象的内容的手段

2、The Scope of Members & “this” pointer(成员作用域与this指针)

1. The Scope of Data Members in Class (数据成员的作用域)

The data members are accessible to all constructors and functions in the class. (数据成员可被类内所有函数访问)
Data fields and functions can be declared in any order in a class. (数据域与函数可按任意顺序声明)
在这里插入图片描述

2. Hidden by same name (同名屏蔽)

If a local variable has the same name as a data field: (若成员函数中的局部变量与某数据域同名)

(1) the local variable takes precedence ( 局部变量优先级高:就近原则)
(2) the data field with the same name is hidden. ( 同名数据域在函数中被屏蔽)

3. The this Pointer (this指针)

How do you reference a class’s hidden data field in a function? (如何在函数内访问类中被屏蔽的数据域)? 可以使用 this 关键字
This 关键字的特性
(1) a special built-in pointer ( 特殊的内建指针)this指针不需要声明也不需要赋初值。
(2) references to the calling object. ( 引用当前函数的调用对象)
在这里插入图片描述
在这里插入图片描述

4. Simple way to avoid name hidden (避免重名屏蔽的简单方法)这是一种比this指针更加简单的方法

class Circle {
public:Circle();Circle(double radius_){//this->radius = radius;radius = radius_; }
private:double radius;
public:void setRadius(double);//……};

5. 编码规范

  1. If the parameter of a member function has the same name as a private class variable, then the parameter should have underscore
    suffix.

  2. 若类的成员函数参数与私有成员变量名相同,那么参数名应加下划线后缀

例:

class SomeClass {int length;
public:void setLength( int length_ );

6、需要注意的地方

1、我们不可以修改this指针的值,使之指向另外一个对象
2、this指针是自动初始化的、this指针指向调用当前函数的对象、我们不可以显示地声明this指针。

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

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

相关文章

java创建临时文件_用Java创建一个临时文件

java创建临时文件The task is to create a temporary file in Java. 任务是用Java创建一个临时文件。 Creating a temporary file 创建一个临时文件 To create a temporary file in java – we use createTempFile() method of "File" class. The createTempFile()…

十九、图像的形态学操作

一、图像形态学 图像形态学是图像处理学科的一个单独分支学科 主要针对的是灰度图和二值图像 是由数学的集合论以及数学中的拓扑几何原理发展而来 二、膨胀操作(dilate) 33的卷积核 以33为卷积核从左往右(从上往下)开始运行,若这卷积核…

X名称空间(WPF)

笔记简述 闲话x名称空间简要x名称空间的Attributex名称空间的标签扩展x名称空间的XAML指令元素闲话 本笔记参考与《深入浅出WPF》、MSDN、Some Blog… MSDN的飞机票点这里。 x名称空间简要 在VS中新建个WpfApplication都会自动生成xmlns:x"http://schemas.microsoft.com/w…

基于Bresenham和DDA算法画线段

直线:ykxb 为了将他在显示屏上显示出来,我们需要为相应的点赋值,那么考虑到计算机的乘法执行效率,我们肯定不会选择用Ykxb这个表达式求值,然后进行画线段。 我们应当是将它转化为加法运算。 下面提供两种常见的算法&am…

leetcode 106. 从中序与后序遍历序列构造二叉树 105. 从前序与中序遍历序列构造二叉树思考分析

目录1、106题目2、参考思路:递归切割数组3、105题目4、同样思路的代码1、106题目 2、参考思路:递归切割数组 代码参考:公众号:代码随想录 后序数组中序数组 以 后序数组(左右中)的最后一个元素作为切割点,先切中序数组…

按频率对元素进行排序

Prerequisite: 先决条件: Hashing data structure 散列数据结构 How to write user-defined comparator for priority queue STL in C? 如何在C 中为优先级队列STL编写用户定义的比较器? How to sort a map based on values instead of value? 如何根…

二十、分水岭算法

一、基本原理 分水岭算法主要是基于距离变换(distance transform),找到mark一些种子点,从这些种子点出发根据像素梯度变化进行寻找边缘并标记 分水岭:可以简单的理解成一座山,然后来洪水了,水开…

细数WOW里暴雪的“亲儿子”们

. 不知何时,魔兽世界的词汇中忽然出现了一个新玩意:亲儿子。虽说这个称呼现在大多是拿来调侃法爷的,但江山代有儿子出,各领风骚一两天,今天风光无限的法爷们也经历过被其他职业压得抬不起头的小媳妇生涯。那么今天…

Linux下串口ttyS2,ttyS3不能用的问题解决办法

PC104,Xlinux下,突然发现串口3,4不能用。。。 以为是硬件的问题,换成wince后,3,4工作正常,排除电路问题 在linux下查看dmesg: serial8250: ttyS0 at I/O 0x3f8 (irq 4) is a 16550Aserial8250: ttyS1 at I/O 0x2f8 (i…

安卓log.e函数打印示例_log1p()函数以及C ++中的示例

安卓log.e函数打印示例C log1p()函数 (C log1p() function) log1p() function is a library function of cmath header, it is used to get the natural logarithm (the base-e logarithm) of the one plus given value. It accepts a value (float, double, or long double) …

【C++grammar】C++类数据成员的初始化

目录1、类成员的就地初始化example2、构造函数初始化3、默认构造函数:Default Constructor4、举例5、成员的初始化方法的优先级1、类成员的就地初始化example class S { int m 7; // ok, copy-initializes m int n(7); // 错误:不允许用小括号初始化…

二十一、人脸检测

一、识别图像中的人脸 haarcascade_frontalface_alt_tree.xml lbpcascade_frontalcatface.xml GitHub上有Haar级联检测器源代码可自行下载,lbp级联检测器也一样有源码可自行下载 也一样 import cv2 as cv import numpy as npdef face_detect(image):gray cv.cvtC…

aspx特殊符号说明

http://www.cnblogs.com/GnagWang/archive/2010/07/14/1777130.html转载于:https://www.cnblogs.com/mingyongcheng/archive/2011/11/24/2261253.html

javascript运算符_JavaScript中的按位运算符

javascript运算符JavaScript按位运算符 (JavaScript Bitwise Operators) A lot of times you come across some strange operators where youre knocking your head to understand what is going on in the code. Almost all the programming languages have bitwise operators…

[置顶] Android的IPC访问控制设计与实现

3.3.1 IPC钩子函数设计与实现 IPC Binder是Android最重要的进程间通信机制,因此,必须在此实施强制访问控制。 1. 修改secuirty.h 打开终端shell,输入指令“cd /android4.0/kernel/goldfish/include/linux/vim security.h”,找到结…

TensorFlow在Anaconda环境下创建

一、我使用的是Anaconda自带的Jupyter编译器,详细的安装教程可以参考博文 二、之后打开Jupyter 三、进行测试 我的tensorflow使用的是2.0版本 import tensorflow.compat.v1 as tf tf.disable_v2_behavior()a tf.constant([1.0,2.0],name"a") b tf.co…

leetcode 654. 构造最大二叉树 思考分析

题目 给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素。 左子树是通过数组中最大值左边部分构造出的最大二叉树。 右子树是通过数组中最大值右边部分构造出的最大二叉树。 通过给定的数组构建最大二叉树&am…

Memcache的命令以及状态监控

输入telnet 127.0.0.1 11211(memcached默认端口为11211) stats :使用stats命令查看当前memcache服务器的状态 pidmemcache服务器的进程IDuptime服务器已经运行的秒数time服务器当前的unix时间戳versionmemcache版本pointer_size当前操作系统 …

flush python_带有示例的Python File flush()方法

flush python文件flush()方法 (File flush() Method) flush() method is an inbuilt method in Python, it is used to clear/flush the internal buffer, it is best practice while working with fila handling in Python, the internal buffer can be cleared before writin…

c++ 请抛弃匈牙利命名法 - 变量命名代码风格的建议。

我只针对c码农们讲,其他语言不了解不过应该大同小异。曾几何时翻开21天学通c系列等脑残入门书,都以匈牙利命名法示人(DWORD dwXXX, int nXXX, string strXXX)。现在我可以负责任的告诉你,把类型名写在前面屁用都没有,对…