A tour of C++ Day6

A tour of C++ Book Note Day6

Just for share my note when I reading the book. If you want to discuss what interests you, you can write in the comments section. If there is a dispute, you can corret it!

2. User-Defined Types

2.1 Introduction

User-Defined/Introduction quote52

  • 然而,C++没有提供一套高级的接口设施去给程序员方便地编写高级应用。相反,C++用了一系列复杂的抽象机制来增强内置类型和操作符,程序员可以从中构建此类高级设施

However, they don’t provide the programmer with high-level facilities to conveniently write advanced applications. Instead, C++ augments the built-in types and operations with a sophisticated set of abstraction mechanisms out of which programmers can build such high-level facilities. ^quote52

User-Defined/type quote53

  • 使用C++抽象机制从其他类构建的类型称为用户自定义类型
    • 自定义类型通常优于内置类型,因为它们更易于使用、不易出错,并且通常与直接使用内置类型一样高效,甚至更为高效

Types built out of other types using C++’s abstraction mechanisms are called user-defined types. ^quote53

2.2 Structures

User-Defined/Structure quote54

  • 构建一个新类型的第一步通常是将其所需的元素组织到数据结构中
    • 组织 -> 描述

The first step in building a new type is often to organize the elements it needs into a data structure ^quote54

struct Vector {
	double* elem;
	int sz;
};

void vector_list(Vector& v, int s) { // initialize a Vector
	v.elem = new double[s];
	v.sz = s;
}

User-Defined/Structure/new quote55

  • new运算符从被成为空闲存储(也被叫做动态内存区和堆)的区域分配内存。在空闲存储区上被分配的对象与其创建的范围无关,一直"存活"到使用delete操作符将其摧毁

The new operator allocates memory from an area called the free store (also known as dynamic memory and heap). Objects allocated on the free store are independent of the scope from which they are created and “live” until they are destroyed using the delete operator ^quote55

// a simple use Vector
double read_and_sum(int s) {
	Vector v;
	vector_init(v, s);

	for (int i = 0; i !=s; ++i)
		std::cin >> v.elem[i];

	double sum = 0;
	for (int i = 0; i != s; ++i)
		sum += v.elem[i];
	return sum;
}

User-Defined/Structure/to quote56

  • 我们使用"."(点)来获取变量(或者甚至引用)结构体成员
  • 使用"->"来获取指针结构体成员

We use . (dot) to access struct members through a name (and through a reference) and -> to access struct members through a pointer. ^quote56

void f(Vector& v, Vector& rv, Vector* pv) {  
    int i1 = v.sz;  
    int i2 = rv.sz;  
    int i3 = pv->sz;  
}

2.3 Classes

User-Defined/Classes quote57

  • 将数据与数据上的操作分开指定具有很多优势,例如能够任意方式使用数据。但是,用户定义类型需要在表示和操作之间建立更紧密的连接,以具有“真实类型”所期望的所有属性

Having data specified separately from the operations on it has advantages, such as the ability to use the data in arbitrary ways.
However, a tighter connection between the representation and the operations is needed for a user-defined type to have all the properties expected of a “real type.” ^quote57

User-Defined/Classes/want quote58

  • 特别是,我们经常想要保证用户无法访问表示(自定义类型中的成员),以简化使用且保证数据的一致性(防止用户对内部数据直接进行更改),并允许用户之后通过接口改进表示(允许用户在定义之后,通过我们设计的接口去规范化的修改内部结构)。
  • 要做到这一点,我们必须区分一个类型的接口(供所有人使用)和它的实现(可以访问其他情况下无法访问的数据)
    • 事实上,这就是OOP中的封装性,而这种语言机制,就被称为类

In particular, we often want to keep the representation inaccessible to users so as to simplify use, guarantee consistent use of the data, and allow us to later improve the representation. To do that, we have to distinguish between the interface to a type (to be used by all) and its implementation (which has access to the otherwise inaccessible data). ^quote58

  • The interface of a class is defined by its public members, and its privatemembers are accessible only through that interface. The public and private parts of a class declaration can appear in any order
class Vector {  
public:  
    Vector(int s): elem{new double[s]}, sz{s} {}  
    double& operator[] (int i) { return elem[i]; }  
    int size() { return sz; }  
private:  
    double* elem;  
    int sz;  
};

User-Defined/Classes/size quote59

The number of elements (6 in the example) can vary from Vector object to Vector object, and a Vector object can have a different number of elements at different times. However, the Vector object itself is always the same size.

Vector v1(6);
Vector v2(12);

// we can see the sz in v1 is 6 but v2 is 12
sizeof(v1)
sizeof(v2)

// -> the the size of two are both the same value 16
  • 这是C++中处理不同数量信息的基本技术:一个固定大小的“句柄”,指的是“其他地方”(例如,在new分配的空闲存储区上)
    • 至于为什么sizeof(v1)的值为16,涉及到内存对齐的概念

This is the basic technique for handling varying amounts of information in C++: a fixed-size handle referring to a variable amount of data “elsewhere” (e.g., on the free store allocated by new; ^quote59

User-Defined/Classes/initialize quote60

  • 与普通函数不同,构造函数保证了类的对象的初始化。因此,定义构造函数消除了类未初始化变量的问题。

Unlike an ordinary function, a constructor is guaranteed to be used to initialize objects of its class. Thus, defining a constructor eliminates the problem of uninitialized variables for a class. ^quote60

User-Defined/Classes/struct quote61

  • struct和class之间没有本质上的区别。默认情况下,struct只是一个成员公开的类
    • 相对的,class就是成员私有的

There is no fundamental difference between a struct and a class; a struct is simply a class with members public by default. ^quote61

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
“a tour of c” 可以理解为C语言的一次巡回之旅。C语言是一种广泛应用于计算机硬件和软件开发的编程语言。它由贝尔实验室的丹尼斯·里奇于1972年开发,并在接下来的几年中逐渐流行起来。以下是关于C语言巡回之旅的一些重要内容: 首先,C语言具有简洁、高效的特点。它使用了一些基本的语法结构和关键字,使得程序代码更加简洁,易于理解和维护。同时,C语言的编译过程相对快速,生成的可执行文件也具有较高的执行效率。 其次,C语言具有广泛的应用领域。它被用于开发系统级软件,如操作系统和编译器,也被广泛应用于游戏开发、嵌入式系统、网络编程等领域。通过一次C语言的巡回之旅,可以深入了解C语言在不同领域中的应用和实践。 另外,C语言还具有丰富的库函数和标准函数供开发人员使用。这些库函数可以提供各种功能,如文件操作、字符串处理、内存管理等。通过学习和了解这些库函数,可以提高编程效率,并加快程序的开发速度。 最后,C语言也是学习其他高级编程语言的基础。许多其他编程语言,如C++、Java、Python等,都是在C语言基础上发展起来的。因此,通过一次C语言的巡回之旅,可以掌握一些基本的编程概念和思维方式,为学习其他编程语言打下坚实的基础。 总之,C语言巡回之旅是一个了解C语言的机会,通过深入学习和实践,可以了解C语言的特点、应用及其在计算机科学领域的重要性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值