C++面向对象高级编程(上) 第三周笔记 GeekBand
复合:has a
构造由内而外:
Container的构造函数首先调用component的default(默认)构造函数,然后再执行自己的构造函数:
Container::container(...):component(){...};
析构由外而内:
Container的析构函数首先析构自己,然后再析构component:
Container::~container() {... ~component()};
Delegation:has a (composition by reference)
继承:is a
构造由内而外:
Derived的构造函数首先调用Base的default(默认)构造函数,然后再执行自己的构造函数:
Derived::Derived(...):Base(){...};
析构由外而内:
Derived的析构函数首先析构自己,然后再析构Base:
Derived::~Derived() {... ~Base()};
Base class 的析构必须是虚析构
非虚函数:你不希望derived class重新定义
虚函数:你希望derived class重新定义,并且有默认定义
纯虚函数:没有默认定义,derived class必须重新定义
继承函数是继承调用权
子类对象调用父类函数:
1.conversion fnction:转换函数
Class Fraction
{
public:
Fraction(int num,int den=1):m_numerator(num),m_denominator(den){}
operator double() const {
return (double)(m_numerator/m_denominator);
}
private:
int numerator;
int denominator;
};
Non-explicit-one-argument ctor:
Class Fraction{
Fraction operator +(const Fraction& f){
Return Fraction(...);
...}