C++ 面向对象高级编程 (上) week 1 (Boolan)

2017-10-23  本文已影响0人  YPAN

课程目标

推荐书目

  1. C++ Primer (5th Edition) by Stanley B. Lippman
  2. The C++ Programming Language (4th Edition) by Bjarne Stroustrup
  3. Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition) by Scott Meyers
  4. The C++ Standard Library - A Tutorial and Reference (2nd Edition) by Nicolai M. Josuttis
  5. STL源码剖析 by 侯捷

点评: 选择一本好的C++书籍(1和2两本书皆可), 然后以及尽量多的完成书籍的习题,可以帮助完成C++语法的学习; 熟悉C++的语法后,通过3学习如何正确高效地使用C++; 如果对STL的一些实现有兴趣,可参考书目4和5.

复数Complex类的设计

主程序complex-test.cpp:

#include <iostream>
#include "complex.h"
using namespace std;

int main() {
  complex c1(2, 1);
  complex c2;
  cout << c1 << endl;
  cout << c2 << endl;

  // ...
  return 0; 
}

头文件complex.h的布局:

#ifndef __COMPLEX__  // use guard against multiple inclusion
#define __COMPLEX__
// forward declaration (前置声明)
// class declaration (类-声明)
// class definition (类-定义)
#endif  // __COMPLEX__

关于guard (防御式申明) (摘自C++ Primer):

C++ programs use the preprocessor to define header guards. Header guards rely on preprocessor variables. Preprocessor variables have one of two possible states: defined or not defined. The #define directive takes a name and defines that name as a preprocessor variable. There are two other directives that test whether a given preprocessor variable has or has not been defined: #ifdef is true if the variable has been defined, and #ifndef is true if the variable has not been defined. If the test is true, then everything following the #ifdef or #ifndef is processed up to the matching #endif.

class的声明(declaration)和定义(definition)

// class declaration (类-声明)
class complex {
public:
  complex (double r = 0, double i = 0): re (r), im (i) { }
  complex& operator += (const complex&);
  double real () const { return re; }
  double imag () const { return im; }
private:
  double re, im;
  friend complex& __doapl (complex *, const complex&);
};

有些函数(e.g., constructor, real, imag)在此直接定义 (自动成为inline候选人), 另一些函数(e.g., operator+=, __doapl)在class body之外定义:

// class definition (类-定义)
inline complex&
__doapl (complex* ths, const complex& r)
{
  ths->re += r.re;
  ths->im += r.im;
  return *ths;
}

inline complex&
complex::operator += (const complex& r)
{
  return __doapl (this, r);
}

解释几个重要概念:

Classes often have small functions that can benefit from being inlined. Member functions defined inside the class are automatically inline. Thus, complex’s constructor and real()/imag() are inline by default. We can explicitly declare a member function as inline as part of its declaration inside the class body. Alternatively, we can specify inline on the function definition that appears outside the class body.

Although we are not required to do so, it is legal to specify inline on both the declaration and the definition. However, specifying inline only on the definition outside the class can make the class easier to read.

In C++ we use access specifiers to enforce encapsulation:

  • Members defined after a public specifier are accessible to all parts of the program. The public members define the interface to the class.
  • Members defined after a private specifier are accessible to the member functions of the class but are not accessible to code that uses the class. The private sections encapsulate (i.e., hide) the implementation.

A class may contain zero or more access specifiers, and there are no restrictions on how often an access specifier may appear. Each access specifier specifies the access level of the succeeding members. The specified access level remains in effect until the next access specifier or the end of the class body.

// within the complex class declaration
complex (double r = 0, double i = 0)
  : re (r), im (i)  // initializer list 
{ }

摘自C++ Primer:

Each class defines how objects of its type can be initialized. Classes control object initialization by defining one or more special member functions known as constructors. The job of a constructor is to initialize the data members of a class object. A constructor is run whenever an object of a class type is created.
constructor initializer list specifies initial values for one or more data members of the object being created. The constructor initializer is a list of member names, each of which is followed by that member’s initial value in parentheses (or inside curly braces). Multiple member initializations are separated by commas.

// within the complex class declaration
double real () const { return re; }
double imag () const { return im; }

// inside main()
const  complex c1(2, 1);
cout << c1.real();  // okay
cout << c2.imag();  // okay

摘自C++ Primer:

A const following the parameter list indicates that this is a pointer to const. Member functions that use const in this way are const member functions.
Objects that are const, and references or pointers to const objects, may call only const member functions.

A class can allow another class or function to access its nonpublic members by making that class or function a friend. A class makes a function its friend by including a declaration for that function preceded by the keyword friend.
Ordinarily it is a good idea to group friend declarations together at the beginning or end of the class definition.

Overloaded operators are functions with special names: the keyword operator followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type, a parameter list, and a body.
An overloaded operator function has the same number of parameters as the operator has operands. A unary operator has one parameter; a binary operator has two. In a binary operator, the left-hand operand is passed to the first parameter and the right-hand operand to the second. Except for the overloaded function-call operator, operator(), an overloaded operator may not have default arguments.
If an operator function is a member function, the first (left-hand) operand is bound to the implicit this pointer. Because the first operand is implicitly bound to this, a member operator function has one less (explicit) parameter than the operator has operands.

上一篇下一篇

猜你喜欢

热点阅读