1. C++基础
A. 类声明(Class Declaration)
class Name {
friend declaration
public:
public members
protected:
protected members
private:
private members
};
B. 派生类声明(Derived Class Declaration)
class Name : [virtual] <public|protected|private> base-class list {
class body
};
C. 函数定义(Function Definition)
valuetype fn_name(type arg1, type arg2, …) throw (exceptions) {
declarations and statements
}
D. 成员函数定义(Member Function Definition)
valuetype ClassName::fn_name(type arg1, type arg2, …) throw (exceptions) {
declarations and statements
}
E. 异常(Exceptions)
try {
statements
}
catch(e-type1 e) {
statements
}
catch(e-type2 e) {
statements
}
… …
catch(…) {
statements
}
F. 名字空间(Namespace)
namespace xyz { … … }
using xyz; // imports all names from namespace xyz
using xyz::ident; // imports ident from namespace xyz
G. 模板(Templates)
a) 模板函数
template <typename T, … …>
function prototype or definition
b) 模板类
template<class T, … …>
class declaration or definition
ClassName<types> obj(args);
H. 引用类型参数
Pass by value can be expensive if the arguments represent large data objects. Pass by reference is a way to pass arguments without copying. A reference parameter is an alias for the actual argument passed in a function call.
A reference parameter is used most often for the following purpose:
a) To pass an argument without making a copy of it.
b) To allow the called function to modify the actual argument.
c) To collect data produced by the called function.
I. 内联函数
For small functions, function call overhead becomes significant compared to the amount of time spent in the called function. To reduce this overhead and to increase code efficiency, C++ allows you to declare functions inline. For example,
inline int MAX(int a, int b) {
return a > b ? a : b;
}
J. 内存的分配和回收(new和delete)
K. 显示类型转换(static_cast<type>、const_cast<type *|&>、dynamic_cast<type *|&>)
a) dynamic_cast在转型转换时会进行类型检查,如果不能转换返回NULL
b) const_cast将只读对象转换成可读写对象的引用和指针
L. C++编程注意事项:
a) Declare functions, variables, and classes before using them.
b) Always terminate a declaration or a class definition with a semicolon.
c) A C-style character string is an array of characters terminated by ‘/0’. Consider using the C++ string class instead of the C-style string when you can.
d) Always remember the array is zero-based indexing.
e) Logical values are of type bool. Logical false is zero, and logical true is anything else.
f) The address-of operator & produces a pointer.
g) The value-of operator * produce a value through a pointer.
h) Expect for reference parameters, arguments of functions are always passed by value.
i) Avoid hard-coded constants; use const identifiers instead.
j) Avoid passing objects by value in function calls; use pass by reference whenever possible.
M. C++关键字
a) C语言中有的关键字
b) C语言中没有的关键字:
bool | catch | const_cast | delete | dynamic_cast | explicit |
export | false | friend | mutable | namespace | new |
operator | private | protected | public | reinterpret_cast | template |
this | throw | true | try | typeid | typename |
using | virtual | wchar_t |
|
|
|
c) 其他用于运算的关键字:
and | and_eq | bitand | bitor | compl | not |
or | or_eq | xor | xor_eq | not_eq |
|
2. 类和对象
A. 定义类
class Account {
public:
Account();
Account(unsigned n, double b);
void deposit(double amount);
bool withdraw(double amount);
double balance();
unsigned id();
private:
unsigned account_no;
double account_balance;
};
B. 创建对象
Account susan(5551234, 600.0); susan.balance();
Account *jack; jack = new Account; jack->balance();
C. 访问控制
public / protected / private
D. 成员函数
#include “Account.h”
void Account::deposit(double amount) {
if(amount > 0) {
account_balance += amount;
}
}
E. 构造器和成员初始化列表
Account::Account(unsigned id = 12345, double amount = 0.0) :
account_no(id), account_balance(amount) {
}
F. 析构器
Account::~Account() {
}
G. 友元(friend):可以访问类的private成员
a) 友元类
class Y {
friend class X; // all members of X are friends of Y
};
a) 友元函数
class Point {
friend double distance(const Point &p1, const Point &p2);
};
破坏封装性,能不用尽量不用
H. 实例成员和静态成员(static)
The difference between a static and a instance member function is the existence of the host pointer this:
a) A static function has no host object and therefore no access to this, explicitly or implicitly. Thus, a static function can refer to an instance member only through an explicit object.
b) An instance member function can be called only through an object of its class and is instantiated with a pointer to the host (this).
c) A static member function can be invoked either through an object or directly using the class scope operator. It has no host pointer and cannot reference instance members without going through an explicit object.
d) Constructors and destructors cannot be static.
I. 编程技巧:
a) Use capitalized nouns for class names. Join multiple words, and abbreviate if necessary, while capitalizing each word.
b) Put each class definition in a separate header file, and be sure to use the once-only header feature.
c) Put member function definitions in a corresponding .c or .cpp file, which uses #include to include its own header file.
d) In a class definition, put public members first and carefully document public member functions with comments. Specify the meaning of arguments.
e) Give only extremely simple member functions inside the class definition.
f) If a class has any constructor, provide a default one.
3. I/O流
4. 运算符重载
5. 异常
6. 面向对象技术