PowerPoint Presentation

Size: px
Start display at page:

Download "PowerPoint Presentation"

Transcription

1 C++ 程序设计 第 7 章类 计算机学院黄章进 zhuang@ustc.edu.cn

2 内容 类 7.1 定义抽象数据类型 7.2 访问控制与封装 7.3 类的其他特性 7.4 类的作用域 7.5 构造函数再探 7.6 类的静态成员 2

3 定义抽象数据类型 C++ 中使用类 (class) 定义自己的数据类型 类的基本思想是数据抽象 (data abstraction) 和封装 (encapsulation) 数据抽象是一种依赖于接口 (interface) 和实现 (implementation) 分离的编程及设计技术 类的接口包括类用户所能执行的操作 类的实现包括 : 类的数据成员 实现接口的函数体以及定义类所需的各种私有函数 封装实施类的接口和实现的分离, 隐藏了类的实现细节 采用了数据抽象和封装的类定义了一个抽象数据类型 (abstract data type) 3

4 定义抽象数据类型 第 1 章使用的 Sales_item 类是一个抽象数据类型, 不能直接访问其数据成员 接口 :isbn 成员函数 支持 + = += << 和 >> 运算符 第 2 章定义的 Sales_data 类不是一个抽象数据类型, 只有数据成员, 且能访问 struct Sales_data { std::string bookno; unsigned units_sold = 0; double revenue = 0.0; }; 4

5 设计 Sales_data 类 定义抽象数据类型 Sales_data 类的接口包含如下操作 : 成员函数 isbn, 用于返回对象的 ISBN 编号 成员函数 combine, 用于将一个 Sales_data 对象加到另一个对象上 非成员函数 add, 执行两个 Sales_data 对象的加法 非成员函数 read, 将数据从 istream 读入到 Sales_data 对象中 非成员函数 print, 将 Sales_data 对象的值输出到 ostream 5

6 定义 Sales_data 类 定义抽象数据类型 Sales_data 类的定义如下 : struct Sales_data { // operations on Sales_data objects std::string isbn() const { return bookno; } Sales_data& combine(const Sales_data&); double avg_price() const; // data members std::string bookno; unsigned units_sold = 0; double revenue = 0.0; }; // nonmember Sales_data interface functions Sales_data add(const Sales_data&, const Sales_data&); std::ostream &print(std::ostream&, const Sales_data&); std::istream &read(std::istream&, Sales_data&); 6

7 定义 Sales_data 类 定义抽象数据类型 类以关键字 struct 开始, 紧跟着类名和类体, 最后必须写一个分号 类体由花括号包围, 形成一个类作用域 类体定义类的成员, 有数据成员 (data member) 和函数成员 (function member) 等 类内部定义的名字必须唯一, 但可与类外部定义的名字重复 struct Sales_data { /*... */ } accum, trans, *salesptr; // equivalent, but better way to define these objects struct Sales_data { /*... */ }; Sales_data accum, trans, *salesptr; 7

8 使用 Sales_data 类 Sales_data total; // variable to hold the running sum if (read(cin, total)) { // read the first transaction Sales_data trans; // variable to hold data for the next transaction while(read(cin, trans)) { // read the remaining transactions if (total.isbn() == trans.isbn()) // check the isbns total.combine(trans); // update the running total else { print(cout, total) << endl; // print the results total = trans; // process the next book } } print(cout, total) << endl; // print the last transaction } else { // there was no input cerr << "No data?!" << endl; // notify the user } 8

9 定义数据成员 定义 Sales_data 类 类的数据成员定义类的对象的内容 每个对象有自己的一份数据成员拷贝 定义数据成员的方法和普通变量一样 : 基本类型后紧跟一个或多个声明符 struct Sales_data { // operations on Sales_data objects... // data members std::string bookno; unsigned units_sold = 0; double revenue = 0.0; }; 9

10 定义数据成员 定义 Sales_data 类 C++11 中, 可以为数据成员提供一个类内初始值 (in-class initializer) 类内初始值或者放在花括号里, 或者放在等号右边, 但不能放在圆括号里 struct Sales_data {... // data members std::string bookno; unsigned units_sold = 0; double revenue = 0.0; }; 创建对象时, 类内初始值将被用于初始化数 据成员, 没有初始值的成员将被默认初始化 Sales_data total; // variable to hold the running sum 10

11 定义成员函数 定义 Sales_data 类 定义和声明成员函数的方式与普通函数差不多 成员函数的声明必须在类的内部, 但成员函数体可以定义在类内也可以定义在类外 成员函数体也是一个块 struct Sales_data { // operations on Sales_data objects std::string isbn() const { return bookno; } Sales_data& combine(const Sales_data&); double avg_price() const;... }; isbn 函数如何获得 bookno 成员所依赖的对象? 11

12 this 指针 定义 Sales_data 类 考察对 isbn 成员函数的调用 : total.isbn(); // Sales_data total; 使用点运算符来访问 total 对象的 isbn 成员 调用成员函数时, 实际上是替某个对象调用它 如果 isbn 访问 Sales_data 的某个成员, 那么它隐式地访问调用该函数的对象的成员 std::string isbn() const { return bookno; } isbn 返回 bookno 时, 实际上它隐式地返回 total.bookno 12

13 this 指针 定义 Sales_data 类 成员函数通过一个额外的隐式形参 this 来访问调用它的那个对象 定义名为 this 的形参或变量是不合法的 编译器把 this 指针初始化成调用成员函数的对象的地址 调用 total.isbn() 等价于如下伪代码 : // pseudo-code illustration of how a call to a member function is translated Sales_data::isbn(&total) 成员函数体内任何对类成员的直接访问都可以看做 this 指针的隐式引用 std::string isbn() const { return this->bookno; } 13

14 const 成员函数 定义 Sales_data 类 std::string isbn() const { return bookno; } C++ 允许把 const 关键字放在成员函数的参数列表之后, 声明一个常量成员函数 (const member function) 默认情况下,this 的类型是指向类类型非常量对象的常量指针 :Sales_data *const 常量成员函数的 this 是指向常量的常量指针 :const Sales_data *const 常量成员函数不能改变调用它的对象的内容 14

15 const 成员函数 定义 Sales_data 类 std::string isbn() const { return bookno; } 可以把 isbn 的函数体想象成如下形式 : // pseudo-code illustration of how the implicit this pointer is used // this code is illegal: we may not explicitly define the this pointer ourselves // note that this is a pointer to const because isbn is a const member std::string Sales_data::isbn(const Sales_data *const this) { return this->bookno; } 常量对象, 以及常量对象的引用或指针都只能调用常量成员函数 15

16 类作用域和成员函数 定义 Sales_data 类 类成员函数的定义嵌套在类作用域内 isbn 函数用到的 bookno 就是定义在类 Sales_data 内的数据成员 成员函数体可以随意使用类中的其他成员而无须在意这些成员出现的位置 即使 bookno 定义在 isbn 之后,isbn 还是能够使用 bookno 编译器分两步处理类 : 首先编译成员的声明 然后处理成员函数体 ( 如果有的话 ) 16

17 在类外定义成员函数 定义 Sales_data 类 在类外部定义成员函数时, 成员函数的定义必须与它在类内的声明匹配 类外定义的成员的名字必须包含它所属的类名 double Sales_data::avg_price() const { } if (units_sold) else return revenue/units_sold; return 0; 17

18 定义返回 this 对象的函数 定义 Sales_data 类 函数 combine 的设计初衷类似于复合赋值运算符 += 调用该函数的对象代表的是赋值运算符的左侧运算对象, 即 *this 右侧运算对象通过显式的实参传入函数 内置赋值运算符把其左侧运算对象作为左值返回,combine 函数必须返回引用类型 Sales_data& Sales_data::combine(const Sales_data &rhs) { units_sold += rhs.units_sold; // add the members of rhs into revenue += rhs.revenue; // the members of ''this'' object return *this; // return the object on which the function was called } 18

19 定义类相关的非成员函数 定义抽象数据类型 // nonmember Sales_data interface functions Sales_data add(const Sales_data&, const Sales_data&); std::ostream &print(std::ostream&, const Sales_data&); std::istream &read(std::istream&, Sales_data&); 经常需要定义一些类的辅助函数, 这些函数从概念上来说属于类的接口的组成部分, 但它们并不属于类本身 这些函数一般声明 ( 而非定义 ) 在类所在的头文件中 19

20 定义 read 和 print 函数 定义类相关的非成员函数 // input transactions contain ISBN, number of copies sold, and sales price istream &read(istream &is, Sales_data &item) { double price = 0; is >> item.bookno >> item.units_sold >> price; item.revenue = price * item.units_sold; return is; } ostream &print(ostream &os, const Sales_data &item) { os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os; } 20

21 定义 read 和 print 函数 定义类相关的非成员函数 read 和 print 分别接受一个各自 IO 类型的引用作为其参数 IO 类属于不能被拷贝的类型, 只能通过引用来传递 读取和写入会改变流的内容, 所以两个函数接受的是普通引用, 而非常量引用 print 函数不负责换行 一般执行输出任务的函数应该尽量减少对格式的控制 21

22 定义 add 函数 定义类相关的非成员函数 add 函数接受两个 Sales_data 对象作为其参数, 返回值是一个表示它们和的新的 Sales_data 对象 默认情况下, 拷贝类的对象其实是拷贝对象的数据成员 Sales_data add(const Sales_data &lhs, const Sales_data &rhs) { Sales_data sum = lhs; // copy data members from lhs into sum sum.combine(rhs); sum return sum; } // add data members from rhs into 22

23 构造函数 定义抽象数据类型 类通过名为构造函数 (constructor) 的成员函数来控制其对象的初始化过程 只要类的对象被创建, 就会执行构造函数 构造函数的任务是初始化类的数据成员 构造函数的名字和类名相同 构造函数没有返回类型 类可以包含多个构造函数 必须在形参数量或类型上有所区别 构造函数不能声明成 const 构造函数在 const 对象的初始化过程向其写值 23

24 合成的默认构造函数 构造函数 Sales_data total; // variable to hold the running sum 如果类没有显式地定义构造函数, 那么编译器会隐式地定义一个默认构造函数 (default constructor) 默认构造函数是一个没有参数的构造函数 编译器创建的默认构造函数称为合成的默认构造函数 (synthesized default constructor), 其初始化规则为 : 如果存在类内初始值, 用它来初始化成员 否则, 默认初始化该成员 24

25 不能依赖于合成的默 认构造函数 构造函数 一般地, 类必须定义自己的默认构造函数, 不能依赖于合成默认构造函数 只有当类没有声明任何构造函数时, 编译器才自动生成默认构造函数 合成的默认构造函数可能执行错误的操作 默认初始化的内置类型或复合类型的数据成员的值是未定义的 有时编译器无法合成默认的构造函数 例如, 类中含有一个类类型的成员且该类没有默认构造函数时 25

26 定义 Sales_data 的构 造函数 构造函数 定义 4 个构造函数使用如下参数 : 1 个 istream&, 从中读取一条交易信息 1 个 const string&, 表示 ISBN 编号 ;1 个 unsigned, 表示售出的图书数量 ; 以及 1 个 double, 表示图书的售价 1 个 const string&, 表示 ISBN 编号 ; 编译器将赋予其他成员默认值 一个空参数列表 ( 即默认构造函数 ) 如果定义了其他构造函数, 那么必须定义一个默认构造函数 26

27 定义 Sales_data 的构 造函数 构造函数 struct Sales_data { // constructors added Sales_data() = default; Sales_data(const std::string &s): bookno(s) { } Sales_data(const std::string &s, unsigned n, double p) : bookno(s), units_sold(n), revenue(p*n) { } Sales_data(std::istream &); // other members as before std::string isbn() const { return bookno; } Sales_data& combine(const Sales_data&); double avg_price() const; std::string bookno; unsigned units_sold = 0; double revenue = 0.0; }; 27

28 =default 的含义 构造函数 默认构造函数定义如下 : Sales_data() = default; 在 C++11 中, 如果需要默认行为, 通过在参数列表后写上 =default 来要求编译器生成合成默认构造函数 =default 既可以和声明一起出现在类内部, 也可以作为定义出现在类外 如果 =default 出现在类内部, 则默认构造函数是内联函数 ; 如果在类外, 则该成员默认情况下不是内联的 28

29 构造函数初始值列表 构造函数 类中定义的其他两个构造函数 : Sales_data(const std::string &s): bookno(s) { } Sales_data(const std::string &s, unsigned n, double p): bookno(s), units_sold(n), revenue(p*n) { } 冒号以及冒号与左花括号之间的代码称为构造函数初始值列表 (constructor initializer list), 为新创建对象的一个或多个数据成员赋初值 构造函数初始值是逗号分隔的成员名列表, 每个名字后紧跟括号或花括号括起来的成员初始值 29

30 构造函数初始值列表 构造函数 对未出现在初始值列表中的成员, 将以与合成默认构造函数相同的方式隐式初始化 只接受 1 个 string 参数的构造函数等价于 : // has the same behavior as the original constructor defined above Sales_data(const std::string &s): bookno(s), units_sold(0), revenue(0){ } 如果编译器不支持类内初始值, 那么所有的构造函数都应该显式地初始化每个内置类型的成员 30

31 在类外定义构造函数 构造函数 在类外定义构造函数时, 必须指明该构造函数是哪个类的成员 Sales_data::Sales_data(std::istream &is) { read(is, *this); // read will read a transaction from is into this object } 即使构造函数初始值列表是空的, 对象的成员仍然会在函数体执行前被初始化 31

32 拷贝 赋值和析构 定义抽象数据类型 除了定义类的对象如何初始化外, 还需要控制拷贝 (copy) 赋值 (assign) 和销毁 (destroy) 对象时的行为 初始化变量以及以传值方式传递或返回一个对象时, 对象被拷贝 使用赋值运算符时, 发生对象的赋值 当对象不再存在时, 执行销毁的操作 32

33 拷贝 赋值和析构 定义抽象数据类型 如果不定义这些操作, 编译将替我们合成它们 编译器生成的版本对对象的每个成员执行拷贝 赋值和销毁操作 编译器执行如下赋值语句时, total = trans; // process the next book 其行为与下面的代码相同 : // default assignment for Sales_data is equivalent to: total.bookno = trans.bookno; total.units_sold = trans.units_sold; total.revenue = trans.revenue; 33

34 访问控制与封装 在 C++ 中, 使用访问说明符 (access specifiers) 实施类的封装 定义在 public 说明符之后的成员在整个程序内可被访问 public 成员定义类的接口 定义在 private 说明符之后的成员可被类的成员函数访问, 但不能被使用该类的代码访问 private 部分封装 ( 隐藏 ) 了类的实现 34

35 封装 Sales_data 类 class Sales_data { public: // access specifier added Sales_data() = default; Sales_data(const std::string &s, unsigned n, double p): bookno(s), units_sold(n), revenue(p*n) { } Sales_data(const std::string &s): bookno(s) { } Sales_data(std::istream&); std::string isbn() const { return bookno; } Sales_data &combine(const Sales_data&); private: // access specifier added double avg_price() const { return units_sold? revenue/units_sold : 0; } std::string bookno; unsigned units_sold = 0; double revenue = 0.0; }; 35

36 访问控制与封装 作为接口的构造函数和部分成员函数跟在 public 说明符之后 数据成员和作为实现的部分成员函数跟在 private 说明符之后 一个类可以包含 0 或多个访问说明符, 且某个说明符出现的次数没有限制 每个访问说明符指定了随后成员的访问级别, 其有效范围直到出现下一个访问说明符或到达类的结尾处为止 36

37 使用 class 或 struct 访问控制与封装 可以使用 class 或 struct 来定义一个类, 区别在于它们的默认访问权限不同 使用 struct 关键字, 则定义在第一个访问说明符之前的成员是 public 的 一般地, 若定义的类的所有成员是 public 时, 使用 struct 使用 class 关键字, 则定义在第一个访问说明符之前的成员是 private 的 若定义的类有 private 成员时, 使用 class 37

38 友元 访问控制与封装 Sales_data 的数据成员是 private 的, 于是 read print 和 add 函数无法正常编译 类可以允许其他类或函数访问它的非公有成员, 方式是令其他类或函数成为它的友元 (friend) 如果类想把一个函数作为它的友元, 只需要在类内增加一条以 friend 关键字开始的该函数声明语句 38

39 友元 访问控制与封装 class Sales_data { // friend declarations for nonmember Sales_data operations added friend Sales_data add(const Sales_data&, const Sales_data&); friend std::istream &read(std::istream&, Sales_data&); friend std::ostream &print(std::ostream&, const Sales_data&); // other members and access specifiers as before... }; // declarations for nonmember parts of the Sales_data interface Sales_data add(const Sales_data&, const Sales_data&); std::istream &read(std::istream&, Sales_data&); std::ostream &print(std::ostream&, const Sales_data&); 39

40 友元的声明 友元 友元声明 (friend declaration) 只能出现类定义的内部 一般地, 最好在类定义开始或结束的位置集中声明友元 友元不是类的成员, 也不受它所在区域访问控制权限的约束 友元声明不是通常意义上的函数声明 为了使友元对类的用户可见, 通常把友元的声明 ( 在类外 ) 与类放在同一个头文件 40

41 类的其他特性 类成员再探 类型成员 内联成员函数 重载成员函数 可变数据成员 类类型成员的类内初始值 返回 *this 的成员函数 类类型 友元类 41

42 定义类型成员 类成员再探 假设 Screen 类表示显示器中的一个窗口 1 个保存屏幕窗口内容的 string 成员 3 个 string::size_type 成员, 分别表示光标位置以及窗口的高和宽 class Screen { public: typedef std::string::size_type pos; private: pos cursor = 0; pos height = 0, width = 0; std::string contents; }; 42

43 定义类型成员 类成员再探 除了定义数据和函数成员, 类还可以定义类型成员 (type member)-- 某种类型在类内的别名 类定义的类型名字和其他成员一样存在访问限制, 可以是 public 或 private 之一 声明类型别名时, 可以使用 typedef 或 using 形式 using pos = std::string::size_type; 不同于普通成员, 类型成员必须先定义后使用 类型成员通常出现在类定义的开始位置 43

44 Screen 类的成员函数 类成员再探 class Screen { public: typedef std::string::size_type pos; Screen() = default; // needed because Screen has another constructor // cursor initialized to 0 by its in-class initializer Screen(pos ht, pos wd, char c): height(ht), width(wd), contents(ht * wd, c) { } char get() const // get the character at the cursor { return contents[cursor]; } // implicitly inline inline char get(pos ht, pos wd) const; // explicitly inline Screen &move(pos r, pos c); // can be made inline later private: pos cursor = 0; pos height = 0, width = 0; std::string contents; }; 44

45 内联成员函数 类成员再探 定义在类内的成员函数是自动 inline 的 定义在类外的成员函数也可以 inline 可以在类内显式地声明成员函数为 inline 的, 也可在类外定义函数时说明是 inline 的 在声明和定义时同时说明 inline 是合法的 一般只在类外定义函数时说明 inline, 可使类更容易理解 45

46 内联成员函数 类成员再探 inline // we can specify inline on the definition Screen &Screen::move(pos r, pos c) { pos row = r * width; // compute the row location cursor = row + c ; // move cursor to the column within that row return *this; // return this object as an lvalue } char Screen::get(pos r, pos c) const // declared as inline in the class { pos row = r * width; // compute row location return contents[row + c]; // return character at the given column } 46

47 重载成员函数 成员函数也可以被重载, 只要函数之间在形参的数量和 / 或类型上有所区别 类成员再探 编译器根据实参来决定运行哪个版本的成员函数 Screen myscreen; // calls default constructor char ch = myscreen.get();// calls Screen::get() ch = myscreen.get(0,0); // calls Screen::get(pos, pos) 47

48 可变数据成员 类成员再探 可变数据成员 (mutable data member) 永远不会是 const, 即使它是 const 对象的成员 在数据成员的声明中加入 mutable 关键字 任何成员函数, 包括 const 成员函数都可以改变一个可变数据成员的值 48

49 可变数据成员 类成员再探 class Screen { public: void some_member() const; private: }; mutable size_t access_ctr; // may change even in a const object // other members as before void Screen::some_member() const { ++access_ctr; // keep a count of the calls to any member function } // whatever other work this member needs to do 49

50 类类型数据成员的初始值 类成员再探 定义窗口管理类 Window_mgr 表示显示器上的一组 Screen 对象 包含一个 vector<screen> 成员 默认情况下, 希望 Window_mgr 类对象创建时有一个默认初始化的 Screen 对象 最好的方式是把这个默认值声明为类内初始值 class Window_mgr { private: // Screens this Window_mgr is tracking // by default, a Window_mgr has one standard sized blank Screen std::vector<screen> screens{screen(24, 80, ' ') }; }; 50

51 返回 *this 的成员函数 类成员再探 添加函数设置当前光标或指定位置处的字符 class Screen { public: Screen &set(char); Screen &set(pos, pos, char); // other members as before }; inline Screen &Screen::set(char c) { contents[cursor] = c; // set the new value at the current cursor location return *this; // return this object as an lvalue } inline Screen &Screen::set(pos r, pos col, char ch) { contents[r*width + col] = ch; // set specified location to given value return *this; // return this object as an lvalue } 51

52 返回 *this 的成员函数 类成员再探 和 move 操作一样,set 成员函数返回调用它们的对象的引用 返回引用的函数是左值 (lvalue) 的 // move the cursor to a given position, and set that character myscreen.move(4,0).set('#'); 上面代码中的操作作用在同一个对象 myscreen 上, 等价于 myscreen.move(4,0); myscreen.set('#'); 52

53 从 const 成员函数返回 *this 返回 *this 的成员函数 如果 const 成员函数以引用的形式返回 *this, 那么返回类型是常量引用 增加 const 成员函数 display, 负责打印 Screen 的内容 const Screen &display(std::ostream &os) const; 不能将 display 嵌入到一组动作的序列中 Screen myscreen; // if display returns a const reference, the call to set is an error myscreen.display(cout).set('*'); 53

54 基于 const 的重载 返回 *this 的成员函数 通过区分成员函数是否是 const 的, 可以对其进行重载 原因同根据指针形参是否指向 const 而重载 常量对象只能调用 const 成员函数 非常量对象可以调用常量版本或非常量版本, 但非常量版本是更好的匹配 定义私有成员 do_display 负责打印的实际工作, 重载的 display 操作调用该函数然后返回执行操作的对象 54

55 基于 const 的重载 返回 *this 的成员函数 class Screen { public: // display overloaded on whether the object is const or not Screen &display(std::ostream &os) { do_display(os); return *this; } const Screen &display(std::ostream &os) const { do_display(os); return *this; } private: // function to do the work of displaying a Screen void do_display(std::ostream &os) const {os << contents;} // other members as before }; 55

56 基于 const 的重载 返回 *this 的成员函数 在某个对象上调用 display 时, 对象是否是 const 决定了调用哪个版本的 display Screen myscreen(5,3); const Screen blank(5, 3); myscreen.set('#').display(cout); // calls non const version blank.display(cout); // calls const version 56

57 类类型 每个类定义了唯一的类型 类成员再探 即使两个类定义的成员完全一样, 也是不同类型 struct First { int memi; int getmem(); }; struct Second { int memi; int getmem(); }; First obj1; Second obj2 = obj1; // error: obj1 and obj2 have different types 57

58 类类型 类成员再探 可以把类名直接作为类类型的名字, 也可以把类名跟在 struct 或 class 关键字后面 (C 风格方式 ) Sales_data item1; type Sales_data // default-initialized object of class Sales_data item1; // equivalent declaration 58

59 类的声明 类类型 可以仅声明类而暂时不定义它 : class Screen; // declaration of the Screen class 这种声明被称作前向声明 (forward declaration) 在声明之后定义之前, 类型 Screen 是一个不完全类型 (incomplete type) 知道 Screen 是一个类类型, 但是不清楚它到底有哪些成员 可以定义指向不完全类型的指针或引用, 也可以声明 ( 但不能定义 ) 以不完全类型作为形参或者返回类型的函数 59

60 类的声明 类类型 创建类的对象之前必须先定义该类 仅通过声明, 编译器无法确定对象需要多少存储空间 类也必须先被定义, 才能用引用或指针访问其成员 类先被定义后, 才能声明该类类型的数据成员 一个类的数据成员的类型不能是该类自己 类可以包含指向自身的指针或引用类型的数据成员 一旦类名出现后, 就认为该类已被声明 class Link_screen { Screen window; Link_screen *next; Link_screen *prev; }; 60

61 友元再探 类成员再探 类除了把普通的非成员函数定义成友元外 还可以把其他的类定义成友元 也可以把 ( 已定义过的 ) 其他类的成员函数定义成友元 友元函数能定义在类的内部 这样的函数是隐式 inline 的 61

62 类之间的友元关系 友元再探 如果一个类指定了友元类 (friend class), 则友元类的成员函数可以访问该类包括非公有成员在内的所有成员 class Screen { // Window_mgr members can access the private parts of class Screen friend class Window_mgr; //... rest of the Screen class }; 为 Window_mgr 类添加 clear 成员, 负责把指定 Screen 的内容置为空白 62

63 类之间的友元关系 友元再探 class Window_mgr { public: // location ID for each screen on the window using ScreenIndex = std::vector<screen>::size_type; // reset the Screen at the given position to all blanks void clear(screenindex); private: std::vector<screen> screens{screen(24, 80, ' ')}; }; void Window_mgr::clear(ScreenIndex i) { // s is a reference to the Screen we want to clear Screen &s = screens[i]; // reset the contents of that Screen to all blanks s.contents = string(s.height * s.width, ' '); } 63

64 成员函数作为友元 除了令整个类作为友元外, 可以把其他类的成员函数作为友元 友元再探 当把一个成员函数声明成友元时, 必须明确指出该成员函数属于哪个类 class Screen { // Window_mgr::clear must have been declared before class Screen }; friend void Window_mgr::clear(ScreenIndex); //... rest of the Screen class 64

65 成员函数作为友元 友元再探 令某个成员函数作为友元, 必须仔细组织程序的结构以满足声明和定义的彼此依赖关系 首先定义 Window_mgr 类, 其中声明但不定义 clear 函数 接下来定义 Screen 类, 包含对于 clear 的友元声明 最后定义 clear 函数, 此时它才可以访问 Screen 的成员 65

66 函数重载和友元 友元再探 如果一个类想把一组重载函数声明为它的友元, 它必须对这组函数中的每一个分别声明 // overloaded storeon functions extern std::ostream& storeon(std::ostream &, Screen &); extern BitMap& storeon(bitmap &, Screen &); class Screen { // ostream version of storeon may access the private parts of Screen objects friend std::ostream& storeon(std::ostream &, Screen &); //... }; 66

67 友元声明和作用域 友元再探 类和非成员函数的声明不是必须在它们的友元声明之前 当一个名字第一次出现在友元声明中时, 隐式地假定该名字在当前作用域中可见 友元本身不一定真的声明在当前作用域中 即使在类内定义友元函数, 也必须在类外提供相应的声明使得该函数可见 就算只被声明友元函数的类的成员来调用 友元声明只影响访问权限, 不是普通意义上的声明 67

68 友元声明和作用域 友元再探 struct X { friend void f() { /* friend function can be defined in the class body */ } X() { f(); } // error: no declaration for f void g(); void h(); }; void X::g() { return f(); } // error: f hasn't been declared void f(); // declares the function defined inside X void X::h() { return f(); } // ok: declaration for f is now in scope 68

69 类的作用域 一个类定义了它自己的作用域 在类作用域之外, 普通的数据和函数成员只能由对象 引用或指针使用成员访问运算符来访问 ; 类型成员使用作用域运算符访问 Screen::pos ht = 24, wd = 80; // use the pos type defined by Screen Screen scr(ht, wd, ' '); Screen *p = &scr; char c = scr.get(); // fetches the get member from the object scr c = p->get(); which p points // fetches the get member from the object to 69

70 作用域和定义在类外 的成员 在类外部, 成员的名字被隐藏起来了 类的作用域 一旦遇到类名, 定义的剩余部分 ( 包括形参列表和函数体 ) 就在类的作用域内了 void Window_mgr::clear(ScreenIndex i) { } Screen &s = screens[i]; s.contents = string(s.height * s.width, ' '); 70

71 作用域和定义在类外 的成员 当成员函数定义在类外时, 返回类型中使用的名字位于类作用域之外 返回类型必须指明它是哪个类的类型成员 class Window_mgr { public: using ScreenIndex = std::vector<screen>::size_type; // add a Screen to the window and returns its index ScreenIndex addscreen(const Screen&); // other members as before }; // return type is seen before we're in the scope of Window_mgr Window_mgr::ScreenIndex Window_mgr::addScreen(const Screen &s) { screens.push_back(s); return screens.size() - 1; 类的作用域 } 71

72 练习 类的作用域 练习 7.33: 为 Screen 类添加如下定义的 size 成员函数是否有问题? class Screen { public: typedef std::string::size_type pos; inline pos size() const; private: pos height = 0, width = 0; // other members as before }; pos Screen::size() const { return height * width; } 72

73 类作用域与名字查找 类的作用域 一般的名字查找 (name lookup) 过程 : 首先, 在名字所在的块内寻找其声明语句 只考虑在名字使用之前出现的声明 如果没找到, 继续查找外层作用域 如果最终未找到匹配的声明, 程序报错 类的成员函数中名字的解析方式不大一样, 因为类的定义分两步处理 : 首先, 编译成员的声明 直到类全部可见后才编译函数体 成员函数体中可使用类中定义的任何名字 73

74 类成员声明的名字查找 类作用域与名字查找 声明 ( 包括返回类型和形参列表中的名字 ) 中使用的名字, 都必须在使用前确保可见 两步处理方式只适用于成员函数体中使用的名字 typedef double Money; string bal; class Account { public: Money balance() { return bal; } private: }; Money bal; //... 74

75 类型名要特殊处理 类作用域与名字查找 一般地, 内层作用域可以重定义外层作用域中的名字, 即使该名字已在内层作用域中使用过 在类中, 如果成员使用了外层作用域中的类型名字, 那么类不能在之后重新定义该名字 如果未使用, 可以重新定义 一般把类型名的定义放在类开始处 75

76 类型名要特殊处理 类作用域与名字查找 typedef double Money; class Account { public: Money balance() { return bal; } // uses Money from the outer scope private: typedef double Money; // error: cannot redefine Money }; Money bal; //... 76

77 成员定义内普通块作 用域名字查找 类作用域与名字查找 成员函数体中使用的名字按如下方式解析 : 首先, 在成员函数内查找该名字的声明 只考虑函数体内该名字使用前出现的声明 如果在成员函数内没有找到声明, 则在类内继续查找 考虑类的所有成员 如果类内也没找到该名字的声明, 则在成员函数定义之前的外层作用域内继续查找 77

78 成员定义内普通块作 用域名字查找 类作用域与名字查找 // note: this code is for illustration purposes only and reflects bad practice // it is generally a bad idea to use the same name for a parameter and a member int height; // defines a name subsequently used inside Screen class Screen { public: typedef std::string::size_type pos; void dummy_fcn(pos height) { cursor = width * height; // which height? the parameter } private: pos cursor = 0; pos height = 0, width = 0; }; 78

79 成员定义内普通块作 用域名字查找 类作用域与名字查找 上例中,height 形参隐藏了同名的成员 如果想使用 height 成员, 可采用如下两种方法 // bad practice: names local to member functions shouldn't hide member names void Screen::dummy_fcn(pos height) { cursor = width * this->height; // member height // alternative way to indicate the member cursor = width * Screen::height; // member height } 更好的方式是给形参起个其他名字 // good practice: don't use a member name for a parameter or other local variable void Screen::dummy_fcn(pos ht) { cursor = width * height; // member height } 79

80 在外层作用域中查找 类作用域与名字查找 如果编译器在函数和类作用域中都没有找到名字, 将接着在外层作用域中查找 如果想使用外层作用域中的名字, 可以显式地通过作用域运算符来请求 // bad practice: don't hide names that are needed from surrounding scopes void Screen::dummy_fcn(pos height) { cursor = width * ::height;// which height? the global one } 80

81 在外层作用域中查找 类作用域与名字查找 当成员函数定义在类外时, 名字查找的第三步不仅要考虑类定义所在作用域中的声明, 还要考虑函数定义所在作用域中的声明 int height; // defines a name subsequently used inside Screen class Screen { public: typedef std::string::size_type pos; void setheight(pos); pos height = 0; // hides the declaration of height in the outer scope }; Screen::pos verify(screen::pos); void Screen::setHeight(pos var) { // var: refers to the parameter, height: refers to the class member // verify: refers to the global function } height = verify(var); 81

82 练习 类作用域与名字查找 练习 7.35: 说明下面代码中 Type 和 initval 分别使用哪个定义? 如果代码存在错误, 尝试修改它 typedef string Type; Type initval(); class Exercise { public: typedef double Type; Type setval(type); Type initval(); private: Type val; }; Type Exercise::setVal(Type parm) { val = parm + initval(); return val; } 82

83 构造函数再探 如果成员在构造函数初始值列表中没有显式地初始化, 且无类内初始值, 则该成员将在构造函数体执行之前被默认初始化 // legal but sloppier way to write the Sales_data constructor: no constructor initializers Sales_data::Sales_data(const string &s, unsigned cnt, double price) { bookno = s; units_sold = cnt; revenue = cnt * price; } 83

84 构造函数初始值列表 构造函数再探 如果成员是 const 引用或者属于某种未提供默认构造函数的类类型, 必须通过构造函数初始值列表来提供初值 class ConstRef { public: ConstRef(int ii); private: int i; const int ci; int &ri; }; 84

85 初始值有时必不可少 构造函数初始值列表 常量或引用成员必须被初始化 // error: ci and ri must be initialized ConstRef::ConstRef(int ii) { // assignments: i = ii; // ok ci = ii; // error: cannot assign to a const ri = i; // error: ri was never initialized } 只能通过构造函数初始值列表来初始化 // ok: explicitly initialize reference and const members ConstRef::ConstRef(int ii): i(ii), ci(ii), ri(i) { } 85

86 成员初始化的顺序 构造函数初始值列表 在构造函数初始值列表中, 每个成员只能出现一次 成员的初始化顺序与它们在类定义中出现的顺序一致 最好令构造函数初始值的顺序与成员声明的顺序一致, 且尽量避免使用某些成员初始化其他成员 class X { int i; int j; public: // undefined: i is initialized before j X(int val): j(val), i(j) { } }; 86

87 默认实参和构造函数 构造函数初始值列表 构造函数中可以使用默认实参 若构造函数为所有形参提供了默认实参, 则它实际上也定义了默认构造函数 class Sales_data { public: // defines the default constructor as well as one that takes a string argument Sales_data(std::string s = ""): bookno(s) { } // remaining constructors unchanged Sales_data(std::string s, unsigned cnt, double rev): bookno(s), units_sold(cnt), revenue(rev*cnt) { } Sales_data(std::istream &is) { read(is, *this); } // remaining members as before }; 87

88 练习 构造函数初始值列表 练习 7.36: 找出并修改代码中的问题 struct X { X (int i, int j): base(i), rem(base % j) { } int rem, base; }; 88

89 委托构造函数 构造函数再探 委托构造函数 (delegating constructor) 使用它所属类的其他构造函数执行其初始化过程 委托构造函数也有成员初始值列表和函数体 在委托构造函数内, 成员初始值列表只有一个唯一的入口, 就是类名本身 : 类名后面紧跟圆括号括起来的实参列表 实参列表必须与类中另外一个构造函数匹配 89

90 委托构造函数 构造函数再探 class Sales_data { public: // nondelegating constructor initializes members from corresponding arguments Sales_data(std::string s, unsigned cnt, double price): bookno(s), units_sold(cnt), revenue(cnt*price) { } // remaining constructors all delegate to another constructor Sales_data(): Sales_data("", 0, 0) {} Sales_data(std::string s): Sales_data(s, 0,0) {} Sales_data(std::istream &is): Sales_data() { read(is, *this); } // other members as before }; 90

91 默认构造函数的作用 构造函数再探 当对象被默认初始化或值初始化时自动执行默认构造函数 在以下情况下发生默认初始化 : 在块作用域中不使用任何初始值定义一个非静态变量或者数组时 当类含有使用合成默认构造函数的类类型成员时 当类类型的成员在构造函数初始值列表中没有被显式地初始化, 且无类内初始值时 91

92 默认构造函数的作用 构造函数再探 值初始化在以下情况下发生 : 在数组初始化过程中, 如果提供的初始值数量少于数组的大小时 当定义全局对象或局部静态对象而不提供初始值时 当通过形如 T() 的表达式显式地请求值初始化时, 其中 T 是类型名 vector<t> vec(10); 在上述情况下, 类必须包含一个默认构造函数 92

93 默认构造函数的作用 构造函数再探 class NoDefault { public: NoDefault(const std::string&); // additional members follow, but no other constructors }; struct A { // my_mem is public by default NoDefault my_mem; }; A a; // error: cannot synthesize a constructor for A struct B { B() {} // error: no initializer for b_member NoDefault b_member; }; 93

94 使用默认构造函数 默认构造函数的作用 下面代码试图声明一个默认初始化的对象 : Sales_data obj(); // ok: but defines a function, not an object if (obj.isbn() == Primer_5th_ed.isbn()) // error: obj is a function 如果想定义一个使用默认构造函数进行初始化的对象, 正确方法是去掉对象名后的空括号对 // ok: obj is a default-initialized object Sales_data obj; 94

95 隐式的类类型转换 构造函数再探 能用一个实参调用的构造函数也称为转换构造函数 (converting constructor) class Sales_data { public: // access specifier added Sales_data() = default; Sales_data(const std::string &s, unsigned n, double p): bookno(s), units_sold(n), revenue(p*n) { } Sales_data(const std::string &s): bookno(s) { } Sales_data(std::istream&); Sales_data &combine(const Sales_data&); // other members as before }; 95

96 隐式的类类型转换 构造函数再探 转换构造函数定义了一条从构造函数的形参类型到类类型的隐式转换规则 // Sales_data(const std::string &s); // Sales_data &combine(const Sales_data&); string null_book = " "; // constructs a temporary Sales_data object // with units_sold and revenue equal to 0 and bookno equal to null_book item.combine(null_book); 96

97 只允许一步类类型转换 隐式的类类型转换 编译器只会自动地应用一步类型转换 无法隐式地使用两次转换 // error: requires two user-defined conversions: // (1) convert " " to string // (2) convert that (temporary) string to Sales_data item.combine(" "); 可以显式地把字符串转换成 string 或者 Sales_data 对象 // ok: explicit conversion to string, implicit conversion to Sales_data item.combine(string(" ")); // ok: implicit conversion to string, explicit conversion to Sales_data item.combine(sales_data(" ")); 97

98 抑制构造函数定义的 隐式转换 隐式的类类型转换 可以通过把构造函数声明为 explicit 来阻止隐式转换 class Sales_data { public: }; Sales_data() = default; Sales_data(const std::string &s, unsigned n, double p): bookno(s), units_sold(n), revenue(p*n) { } explicit Sales_data(const std::string &s): bookno(s) { } explicit Sales_data(std::istream&); // remaining members as before 98

99 抑制构造函数定义的 隐式转换 隐式的类类型转换 声明为 explicit 之后, 没有构造函数能用于隐式地创建 Sales_data 对象 string null_book = " "; item.combine(null_book); // error: string constructor is explicit item.combine(cin); // error: istream constructor is explicit 关键字 explicit 只对能用一个实参调用的构造函数有意义 只有在类内声明构造函数时能使用 explicit, 在类外定义时不能重复 // error: explicit allowed only on a constructor declaration in a class header explicit Sales_data::Sales_data(istream& is) { read(is, *this); } 99

100 explicit 构造函数只能 用于直接初始化 隐式的类类型转换 拷贝初始化 (= 号形式 ) 会发生隐式转换 explicit 构造函数只能用于直接初始化 string null_book = " "; Sales_data item1 (null_book); // ok: direct initialization // error: cannot use the copy form of initialization with an explicit constructor Sales_data item2 = null_book; 100

101 显式地使用构造函数转换 隐式的类类型转换 可以显式地使用 explicit 构造函数进行强制类型转换 string null_book = " "; // ok: the argument is an explicitly constructed Sales_data object item.combine(sales_data(null_book)); // ok: static_cast can use an explicit constructor item.combine(static_cast<sales_data>(cin)); 101

102 有显式构造函数的标 准库类 隐式的类类型转换 string 类的单参数构造函数 string(const char *) 不是 explicit 的 接受容量参数的 vector 构造函数 vector<t>(size_type n) 是 explicit 的 vector<int> ivec(10); // ten elements, each initialized to 0 vector<int> vi = 10; // error: must use direct initialization to supply a size 102

103 练习 隐式的类类型转换 练习 7.48: 假定 Sales_data 的构造函数 Sales_data(const std::string &s) 不是 explicit 的, 则下述定义将执行什么样的操作? string null_isbn(" "); Sales_data item1(null_isbn); Sales_data item2(" "); 如果是 explicit 的, 又发生什么呢? 103

104 练习 隐式的类类型转换 练习 7.49: 对于 combine 函数的三种不同声明, 调用 i.combine(s) 时分别发生什么情况? 其中,i 是一个 Sales_data 对象, 而 s 是一个 string 对象 (a) Sales_data &combine(sales_data); (b) Sales_data &combine(sales_data&); (c) Sales_data &combine(const Sales_data&) const; 104

105 聚合类 构造函数再探 聚合类 (aggregate class) 满足如下条件 : 所有数据成员都是 public 的 没有定义任何构造函数 没有类内初始值 没有基类和 virtual 函数 struct Data { int ival; string s; }; 105

106 聚合类的初始化 聚合类 可以用花括号括起来的初始值列表来初始化聚合类的数据成员 // val1.ival = 0; val1.s = string("anna") Data val1 = { 0, "Anna" }; 初始值的顺序必须与数据成员的声明顺序一致 // error: can't use "Anna" to initialize ival, or 1024 to initialize s Data val2 = { "Anna", 1024 }; 如果初始值列表中的元素个数少于数据成员数, 则靠后的成员被值初始化 初始值列表的元素个数不能多于成员数 106

107 练习 聚合类 练习 7.52: 第 2 章中 Sales_data 类定义为 struct Sales_data { }; std::string bookno; unsigned units_sold = 0; double revenue = 0.0; 解释下面的初始化过程 若存在问题, 尝试修改它 Sales_data item = {" ", 25, 15.99}; 107

108 类的静态成员 类 有时候, 类需要一些成员来表征自身的类属性, 而不是其个体对象的属性 实现同一个类的不同对象之间的数据和函数共享 例如, 一个银行账户类可能需要一个表示当前基准利率的数据成员 109

109 声明静态成员 类的静态成员 在成员声明前加关键字 static 来表示类属性 静态成员可以是 public 或 private 的 静态数据成员可以是 const 常量 引用 数组 类类型等 class Account { public: void calculate() { amount += amount * interestrate; } static double rate() { return interestrate; } static void rate(double); private: std::string owner; double amount; static double interestrate; static double initrate(); }; 110

110 声明静态成员 类的静态成员 类的静态成员存在于任何对象之外 对象中不包含与静态数据成员相关的数据 静态成员函数不绑定到任何对象, 即不包含 this 指针 静态成员函数不能声明为 const 不能在静态成员函数体内使用 this 指针 限制适用于 this 的显式使用, 以及调用非静态成员的隐式使用 必须通过对象来访问非静态成员 111

111 使用静态成员 类的静态成员 在类外使用类名和作用域运算符访问静态成员 double r; r = Account::rate(); // access a static member using the scope operator 也可以使用类的对象 引用或指针来访问静态成员 Account ac1; Account *ac2 = &ac1; // equivalent ways to call the static member rate function r = ac1.rate(); // through an Account object or reference r = ac2->rate(); // through a pointer to an Account object 112

112 使用静态成员 类的静态成员 成员函数不用通过作用域运算符就能直接使用静态成员 class Account { public: void calculate() { amount += amount * interestrate; } private: static double interestrate; // remaining members as before }; 113

113 定义静态成员 类的静态成员 可以在类内也可以在类外定义静态成员函数 在类外定义静态成员时, 不能重复 static static 关键字只出现在类内的声明语句中 void Account::rate(double newrate) { } interestrate = newrate; 114

114 定义静态成员 类的静态成员 静态数据成员不属于类的任何一个对象, 必须在类外定义和初始化每个静态数据成员 静态数据成员定义在任何函数之外, 具有静态生存期 // define and initialize a static class member double Account::interestRate = initrate(); 和其他成员的定义一样, 定义静态成员也可以访问类的私有成员 115

115 静态数据成员的类内 初始化 类的静态成员 一般情况下, 类的静态成员不能在类内初始化 但可以为 const 整数类型静态成员提供类内初始值, 且必须为字面值类型的 constexpr 静态成员提供类内初始值 初始值必须是常量表达式 如果静态成员的应用场景仅限于编译器可以替换其值的情况, 则初始化的 const 或 constexpr 静态成员可以不在类外定义 116

116 静态数据成员的类内 初始化 类的静态成员 class Account { public: static double rate() { return interestrate; } static void rate(double); private: static constexpr int period = 30; // period is a constant expression double daily_tbl[period]; }; 如果在类内提供了初始值, 则成员定义时不能再指定初始值 // definition of a static member with no initializer constexpr int Account::period; // initializer provided in the class definition 117

117 只静态成员可用的场景 类的静态成员 静态数据成员可以是不完全类型 特别地, 静态数据成员的类型可以是它所属的类类型 class Bar { public: //... private: static Bar mem1; // ok: static member can have incomplete type Bar *mem2; incomplete type Bar mem3; complete type }; // ok: pointer member can have // error: data members must have 118

118 只静态成员可用的场景 类的静态成员 静态和普通数据成员的另外一个区别是静态数据成员可以作为默认实参 class Screen { public: // bkground refers to the static member // declared later in the class definition Screen& clear(char = bkground); private: }; static const char bkground; 119

119 练习 类的静态成员 练习 7.58: 下面静态数据成员的声明和定义是否有错? // example.h class Example { public: static double rate = 6.5; static const int vecsize = 20; static vector<double> vec(vecsize); }; // example.c #include "example.h" double Example::rate; vector<double> Example::vec; 120

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double

More information

Microsoft PowerPoint - 8. 运算符重载 Operator Overloading.pptx

Microsoft PowerPoint - 8. 运算符重载 Operator Overloading.pptx 运算符重载 Operator Overloading class Point { public: ; double x_, y_; Why Operator Overloading? Point (double x =0, double y = 0):x_(x),y_(y) { int main(){ Point a(1., 2), b(3,4); Point c = a + b; return 0;

More information

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double x) { d

More information

Microsoft PowerPoint - ch6 [相容模式]

Microsoft PowerPoint - ch6 [相容模式] UiBinder wzyang@asia.edu.tw UiBinder Java GWT UiBinder XML UI i18n (widget) 1 2 UiBinder HelloWidget.ui.xml: UI HelloWidgetBinder HelloWidget.java XML UI Owner class ( Composite ) UI XML UiBinder: Owner

More information

C/C++ - 函数

C/C++ - 函数 C/C++ Table of contents 1. 2. 3. & 4. 5. 1 2 3 # include # define SIZE 50 int main ( void ) { float list [ SIZE ]; readlist (list, SIZE ); sort (list, SIZE ); average (list, SIZE ); bargragh

More information

Microsoft PowerPoint - string_kruse [兼容模式]

Microsoft PowerPoint - string_kruse [兼容模式] Strings Strings in C not encapsulated Every C-string has type char *. Hence, a C-string references an address in memory, the first of a contiguous set of bytes that store the characters making up the string.

More information

WWW PHP

WWW PHP WWW PHP 2003 1 2 function function_name (parameter 1, parameter 2, parameter n ) statement list function_name sin, Sin, SIN parameter 1, parameter 2, parameter n 0 1 1 PHP HTML 3 function strcat ($left,

More information

Microsoft PowerPoint - 3. 函数Functionl.ppt [兼容模式]

Microsoft PowerPoint - 3. 函数Functionl.ppt [兼容模式] 函数 Function 如何重用代码 How to reuse code 3 4 = 3*3*3*3 3 4,6 5 : 拷贝 - 粘帖代码 (Copy-paste code) 3 4,6 5,12 10 : 拷贝 - 粘帖代码 (Copy-paste code) Bad! 使用函数 (with a function) 使用函数 (with a function) 使用函数 (with a function)

More information

幻灯片 1

幻灯片 1 第三课类和对象 ( 封装 ) 互联网新技术在线教育领航者 1 内容概述 1. 封装的概念 2. 访问控制 3. 栈类的封装 4. 构造与析构 5.myString 构造函数 6. 构造与析构的次序 7. 类文件写法 8. 对象的内存 9.this 指针初探 10. 构造函数初始值列表 11. 拷贝构造和赋值运算符重载 12. 浅拷贝 13. 深拷贝 14. 成员函数内联 15. 友元 16.const

More information

程序设计语言及基础

程序设计语言及基础 Chapter 10 Classes: A Deeper Look, Part 2 http://jssec.seu.edu.cn 杨明 yangming2002@seu.edu.cn OBJECTIVES To specify const (constant) objects and const member functions. To create objects composed of other

More information

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc References (Section 5.2) Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 15-16, 2010 H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 0 / 22 Fun Time (1) What happens in memory? 1 i n t i ; 2

More information

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 月 3 日 1 1 INPUTOUTPUT 1 InputOutput 题目描述 用 cin 输入你的姓名 ( 没有空格 ) 和年龄 ( 整数 ), 并用 cout 输出 输入输出符合以下范例 输入 master 999 输出 I am master, 999 years old. 注意 "," 后面有一个空格,"." 结束,

More information

OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票

OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票 复习 创建对象 构造函数 函数重载 : 函数 = 函数名 + 参数列表 public class MyType { int i; double d; char c; void set(double x)

More information

OOP with Java 通知 Project 2 提交时间 : 3 月 14 日晚 9 点 另一名助教 : 王桢 学习使用文本编辑器 学习使用 cmd: Power shell 阅读参考资料

OOP with Java 通知 Project 2 提交时间 : 3 月 14 日晚 9 点 另一名助教 : 王桢   学习使用文本编辑器 学习使用 cmd: Power shell 阅读参考资料 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 2 提交时间 : 3 月 14 日晚 9 点 另一名助教 : 王桢 Email: 51141201063@ecnu.cn 学习使用文本编辑器 学习使用 cmd: Power shell 阅读参考资料 OOP with Java Java 类型 引用 不可变类型 对象存储位置 作用域 OOP

More information

运算符重载 为什么要 运算符重载 那些运算符可以重载, 哪些不可以 如何实现运算符重载 实现方式 : 成员函数与非成员函数 类型转换 怎样实现对象与基本数据类型数据的运算 2

运算符重载 为什么要 运算符重载 那些运算符可以重载, 哪些不可以 如何实现运算符重载 实现方式 : 成员函数与非成员函数 类型转换 怎样实现对象与基本数据类型数据的运算 2 第十一讲 运算符重载 与类型转换 运算符重载 为什么要 运算符重载 那些运算符可以重载, 哪些不可以 如何实现运算符重载 实现方式 : 成员函数与非成员函数 类型转换 怎样实现对象与基本数据类型数据的运算 2 为什么要运算符重载 预定义的运算符只针对基本数据类型, 若要对类的对象进行类似的运算, 需要重新定义运算符的功能 运算符重载实质就是函数重载 : 对已有的运算符赋予多重含义, 使得同一个运算符作用于不同类型的数据时导致不同的行为

More information

PowerPoint Presentation

PowerPoint Presentation 程序设计 II 第 10 讲运算符重载与类型转换 计算机学院黄章进 zhuang@ustc.edu.cn 内容 10.1 基本概念 10.2 输入和输出运算符 10.3 算术和关系运算符 10.4 赋值运算符 10.5 下标运算符 10.6 自增和自减运算符 10.7 成员访问运算符 10.8 函数调用运算符 10.9 类型转换运算符 2 基本概念 重载运算符函数的声明形式 : 返回类型 operator

More information

chap07.key

chap07.key #include void two(); void three(); int main() printf("i'm in main.\n"); two(); return 0; void two() printf("i'm in two.\n"); three(); void three() printf("i'm in three.\n"); void, int 标识符逗号分隔,

More information

Microsoft PowerPoint - 6. 用户定义类型User-defined Datatypes.ppt [兼容模式]

Microsoft PowerPoint - 6. 用户定义类型User-defined Datatypes.ppt [兼容模式] 用户定义类型 User-defined Datatypes classes and structs 几何向量 (Geometry Vector) 二维平面上的向量由起点和终点构成 每个点包含两个坐标 (x, y), 因此一个向量需要四个实数表示 Start= (0.9,1.5) Start= (0.4,0.8) int main() { double xstart = 0.4; double xend

More information

PowerPoint Presentation

PowerPoint Presentation C++ 程序设计 第 12 章管理 计算机学院黄章进 zhuang@ustc.edu.cn 内容 管理 12.1 (12.1.2) 12.2 动态数组 (12.2.1) 2 对象生存期 之前程序中所使用的对象有严格定义的生存期 全局对象在程序启动时分配, 在程序结束时销毁 局部自动对象在进入定义所在块中被创建, 在离开块时销毁 局部静态对象在第一次使用前分配, 在程序结束时销毁 C++ 还支持动态分配对象

More information

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课 复习 Java 包 创建包 : package 语句, 包结构与目录结构一致 使用包 : import restaurant/ - people/ - Cook.class - Waiter.class - tools/ - Fork.class

More information

C/C++ - 字符输入输出和字符确认

C/C++ - 字符输入输出和字符确认 C/C++ Table of contents 1. 2. getchar() putchar() 3. (Buffer) 4. 5. 6. 7. 8. 1 2 3 1 // pseudo code 2 read a character 3 while there is more input 4 increment character count 5 if a line has been read,

More information

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double x) { d =

More information

c_cpp

c_cpp C C++ C C++ C++ (object oriented) C C++.cpp C C++ C C++ : for (int i=0;i

More information

C/C++ - 文件IO

C/C++ - 文件IO C/C++ IO Table of contents 1. 2. 3. 4. 1 C ASCII ASCII ASCII 2 10000 00100111 00010000 31H, 30H, 30H, 30H, 30H 1, 0, 0, 0, 0 ASCII 3 4 5 UNIX ANSI C 5 FILE FILE 6 stdio.h typedef struct { int level ;

More information

エスポラージュ株式会社 住所 : 東京都江東区大島 東急ドエルアルス大島 HP: ******************* * 关于 Java 测试试题 ******

エスポラージュ株式会社 住所 : 東京都江東区大島 東急ドエルアルス大島 HP:  ******************* * 关于 Java 测试试题 ****** ******************* * 关于 Java 测试试题 ******************* 問 1 运行下面的程序, 选出一个正确的运行结果 public class Sample { public static void main(string[] args) { int[] test = { 1, 2, 3, 4, 5 ; for(int i = 1 ; i System.out.print(test[i]);

More information

FY.DOC

FY.DOC 高 职 高 专 21 世 纪 规 划 教 材 C++ 程 序 设 计 邓 振 杰 主 编 贾 振 华 孟 庆 敏 副 主 编 人 民 邮 电 出 版 社 内 容 提 要 本 书 系 统 地 介 绍 C++ 语 言 的 基 本 概 念 基 本 语 法 和 编 程 方 法, 深 入 浅 出 地 讲 述 C++ 语 言 面 向 对 象 的 重 要 特 征 : 类 和 对 象 抽 象 封 装 继 承 等 主

More information

天 主 教 輔 仁 大 學 社 會 學 系 學 士 論 文 小 別 勝 新 婚? 久 別 要 離 婚? 影 響 遠 距 家 庭 婚 姻 感 情 因 素 之 探 討 Separate marital relations are getting better or getting worse? -Exp

天 主 教 輔 仁 大 學 社 會 學 系 學 士 論 文 小 別 勝 新 婚? 久 別 要 離 婚? 影 響 遠 距 家 庭 婚 姻 感 情 因 素 之 探 討 Separate marital relations are getting better or getting worse? -Exp 天 主 教 輔 仁 大 學 社 會 學 系 學 士 論 文 小 別 勝 新 婚? 久 別 要 離 婚? 影 響 遠 距 家 庭 婚 姻 感 情 因 素 之 探 討 Separate marital relations are getting better or getting worse? -Explore the impact of emotional factors couples do not

More information

Microsoft PowerPoint - Lecture7II.ppt

Microsoft PowerPoint - Lecture7II.ppt Lecture 8II SUDOKU PUZZLE SUDOKU New Play Check 軟體實作與計算實驗 1 4x4 Sudoku row column 3 2 } 4 } block 1 4 軟體實作與計算實驗 2 Sudoku Puzzle Numbers in the puzzle belong {1,2,3,4} Constraints Each column must contain

More information

Windows RTEMS 1 Danilliu MMI TCP/IP QEMU i386 QEMU ARM POWERPC i386 IPC PC104 uc/os-ii uc/os MMI TCP/IP i386 PORT Linux ecos Linux ecos ecos eco

Windows RTEMS 1 Danilliu MMI TCP/IP QEMU i386 QEMU ARM POWERPC i386 IPC PC104 uc/os-ii uc/os MMI TCP/IP i386 PORT Linux ecos Linux ecos ecos eco Windows RTEMS 1 Danilliu MMI TCP/IP 80486 QEMU i386 QEMU ARM POWERPC i386 IPC PC104 uc/os-ii uc/os MMI TCP/IP i386 PORT Linux ecos Linux ecos ecos ecos Email www.rtems.com RTEMS ecos RTEMS RTEMS Windows

More information

untitled

untitled Ogre Rendering System http://antsam.blogone.net AntsamCGD@hotmail.com geometry systemmaterial systemshader systemrendering system API API DirectX OpenGL API Pipeline Abstraction API Pipeline Pipeline configurationpipeline

More information

Strings

Strings Inheritance Cheng-Chin Chiang Relationships among Classes A 類 別 使 用 B 類 別 學 生 使 用 手 機 傳 遞 訊 息 公 司 使 用 金 庫 儲 存 重 要 文 件 人 類 使 用 交 通 工 具 旅 行 A 類 別 中 有 B 類 別 汽 車 有 輪 子 三 角 形 有 三 個 頂 點 電 腦 內 有 中 央 處 理 單 元 A

More information

无类继承.key

无类继承.key 无类继承 JavaScript 面向对象的根基 周爱 民 / aimingoo aiming@gmail.com https://aimingoo.github.io https://github.com/aimingoo rand = new Person("Rand McKinnon",... https://docs.oracle.com/cd/e19957-01/816-6408-10/object.htm#1193255

More information

Microsoft PowerPoint - 10 模板 Template.pptx

Microsoft PowerPoint - 10 模板 Template.pptx 模板 Tempalte 泛型编程的需要 Why Templates? 设想你对整数类型实现了一个排序算法 : void sort(int *is,int n); 用该函数可以对实 复数或工资单排序吗? 模板可以复用源代码 - 泛型编程. inline void Swap( int &x, int &y){ int t = x; x = y; y =t; inline void Swap(double

More information

Microsoft Word - 物件導向編程精要.doc

Microsoft Word - 物件導向編程精要.doc Essential Object-Oriented Programming Josh Ko 2007.03.11 object-oriented programming C++ Java OO class object OOP Ruby duck typing complexity abstraction paradigm objects objects model object-oriented

More information

Microsoft Word - ch04三校.doc

Microsoft Word - ch04三校.doc 4-1 4-1-1 (Object) (State) (Behavior) ( ) ( ) ( method) ( properties) ( functions) 4-2 4-1-2 (Message) ( ) ( ) ( ) A B A ( ) ( ) ( YourCar) ( changegear) ( lowergear) 4-1-3 (Class) (Blueprint) 4-3 changegear

More information

新版 明解C++入門編

新版 明解C++入門編 511!... 43, 85!=... 42 "... 118 " "... 337 " "... 8, 290 #... 71 #... 413 #define... 128, 236, 413 #endif... 412 #ifndef... 412 #if... 412 #include... 6, 337 #undef... 413 %... 23, 27 %=... 97 &... 243,

More information

Computer Architecture

Computer Architecture ECE 3120 Computer Systems Assembly Programming Manjeera Jeedigunta http://blogs.cae.tntech.edu/msjeedigun21 Email: msjeedigun21@tntech.edu Tel: 931-372-6181, Prescott Hall 120 Prev: Basic computer concepts

More information

Guava学习之Resources

Guava学习之Resources Resources 提供提供操作 classpath 路径下所有资源的方法 除非另有说明, 否则类中所有方法的参数都不能为 null 虽然有些方法的参数是 URL 类型的, 但是这些方法实现通常不是以 HTTP 完成的 ; 同时这些资源也非 classpath 路径下的 下面两个函数都是根据资源的名称得到其绝对路径, 从函数里面可以看出,Resources 类中的 getresource 函数都是基于

More information

Microsoft Word - template.doc

Microsoft Word - template.doc HGC efax Service User Guide I. Getting Started Page 1 II. Fax Forward Page 2 4 III. Web Viewing Page 5 7 IV. General Management Page 8 12 V. Help Desk Page 13 VI. Logout Page 13 Page 0 I. Getting Started

More information

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO Car DVD New GUI IR Flow User Manual V0.1 Jan 25, 2008 19, Innovation First Road Science Park Hsin-Chu Taiwan 300 R.O.C. Tel: 886-3-578-6005 Fax: 886-3-578-4418 Web: www.sunplus.com Important Notice SUNPLUS

More information

C++ 程式設計

C++ 程式設計 C C 料, 數, - 列 串 理 列 main 數串列 什 pointer) 數, 數, 數 數 省 不 不, 數 (1) 數, 不 數 * 料 * 數 int *int_ptr; char *ch_ptr; float *float_ptr; double *double_ptr; 數 (2) int i=3; int *ptr; ptr=&i; 1000 1012 ptr 數, 數 1004

More information

untitled

untitled 1 Outline 數 料 數 數 列 亂數 練 數 數 數 來 數 數 來 數 料 利 料 來 數 A-Z a-z _ () 不 數 0-9 數 不 數 SCHOOL School school 數 讀 school_name schoolname 易 不 C# my name 7_eleven B&Q new C# (1) public protected private params override

More information

C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 1 CIRCLE 1 Circle 描述 编写一个圆类 Circle, 实现半径的输入 面积的计算和输出 输入 圆的半径 (double 类型 ) 输出 圆的面积 ( 保留小数点后两位 ) 样例输入 3 样例输出 28.27 提示 圆周率的取值需要比较精确, 以保证计算结果的精度 #include

More information

02

02 Thinking in C++: Volume One: Introduction to Standard C++, Second Edition & Volume Two: Practical Programming C++ C C++ C++ 3 3 C C class C++ C++ C++ C++ string vector 2.1 interpreter compiler 2.1.1 BASIC

More information

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Microsoft PowerPoint - L17_Inheritance_v4.pptx C++ Programming Lecture 17 Wei Liu ( 刘 威 ) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology May. 2015 Lecture 17 Chapter 20. Object-Oriented Programming: Inheritance

More information

概述

概述 OPC Version 1.6 build 0910 KOSRDK Knight OPC Server Rapid Development Toolkits Knight Workgroup, eehoo Technology 2002-9 OPC 1...4 2 API...5 2.1...5 2.2...5 2.2.1 KOS_Init...5 2.2.2 KOS_InitB...5 2.2.3

More information

C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1

C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1 C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1 1 CRECT 类 1 CRect 类 设计矩形类, 包含 长度和宽度信息 基本构造函数 基础属性的访问接口 ( 读 / 写, Read/Write, Get/Set) 计算周长和面积 ( 注 : 基本构造函数, 一个无参数的默认构造函数, 以及一个初始化数据成员的构造函数如果数据成员的初始化有多种形式, 就提供多个构造函数

More information

入學考試網上報名指南

入學考試網上報名指南 入 學 考 試 網 上 報 名 指 南 On-line Application Guide for Admission Examination 16/01/2015 University of Macau Table of Contents Table of Contents... 1 A. 新 申 請 網 上 登 記 帳 戶 /Register for New Account... 2 B. 填

More information

穨control.PDF

穨control.PDF TCP congestion control yhmiu Outline Congestion control algorithms Purpose of RFC2581 Purpose of RFC2582 TCP SS-DR 1998 TCP Extensions RFC1072 1988 SACK RFC2018 1996 FACK 1996 Rate-Halving 1997 OldTahoe

More information

ENGG1410-F Tutorial 6

ENGG1410-F Tutorial 6 Jianwen Zhao Department of Computer Science and Engineering The Chinese University of Hong Kong 1/16 Problem 1. Matrix Diagonalization Diagonalize the following matrix: A = [ ] 1 2 4 3 2/16 Solution The

More information

OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点

OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点 复习 Protected 可以被子类 / 同一包中的类访问, 不能被其他类访问 弱化的 private 同时赋予 package access class MyType { public int i; public double d; public

More information

Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies to all d

Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies to all d WeChat Search Visual Identity Guidelines WEDESIGN 2018. 04 Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies

More information

Microsoft Word - 第3章.doc

Microsoft Word - 第3章.doc Java C++ Pascal C# C# if if if for while do while foreach while do while C# 3.1.1 ; 3-1 ischeck Test() While ischeck while static bool ischeck = true; public static void Test() while (ischeck) ; ischeck

More information

coverage2.ppt

coverage2.ppt Satellite Tool Kit STK/Coverage STK 82 0715 010-68745117 1 Coverage Definition Figure of Merit 2 STK Basic Grid Assets Interval Description 3 Grid Global Latitude Bounds Longitude Lines Custom Regions

More information

* RRB *

* RRB * *9000000000RRB0010040* *9000000000RRB0020040* *9000000000RRB0030040* *9000000000RRB0040040* *9000000000RRC0010050* *9000000000RRC0020050* *9000000000RRC0030050* *9000000000RRC0040050* *9000000000RRC0050050*

More information

C/C++程序设计 - 字符串与格式化输入/输出

C/C++程序设计 - 字符串与格式化输入/输出 C/C++ / Table of contents 1. 2. 3. 4. 1 i # include # include // density of human body : 1. 04 e3 kg / m ^3 # define DENSITY 1. 04 e3 int main ( void ) { float weight, volume ; int

More information

程序设计与算法语言 静态与友元 C/C++ Programming and Algorithms Static, Friend and Const Dongke Sun ( 孙东科 ) 东南大学机械工程学院 School of Mechanical Engineer

程序设计与算法语言 静态与友元 C/C++ Programming and Algorithms Static, Friend and Const Dongke Sun ( 孙东科 ) 东南大学机械工程学院 School of Mechanical Engineer 程序设计与算法语言 静态与友元 C/C++ Programming and Algorithms Static, Friend and Const Dongke Sun ( 孙东科 ) dksun@seu.edu.cn 东南大学机械工程学院 School of Mechanical Engineering Southeast University Spring semester, 2019 牢记 80/20

More information

Microsoft PowerPoint - STU_EC_Ch02.ppt

Microsoft PowerPoint - STU_EC_Ch02.ppt 樹德科技大學資訊工程系 Chapter 2: Number Systems Operations and Codes Shi-Huang Chen Sept. 2010 1 Chapter Outline 2.1 Decimal Numbers 2.2 Binary Numbers 2.3 Decimal-to-Binary Conversion 2.4 Binary Arithmetic 2.5

More information

提纲 1 2 OS Examples for 3

提纲 1 2 OS Examples for 3 第 4 章 Threads2( 线程 2) 中国科学技术大学计算机学院 October 28, 2009 提纲 1 2 OS Examples for 3 Outline 1 2 OS Examples for 3 Windows XP Threads I An Windows XP application runs as a seperate process, and each process may

More information

untitled

untitled Co-integration and VECM Yi-Nung Yang CYCU, Taiwan May, 2012 不 列 1 Learning objectives Integrated variables Co-integration Vector Error correction model (VECM) Engle-Granger 2-step co-integration test Johansen

More information

C/C++ - 字符串与字符串函数

C/C++ - 字符串与字符串函数 C/C++ Table of contents 1. 2. 3. 4. 1 char C 2 char greeting [50] = " How " " are " " you?"; char greeting [50] = " How are you?"; 3 printf ("\" Ready, go!\" exclaimed John."); " Ready, go!" exclaimed

More information

Microsoft Word - chap10.doc

Microsoft Word - chap10.doc 78 10. Inheritance in C++ 我 們 已 介 紹 了 物 件 導 向 程 式 的 第 一 個 主 要 特 性, 即 程 式 可 模 組 化 成 為 類 別 ( 物 件 ), 類 別 具 有 資 料 封 裝 的 特 性 接 下 來 我 們 要 介 紹 物 件 導 向 程 式 的 另 一 個 主 要 特 性, 那 就 是 類 別 具 有 繼 承 的 功 能 繼 承 就 是 重 複

More information

ch_code_infoaccess

ch_code_infoaccess 地 產 代 理 監 管 局 公 開 資 料 守 則 2014 年 5 月 目 錄 引 言 第 1 部 段 數 適 用 範 圍 1.1-1.2 監 管 局 部 門 1.1 紀 律 研 訊 1.2 提 供 資 料 1.3-1.6 按 慣 例 公 布 或 供 查 閱 的 資 料 1.3-1.4 應 要 求 提 供 的 資 料 1.5 法 定 義 務 及 限 制 1.6 程 序 1.7-1.19 公 開 資

More information

Guava学习之CharSequenceReader

Guava学习之CharSequenceReader CharSequenceReader 类是以 CharSequence 的形式读取字符 CharSequenceReader 类继承自 Reader 类, 除了 remaining() hasremaining() 以及 checkopen() 函数之后, 其他的函数都是重写 Reader 类中的函数 CharSequenceReader 类声明没有用 public 关键字, 所以我们暂时还不能调用这个类

More information

Chn 116 Neh.d.01.nis

Chn 116 Neh.d.01.nis 31 尼 希 米 书 尼 希 米 的 祷 告 以 下 是 哈 迦 利 亚 的 儿 子 尼 希 米 所 1 说 的 话 亚 达 薛 西 王 朝 二 十 年 基 斯 流 月 *, 我 住 在 京 城 书 珊 城 里 2 我 的 兄 弟 哈 拿 尼 和 其 他 一 些 人 从 犹 大 来 到 书 珊 城 我 向 他 们 打 听 那 些 劫 后 幸 存 的 犹 太 人 家 族 和 耶 路 撒 冷 的 情 形

More information

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii 前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii C# 7 More Effective C# C# C# C# C# C# Common Language Runtime CLR just-in-time

More information

CC213

CC213 : (Ken-Yi Lee), E-mail: feis.tw@gmail.com 49 [P.51] C/C++ [P.52] [P.53] [P.55] (int) [P.57] (float/double) [P.58] printf scanf [P.59] [P.61] ( / ) [P.62] (char) [P.65] : +-*/% [P.67] : = [P.68] : ,

More information

<4D6963726F736F667420576F7264202D2032303130C4EAC0EDB9A4C0E04142BCB6D4C4B6C1C5D0B6CFC0FDCCE2BEABD1A15F325F2E646F63>

<4D6963726F736F667420576F7264202D2032303130C4EAC0EDB9A4C0E04142BCB6D4C4B6C1C5D0B6CFC0FDCCE2BEABD1A15F325F2E646F63> 2010 年 理 工 类 AB 级 阅 读 判 断 例 题 精 选 (2) Computer mouse How does the mouse work? We have to start at the bottom, so think upside down for now. It all starts with mouse ball. As the mouse ball in the bottom

More information

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc 聖 保 羅 男 女 中 學 學 年 中 一 入 學 申 請 申 請 須 知 申 請 程 序 : 請 將 下 列 文 件 交 回 本 校 ( 麥 當 勞 道 33 號 ( 請 以 A4 紙 張 雙 面 影 印, 並 用 魚 尾 夾 夾 起 : 填 妥 申 請 表 並 貼 上 近 照 小 學 五 年 級 上 下 學 期 成 績 表 影 印 本 課 外 活 動 表 現 及 服 務 的 證 明 文 件 及

More information

RUN_PC連載_10_.doc

RUN_PC連載_10_.doc PowerBuilder 8 (10) Jaguar CTS ASP Jaguar CTS PowerDynamo Jaguar CTS Microsoft ASP (Active Server Pages) ASP Jaguar CTS ASP Jaguar CTS ASP Jaguar CTS ASP Jaguar CTS ASP Jaguar CTS ASP Jaguar Server ASP

More information

Microsoft Word - 第3章.doc

Microsoft Word - 第3章.doc 第 3 章 lambda 表达式及其应用 lambda 表达式是 Java 8 提供的一种新特性, 它使得 Java 也能像 C# 和 C++ 语言一样进行简单的 函数式编程, 这不仅简化了某些通用结构的实现方式, 也大大增强了 Java 语言的表达功能 3.1 lambda 表达式简介 lambda 表达式是基于数学中的 λ 演算得名, 本质上就是一个没有方法名的匿名方法 例如, 有一个方法定义如下

More information

Microsoft PowerPoint - 07 派生数据类型

Microsoft PowerPoint - 07 派生数据类型 能源与动力工程学院 目录 派生类型 陈 斌 固有数据类型 数值型 (numerical) 整型 INTEGER 实型 REAL 复数型 COMPLEX 非数值型 字符型 CHARACTER 逻辑型 ( 布尔型 )LOGICAL 自定义数据类型 ( 派生类型, derived type) 派生类型是指用户利用 Fortran 系统内部类型, 如整型 实型 复数型 逻辑型 字符型等的组合自行创建出一个新的数据类型,

More information

Edge-Triggered Rising Edge-Triggered ( Falling Edge-Triggered ( Unit 11 Latches and Flip-Flops 3 Timing for D Flip-Flop (Falling-Edge Trigger) Unit 11

Edge-Triggered Rising Edge-Triggered ( Falling Edge-Triggered ( Unit 11 Latches and Flip-Flops 3 Timing for D Flip-Flop (Falling-Edge Trigger) Unit 11 Latches and Flip-Flops 11.1 Introduction 11.2 Set-Reset Latch 11.3 Gated D Latch 11.4 Edge-Triggered D Flip-Flop 11.5 S-R Flip-Flop 11.6 J-K Flip-Flop 11.7 T Flip-Flop 11.8 Flip-Flops with additional Inputs

More information

Strings

Strings Polymorphism and Virtual Functions Cheng-Chin Chiang Virtual Function Basics 多 型 (Polymorphism) 賦 予 一 個 函 數 多 種 意 涵, 存 在 於 同 一 類 別 之 內 祖 先 類 別 與 後 代 類 別 間 物 件 導 向 程 式 設 計 基 本 原 理 虛 擬 函 數 (Virtual Function)

More information

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 odps-sdk 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基 开放数据处理服务 ODPS SDK SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基础功能的主体接口, 搜索关键词 "odpssdk-core" 一些

More information

Microsoft Word - SH090330.doc

Microsoft Word - SH090330.doc 2009 年 3 月 30 日 環 球 指 數 上 周 收 市 價 一 星 期 變 化 百 分 率 四 星 期 變 化 百 分 率 恆 生 指 數 14,119.50 +1285.99 +10.02% +1307.93 +10.21% 國 企 指 數 8,481.22 +985.26 +13.14% +1578.38 +22.87% 上 海 綜 合 指 數 2,374.44 +93.35 +4.09%

More information

Knowledge and its Place in Nature by Hilary Kornblith

Knowledge and its Place in Nature by Hilary Kornblith Deduction by Daniel Bonevac Chapter 7 Quantified Natural Deduction Quantified Natural Deduction As with truth trees, natural deduction in Q depends on the addition of some new rules to handle the quantifiers.

More information

C 1

C 1 C homepage: xpzhangme 2018 5 30 C 1 C min(x, y) double C // min c # include # include double min ( double x, double y); int main ( int argc, char * argv []) { double x, y; if( argc!=

More information

Microsoft PowerPoint - 01_Introduction.ppt

Microsoft PowerPoint - 01_Introduction.ppt Hello, World C 程序设计语言 第 1 章章观其大略 孙志岗 sun@hit.edu.cn http://sunner.cn prf("hello,, world\n"); 超级无敌考考你 : 如何把 hello 和 world 分别打印在两行? 2004-12-19 A Tutorial Introduction 2 hello.c 打印华氏温度与摄氏温度对照表 计算公式 : C=(5/9)(

More information

提问袁小兵:

提问袁小兵: C++ 面 试 试 题 汇 总 柯 贤 富 管 理 软 件 需 求 分 析 篇 1. STL 类 模 板 标 准 库 中 容 器 和 算 法 这 部 分 一 般 称 为 标 准 模 板 库 2. 为 什 么 定 义 虚 的 析 构 函 数? 避 免 内 存 问 题, 当 你 可 能 通 过 基 类 指 针 删 除 派 生 类 对 象 时 必 须 保 证 基 类 析 构 函 数 为 虚 函 数 3.

More information

3.1 num = 3 ch = 'C' 2

3.1 num = 3 ch = 'C' 2 Java 1 3.1 num = 3 ch = 'C' 2 final 3.1 final : final final double PI=3.1415926; 3 3.2 4 int 3.2 (long int) (int) (short int) (byte) short sum; // sum 5 3.2 Java int long num=32967359818l; C:\java\app3_2.java:6:

More information

OOP with Java 通知 Project 2 提交时间 : 3 月 21 日晚 9 点 作业提交格式 学习使用 文本编辑器 cmd, PowerShell (Windows), terminal(linux, Mac)

OOP with Java 通知 Project 2 提交时间 : 3 月 21 日晚 9 点 作业提交格式 学习使用 文本编辑器 cmd, PowerShell (Windows), terminal(linux, Mac) OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 2 提交时间 : 3 月 21 日晚 9 点 作业提交格式 学习使用 文本编辑器 cmd, PowerShell (Windows), terminal(linux, Mac) 复习 面向对象编程 将实际问题分解成不同的对象 不的对象提供不同的服务 对象之间可以传递消息 例子小李深夜

More information

C/C++ - 数组与指针

C/C++ - 数组与指针 C/C++ Table of contents 1. 2. 3. 4. 5. 6. 7. 8. 1 float candy [ 365]; char code [12]; int states [50]; 2 int array [6] = {1, 2, 4, 6, 8, 10}; 3 // day_mon1.c: # include # define MONTHS 12 int

More information

extend

extend (object oriented) Encapsulation Inheritance Polymorphism Dynamic Binding (base class) (derived class) 1 class Base { int I; void X(); void Y(); class Derived: public Base { private: int j; void z(); Derived

More information

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 复习 Protected 可以被子类 / 同一包中的类访问, 不能被其他类访问 弱化的 private 同时赋予 package access class MyType { public int i; public double d; public char

More information

INTRODUCTION TO COM.DOC

INTRODUCTION TO COM.DOC How About COM & ActiveX Control With Visual C++ 6.0 Author: Curtis CHOU mahler@ms16.hinet.net This document can be freely release and distribute without modify. ACTIVEX CONTROLS... 3 ACTIVEX... 3 MFC ACTIVEX

More information

C/C++语言 - C/C++数据

C/C++语言 - C/C++数据 C/C++ C/C++ Table of contents 1. 2. 3. 4. char 5. 1 C = 5 (F 32). 9 F C 2 1 // fal2cel. c: Convert Fah temperature to Cel temperature 2 # include < stdio.h> 3 int main ( void ) 4 { 5 float fah, cel ;

More information

Scott Effective C++ C++ C++ Roger Orr OR/2 ISO C++ Effective Modern C++ C++ C++ Scoot 42 Bart Vandewoestyne C++ C++ Scott Effective Modern C++ Damien

Scott Effective C++ C++ C++ Roger Orr OR/2 ISO C++ Effective Modern C++ C++ C++ Scoot 42 Bart Vandewoestyne C++ C++ Scott Effective Modern C++ Damien Effective Modern C++ C++ C++ C++11/C++14 C++ Scott Meyers Gerhard Kreuzer Siemens AG Effective Modern C++ Effective Modern C++ Andrei Alexandrescu Facebook Modern C++ Design C++ C++ Nevin Liber DRW Trading

More information

Microsoft Word - Final Exam Review Packet.docx

Microsoft Word - Final Exam Review Packet.docx Do you know these words?... 3.1 3.5 Can you do the following?... Ask for and say the date. Use the adverbial of time correctly. Use Use to ask a tag question. Form a yes/no question with the verb / not

More information

國立中山大學學位論文典藏.PDF

國立中山大學學位論文典藏.PDF I II III The Study of Factors to the Failure or Success of Applying to Holding International Sport Games Abstract For years, holding international sport games has been Taiwan s goal and we are on the way

More information

1.ai

1.ai HDMI camera ARTRAY CO,. LTD Introduction Thank you for purchasing the ARTCAM HDMI camera series. This manual shows the direction how to use the viewer software. Please refer other instructions or contact

More information

Microsoft Word - 新1-12.doc

Microsoft Word - 新1-12.doc 实训 5 面向对象编程练习 实训 5 面向对象编程练习 5.1 实训目的 通过编程和上机实验理解 Java 语言是如何体现面向对象编程基本思想 以及如何创建类 和对象 了解成员变量和成员方法的特性 5.2 实训要求 编写一个体现面向对象思想的程序 编写一个创建对象和使用对象的方法的程序 5.3 实训内容 5.3.1 创建对象并使用对象 1 定义一个 Person 类 可以在应用程序中使用该类 成员属性

More information

untitled

untitled A, 3+A printf( ABCDEF ) 3+ printf( ABCDEF ) 2.1 C++ main main main) * ( ) ( ) [ ].* ->* ()[] [][] ** *& char (f)(int); ( ) (f) (f) f (int) f int char f char f(int) (f) char (*f)(int); (*f) (int) (

More information

Windows XP

Windows XP Windows XP What is Windows XP Windows is an Operating System An Operating System is the program that controls the hardware of your computer, and gives you an interface that allows you and other programs

More information

Chapter 1 What is Programing Paradigm 1

Chapter 1 What is Programing Paradigm 1 An Introduction to Programing Paradigm Chase Zhang May 8, 2013 Chapter 1 What is Programing Paradigm 1 CHAPTER 1. WHAT IS PROGRAMING PARADIGM 2 Definition from Wikipedia 1. Object-oriented programming/

More information

HCD0174_2008

HCD0174_2008 Reliability Laboratory Page: 1 of 5 Date: December 23, 2008 WINMATE COMMUNICATION INC. 9 F, NO. 111-6, SHING-DE RD., SAN-CHUNG CITY, TAIPEI, TAIWAN, R.O.C. The following merchandise was submitted and identified

More information

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 复习 Java 包 创建包 : package 语句, 包结构与目录结构一致 使用包 : import restaurant/ - people/ - Cook.class - Waiter.class - tools/ - Fork.class - Table.class

More information

幻灯片 1

幻灯片 1 第四课类和对象 ( 构造深入 ) 互联网新技术在线教育领航者 1 内容概述 1. 数据成员指针 2. 成员函数指针 3. 三 / 五法则 4. 引用计数 5. 写时拷贝 6.swap 函数 7. 移动构造函数 8. 移动赋值运算符重载 9. 对象移动 10.std::vector 动态增长 11.std::vector 与移动 12. 移动小结 互联网新技术在线教育领航者 2 数据成员指针 定义 :

More information

國家圖書館典藏電子全文

國家圖書館典藏電子全文 - - I - II - Abstract Except for few intellect games such as chess, mahjong, and poker games, role-play games in on-line games are the mainstream. As for the so-called RPG, briefly speaking, it is the

More information