課程目標(biāo)
- 以良好的方式編寫(xiě)C++ class
(Object Based: 面對(duì)的是單一class的設(shè)計(jì))- class without pointer members -- Complex (week 1)
- class with pointer members -- String
- 學(xué)習(xí)Classes之間的關(guān)系
(Object Oriented: 面對(duì)的是多重classes的設(shè)計(jì), classes和classes之間的關(guān)系)- 繼承 (inheritance)
- 復(fù)合 (composition)
- 委托 (delegation)
推薦書(shū)目
- C++ Primer (5th Edition) by Stanley B. Lippman
- The C++ Programming Language (4th Edition) by Bjarne Stroustrup
- Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition) by Scott Meyers
- The C++ Standard Library - A Tutorial and Reference (2nd Edition) by Nicolai M. Josuttis
- STL源碼剖析 by 侯捷
點(diǎn)評(píng): 選擇一本好的C++書(shū)籍(1和2兩本書(shū)皆可), 然后以及盡量多的完成書(shū)籍的習(xí)題,可以幫助完成C++語(yǔ)法的學(xué)習(xí); 熟悉C++的語(yǔ)法后,通過(guò)3學(xué)習(xí)如何正確高效地使用C++; 如果對(duì)STL的一些實(shí)現(xiàn)有興趣,可參考書(shū)目4和5.
復(fù)數(shù)Complex類(lèi)的設(shè)計(jì)
主程序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 (類(lèi)-聲明)
// class definition (類(lèi)-定義)
#endif // __COMPLEX__
關(guān)于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 (類(lèi)-聲明)
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&);
};
有些函數(shù)(e.g., constructor, real, imag)在此直接定義 (自動(dòng)成為inline候選人), 另一些函數(shù)(e.g., operator+=, __doapl)在class body之外定義:
// class definition (類(lèi)-定義)
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);
}
解釋幾個(gè)重要概念:
- 關(guān)于inline(內(nèi)聯(lián))函數(shù) (摘自C++ Primer):
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.
- 關(guān)于access level (訪問(wèn)級(jí)別) (摘自C++ Primer):
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.
- 關(guān)于constructor (構(gòu)造函數(shù))
// 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.
- 關(guān)于const member function (常量成員函數(shù))
// 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 toconst
. Member functions that useconst
in this way are const member functions.
Objects that areconst
, and references or pointers to const objects, may call only const member functions.
- 關(guān)于 friend (友元) (摘自C++ Primer):
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.
- 關(guān)于operator overloading (操作符重載) (摘自C++ Primer):
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 tothis
, a member operator function has one less (explicit) parameter than the operator has operands.