Ray's playground

 

Item 34: Differentiate between inheritance of interface and inheritance of implementation(Effective C++)

Inheritance of interface is different from inheritance of implementation. Under public inheritance, derived classes always inherit base class interfaces.
Pure virtual functions specify inheritance of interface only.
Simple (impure) virtual functions specify inheritance of interface plus inheritance of a default implementation.
Non-virtual functions specify inheritance of interface plus inheritance of a mandatory implementation.

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class Shape
 6 {
 7 public:
 8     virtual void draw() const = 0;
 9     virtual void error(const string& msg)
10     {
11         cout << msg << endl;
12     }
13     
14 
15 };
16 
17 void Shape::draw() const
18 {
19     cout << "draw in Shape" << endl;
20 }
21 
22 class Rectangle: public Shape
23 {
24 public:
25     virtual void draw() const
26     {
27         cout << "draw in Rectangle" << endl;
28     }
29 };
30 
31 int main()
32 {
33     Rectangle *= new Rectangle();
34     r->draw();
35     r->Shape::draw();
36     cin.get();
37     return 0;
38 }

posted on 2011-04-02 16:42  Ray Z  阅读(357)  评论(0编辑  收藏  举报

导航