The basic knowledge, manager is a person, apple is a fruit, and
bla bla bla.
Reused code, pure virtual, virtual, and non virtual, Such as
introduced in inheritance section.
Isolate change between Client and Implement. That is the
high level in design pattern.
How to understand interface? Client<->Interface<->Implement. through
Interface, you keep all the change happen implement side, not affect Client
at all.
No source code, just header and lib, you also can use inheritance in C++
language.
When a method is declared virtual in a base class, it is automatically virtual
in the derived class, but it is a good idea to document which functions are
virtual by using the keyword virtual in the derived class declarations
too.
Pure virtual class can’t be instance, it just a abstract interface, it’s
agreement.
Private and protect inheritances are used to implement has-a relationship.
Protect used in three generation inheritance. By protected Inheritance,
grandfather member become protected member in father, so Grandson can
still use grandfather’s members.
In short, Containment approach uses object names to invoke a method,
whereas private Inheritance uses the class name and scope resolution
operator Instead. If you need the base class itself„ use a type case (const
string&) * this; detail can be seen in C++ primer, P800
Four methods to Reuse code 1) Public Inheritance, it’s a IS-A relationship 2) Private Inheritance , It’s a Has-A relationship 3) Composition, aggregation. delegate, association, other objects
4) Template Prefer use containment to reuse code.
When do you need to use private inheritance.1) You want to use base class
protected member 2) Redefine base class virtual function.
Multiple Inheritance will cause one grandson has two copy of grandfathers.
A solution is use virtual key word: When you use this method, you need to
follow New Constructor Rules, Detail can be seen in C++ primer P815. Just
look back when you really need MI.
A inheritance is "is-a" relationship. But it’s not always true. A good design
example about inheritance is circle and ellipse, a circle is ellipse, but when
you use C++ to implement it. You will find ellipse has long and
short axis which circle doesn’t need at all. And in derived class you
can’t subtract but only add to base class. A solution is make an
abstract base class(ABC) then make circle and ellipse inherit from
ABC
Previous example also explain, when you design a class, it doesn’t need to
be a practical object, such as animal, car, people, etc. It can be abstract
conception. (So desgin pattern is so important conception).
Both reference and pointer support polymorphism.
Friend can’t be virtual function, because it’s not a member of class.
Almost all the base class has virtual function, if a class doesn’tcontain a virtual function, It is ain indication that it is not meantto be used as a base class
Don’t rewrite non-virtual base member function, see effective c++
Only virtual function come into vtbl
P777 table 13.1
Inheritance three usage: 1) I want to inherit a interface. (pure virtual, you
have to rewrite ) 2) I want to inherit a implement,but I want to change it.
(virtual, you may or maynot rewrite) 3) I want to inherit a implement, but I
don’t want to change it. (Non-virtual, you can’t rewrite it at all)
Sometimes, syntax is right, but it doesn’t mean it is logical right in practical
application. For example, You can define a empty virtual function, but if
it’s not pure virtual, A new derived class will inheritance default
implementation. In this way, a better method is make it become pure
virtual. It will oblige all the derive class give it’s own implementation. It
can be thought as strategy pattern, it looks like template method,
but it’s not exactly the same. If two derived class share the same
implementation, you can upgrade the implementation to base class, and
make a protect member function, it make derive class can access
it in it’s own implementation base class pure virtual function. A
practical example can be seen in effective C++ item 36 Airplane
example.