Upcoming SlideShare
×

Set Theory In C++

5,381 views

Published on

A class in C++ maps to a set in set theory.

Published in: Education, Technology
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
5,381
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
68
0
Likes
3
Embeds 0
No embeds

No notes for slide
• The first method of enumeration is not applicable in C++, and the second method defining commonality of the elements is used to declare and define a class in C++.
• A class is not discerned or classified by its private or protected members, but by its public ones which are visible.
• This basically restates the rules for member access (§15.3) [1]. We choose access for bases in the same way as for members. [1]
• Question: When should a composition class or a derived class be used? Answer: Composition is used to describe the relationship that “A is part of B” while the single inheritance is used to represents the relationship that “A is a kind (or subset) of B” . And the multiple inheritances represent the relationship that “A is a kind of B in the same time A is a kind C” or “A is the intersection of set B and set C”.
• By default, C++ matches a function call with the correct function definition at compile time. This is called static binding . You can specify that the compiler match a function call with the correct function definition at run time; this is called dynamic binding .
• By default, C++ matches a function call with the correct function definition at compile time. This is called static binding . You can specify that the compiler match a function call with the correct function definition at run time; this is called dynamic binding .
• Actually we can change Singleton a little bit to permit it to have some number of elements.
• For example, we can think A0 represents flower , A1 represents rose , and A2 represents red rose .
• For example, A_0 is integer , A1 is odd number , and A_2 is even numbe r. When A_1 has 2 more public functions compared to its base class A_0, then we can defined the below complete subclasses from A_0. class A_1: public A_0 { public: move(); draw(); }; class A_2: public A_0 { public: unmove(); draw(); }; class A_3: public A_0 { public: move(); undraw(); }; class A_4: public A_0 { public: unmove(); undraw(); };
• A_0 can be an abstract class or not.
• Actually we can pick up the two arbitrary classes and then derive a new class from them in C++, however we need to think of the semantics of what we are doing. For example, 1) the class of TCP process can be derived from the two classes: Process and TCP ; 2) some guy is a scientist and musician . class C: public A, protected B { … }; Only the friends of B, the derived classes of B and the friends of the derived classes of B think C is a kind of A and B. class C: public A, private B { … }; Only the friends of B think C is a kind of A and B.
• From above figure, we can understand why class window is declared as the virtual base of class window_with_menu and class window_with_border.
• 老子道德经：第一章 无名，天地之始；有名，万物之母。 故常无欲，以观其妙；常有欲，以观其徼。 此两者，同出而异名，同谓之玄。玄之又玄，众妙之门。 “ 无”可以用来表述天地浑沌未开之际的状况；而“有”，则是宇宙万物产生之本原的命名。 因此，要常从“无”中去观察领悟“道”的奥妙；要常从“有”中去观 察体会“道”的端倪。 无与有这两者，来源相同而名称相异，都可以称之为玄妙、深远。 它不是一般的玄妙、深奥，而是玄妙又玄妙、深远又深远，是宇宙天地万物之奥妙的总门 ( 从“有名”的奥妙到达无形的奥妙，“道”是洞悉一切奥妙变化的门径 ) 。
• Set Theory In C++

1. 1. Set Theory in C++ Andrew Zhuang [email_address] Aug.7, 2008
2. 2. Outline <ul><li>the Concept of a Class in C++ and a Set in the Set Theory </li></ul><ul><li>Commonality of a Set vs. Public Members of a Class </li></ul><ul><li>Empty Set vs. an Abstract Class </li></ul><ul><li>Single Element Set vs. Singleton </li></ul><ul><li>the Partition of a Set vs. Single Inheritance of a Class </li></ul><ul><li>Multiple Inheritances vs. Intersection of Multiple Sets </li></ul><ul><li>Modulization vs. Union of Sets </li></ul><ul><li>Single Inheritance of an Abstract Class vs. Template Class </li></ul><ul><li>Summary </li></ul><ul><li>Reference </li></ul>
3. 3. the Concepts of a class in C++ and a Set in the Set Theory <ul><li>The most fundamental notion of objected-oriented design and programming is that the program is a model of some aspects of reality . The classes in the program represent the fundamental concepts of the application and in particular, the fundamental concepts of the ‘reality’ being modeled. [1] </li></ul><ul><li>- Bjarne Stroustrup </li></ul><ul><li>A set represents a group of the objects with the same character. </li></ul><ul><li>A set is usually described in one of the two following ways [4]: </li></ul><ul><ul><li>By enumeration, e.g. {1, 2, 3} denotes the set consisting of the number 1,2,3 and nothing else; </li></ul></ul><ul><ul><li>By a defining property or commonality (sentential functional) p(x). Here it is important to define a universe U to which all the x have to belong. We then write P = {x : x U and p(x) is true} or simply P = {x: p(x)}. </li></ul></ul>
4. 4. Commonality of a Set vs. Public Members of a Class <ul><li>A class is discerned or classified by its public members : public data members and public member functions. </li></ul><ul><ul><li>Normally the data member is always declared as private one. </li></ul></ul><ul><li>The public (or visible) interfaces of a class represent the commonality of a set. </li></ul><ul><li>Equal Sets </li></ul><ul><ul><li>Two sets P and Q are equal, denoted by P = Q , if they contain the same elements. [4] </li></ul></ul><ul><li>Equal Classes in C++ </li></ul><ul><ul><li>There is no statement to judge whether the two arbitrary classes can be treated as same or duplicate. </li></ul></ul><ul><ul><li>It is meaningless to define the duplicate classes each of which presents the same interfaces or characters to the general users. </li></ul></ul>
5. 5. Commonality of a Set vs. Public Members of a Class <ul><ul><li>From the above declarations of class A and class B, we can see it is unnecessary to declare the duplicate classes. </li></ul></ul><ul><ul><li>class A { </li></ul></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>static int a; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>A(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>static int init(); </li></ul></ul><ul><ul><li>static int draw(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>class B { </li></ul></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>static int b; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>B(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>static int init(); </li></ul></ul><ul><ul><li>static int draw(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><li>Normally any two arbitrary classes with no derivation relationship are different from each other, for each of them has the public construction/destruction member function whose name is same as the class name. </li></ul><ul><li>Considering the below two classes: </li></ul><ul><ul><li>class A and class B present the same public interfaces to the general users. </li></ul></ul>
6. 6. Commonality of a Set vs. Public Members of a Class <ul><li>Access Control [1] </li></ul><ul><ul><li>A member of a class can be private, protected, or public: </li></ul></ul><ul><ul><ul><li>If it is private, its name can be used only by member functions and friends of the class in which it is declared. </li></ul></ul></ul><ul><ul><ul><li>If it is protected, its name can be used only by member functions and friends of the class in which it is declared and by member functions and friends of classes derived from this class. </li></ul></ul></ul><ul><ul><ul><li>If it is public, its name can be used by any function. </li></ul></ul></ul><ul><ul><li>This reflects the view that there are three kinds of functions accessing a class: functions implementing the class (its friends and members), functions implementing a derived class (the derived class’ friends and members), and other functions. </li></ul></ul>
7. 7. Commonality of a Set vs. Public Members of a Class <ul><li>Access to Base Classes [1] </li></ul><ul><ul><li>Like a member, a base class can be declared private, protected, or public. For example: </li></ul></ul><ul><ul><ul><li>class X: public B { /* …*/ }; </li></ul></ul></ul><ul><ul><ul><li>class Y: protected B { /*…*/ }; </li></ul></ul></ul><ul><ul><ul><li>class Z: private B { /*…*/ }; </li></ul></ul></ul><ul><ul><li>Access specifier for a base class controls the access to members of the base class and the conversion of pointers and references from the derived class type to the base class type. Consider a class D derived from a base class B: </li></ul></ul><ul><ul><ul><li>If B is a private base, its public and protected members can be used only by member functions and friends of D. Only friends and members of D can convert a D * to a B *. </li></ul></ul></ul><ul><ul><ul><li>If B is a protected base, its public and protected members can be used only by member functions and friends of D and by member functions and friends of classes derived from D. Only friends and members of D and friends and members of classes derived from D can convert a D * to a B *. </li></ul></ul></ul><ul><ul><ul><li>If B is a public base, its public members can be used by any function. In addition, its protected members can be used by members and friends of D and members and friends of classes derived from D. Any function can convert a D * to a B *. </li></ul></ul></ul>
8. 8. Commonality of a Set vs. Public Members of a Class <ul><li>The Visible Level of Member Functions in a Class to Other Guys than Himself </li></ul><ul><li>Low: private members can only been seen by his friends. </li></ul><ul><li>Medium: protected members can only been seen by his friends, his derived classes, and the friends of his derived classes. </li></ul><ul><li>High: public members can be seen by anyone. </li></ul><ul><li>Class D is derived from class B. </li></ul><ul><ul><li>The visible level of the public and protected members in B can be maintained as before, or downgraded to the lower. </li></ul></ul><ul><li>When B is a private base, both the visible levels of the public and protected members are downgraded from high/medium to low as private ones. </li></ul><ul><li>When B is a protected base, both the visible levels of the public and protected members are downgraded from high/medium to medium as protected ones. </li></ul><ul><li>When B is a public base, both the visible levels of the public and protected members are maintained as before. </li></ul>
9. 9. Commonality of a Set vs. Public Members of a Class <ul><li>B is private base </li></ul><ul><li>Just considering only the public and protected members of B, D1 can be supposed as below. </li></ul><ul><ul><li>class D1 { </li></ul></ul><ul><ul><li>… // something handling with B’s private members that can be seen or </li></ul></ul><ul><ul><li>// used by member functions and friends of B. </li></ul></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>int b; </li></ul></ul><ul><ul><li>int c; </li></ul></ul><ul><ul><li>int B(); </li></ul></ul><ul><ul><li>int foo(); </li></ul></ul><ul><ul><li>int goo(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>D1() { B(); } </li></ul></ul><ul><ul><li>int koo(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>Class E1 is derived from D1 with public inheritance, or protected inheritance or protected inheritance. </li></ul></ul><ul><ul><li>class B { </li></ul></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>int a; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int b; </li></ul></ul><ul><ul><li>int foo(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>int c; </li></ul></ul><ul><ul><li>int B(); </li></ul></ul><ul><ul><li>int goo(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>class D1: private B { </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>D1():B() { } </li></ul></ul><ul><ul><li>int koo(); </li></ul></ul><ul><ul><li>}; </li></ul></ul>
10. 10. Commonality of a Set vs. Public Members of a Class <ul><li>B is private base </li></ul><ul><ul><li>The public interfaces of D1 are presented differently to the different guys. </li></ul></ul><ul><ul><li>Someone can use or see the same public interfaces of D1 as those of B, like c, B(), and goo() in this case, he thinks D1 is a kind of B; otherwise, he thinks D1 is not a kind of B. </li></ul></ul><ul><ul><li>The friends of B think D1 is not a kind of B. </li></ul></ul><ul><ul><li>The friends of D1 think D1 is a kind of B. </li></ul></ul><ul><ul><li>The classes derived from D1, like E1, and the friends of the classes derived from D1 , like the friends of E1, think D1 is not a kind of B. </li></ul></ul><ul><ul><li>The general users think D1 is a not kind of B. </li></ul></ul><ul><li>Conclusion: Only the friends of D1 think D1 is a kind of B which is the private base of D1. </li></ul>
11. 11. Commonality of a Set vs. Public Members of a Class <ul><li>B is protected base </li></ul><ul><li>Just considering only the public and protected members of B, D2 can be supposed as below. </li></ul><ul><ul><li>class D2 { </li></ul></ul><ul><ul><li>… // something handling with B’s private members that can be seen or </li></ul></ul><ul><ul><li>// used by member functions and friends of B. </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int b; </li></ul></ul><ul><ul><li>int c; </li></ul></ul><ul><ul><li>int B(); </li></ul></ul><ul><ul><li>int foo(); </li></ul></ul><ul><ul><li>int goo(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>D1() { B(); } </li></ul></ul><ul><ul><li>int loo(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>Class E2 is derived from D2 with public inheritance, or protected inheritance or protected inheritance. </li></ul></ul><ul><li>class B { </li></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>int a; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int b; </li></ul></ul><ul><ul><li>int foo(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>int c; </li></ul></ul><ul><ul><li>int B(); </li></ul></ul><ul><ul><li>int goo(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>class D2: protected B { </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>D2():B() { } </li></ul></ul><ul><ul><li>int loo(); </li></ul></ul><ul><ul><li>}; </li></ul></ul>
12. 12. Commonality of a Set vs. Public Members of a Class <ul><li>B is protected base </li></ul><ul><ul><li>The friends of B think D2 is a not kind of B. </li></ul></ul><ul><ul><li>The friends of D2, the classes derived from D2, like E2, and the friends of the classes derived from D2, like the friends of E2, think D2 is a kind of B. </li></ul></ul><ul><ul><li>The general users think D2 is a not kind of B. </li></ul></ul><ul><li>Conclusion: Only the friends of D2, the classes from D2 and the friends of the classes derived from D2 think D2 is a kind of B which is the protected base of D2. </li></ul>
13. 13. Commonality of a Set vs. Public Members of a Class <ul><li>B is public base </li></ul><ul><li>Just considering only the public and protected members of B, D2 can be supposed as below. </li></ul><ul><ul><li>class D3 </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>… // something handling with B’s private members that can be seen or </li></ul></ul><ul><ul><li>// used by memnber functions and friends of B. </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int b; </li></ul></ul><ul><ul><li>int foo(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>int c; </li></ul></ul><ul><ul><li>B(); </li></ul></ul><ul><ul><li>D3() { B(); } </li></ul></ul><ul><ul><li>int goo(); </li></ul></ul><ul><ul><li>int moo(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>Class E3 is derived from D3 with public inheritance, or protected inheritance or protected inheritance. </li></ul></ul><ul><li>class B { </li></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>int a; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int b; </li></ul></ul><ul><ul><li>int foo(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>int c; </li></ul></ul><ul><ul><li>int B(); </li></ul></ul><ul><ul><li>int goo(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>class D3: public B { </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>D3():B() { } </li></ul></ul><ul><ul><li>int moo(); </li></ul></ul><ul><ul><li>}; </li></ul></ul>
14. 14. Commonality of a Set vs. Public Members of a Class <ul><li>B is public base </li></ul><ul><ul><li>The friends of B think D3 is a kind of B. </li></ul></ul><ul><ul><li>The friends of D3, the classes derived from D3, like E3, and the friends of the classes derived from D2, like the friends of E2, think D3 is a kind of B. </li></ul></ul><ul><ul><li>The general users think D3 is a kind of B. </li></ul></ul><ul><li>Conclusion: Everyone think D3 is a kind of B which is the public base of D3. </li></ul>
15. 15. Commonality of a Set vs. Public Members of a Class <ul><li>B is public base </li></ul><ul><ul><li>The friends of B think D3 is a kind of B. </li></ul></ul><ul><ul><li>The friends of D3, the classes derived from D3, like E3, and the friends of the classes derived from D2, like the friends of E2, think D3 is a kind of B. </li></ul></ul><ul><ul><li>The general users think D3 is a kind of B. </li></ul></ul><ul><li>Conclusion: Everyone think D3 is a kind of B which is the public base of D3. </li></ul>
16. 16. Commonality of a Set vs. Public Members of a Class <ul><li>An Example on Class Hierarchies </li></ul><ul><ul><li>class A { </li></ul></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>int a1; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int a2; </li></ul></ul><ul><ul><li>int aoo_1(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>int a3; </li></ul></ul><ul><ul><li>int A(); </li></ul></ul><ul><ul><li>int aoo_2(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>class B : public A { </li></ul></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>int b1; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int b2; </li></ul></ul><ul><ul><li>int boo_1(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>int b3; </li></ul></ul><ul><ul><li>B():A() { }; </li></ul></ul><ul><ul><li>int boo_2(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>class C : private B { </li></ul></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>int c1; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int c2; </li></ul></ul><ul><ul><li>int coo_1(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>int c3; </li></ul></ul><ul><ul><li>C():B() { }; </li></ul></ul><ul><ul><li>int coo_2(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>class D : protected C { </li></ul></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>int d1; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int d2; </li></ul></ul><ul><ul><li>int doo_1(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>int d3; </li></ul></ul><ul><ul><li>D():C() { }; </li></ul></ul><ul><ul><li>int doo_2(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>class D : { </li></ul></ul><ul><ul><li>private: </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>int d1; </li></ul></ul><ul><ul><li>protected: </li></ul></ul><ul><ul><li>int c2; </li></ul></ul><ul><ul><li>int c3; </li></ul></ul><ul><ul><li>int d2; </li></ul></ul><ul><ul><li>int C():B() { }; </li></ul></ul><ul><ul><li>int coo_1(); </li></ul></ul><ul><ul><li>int coo_2(); </li></ul></ul><ul><ul><li>int doo_1(); </li></ul></ul><ul><ul><li>public: </li></ul></ul><ul><ul><li>int d3; </li></ul></ul><ul><ul><li>D():C() { }; </li></ul></ul><ul><ul><li>int doo_2(); </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><li>Everyone thinks B is a kind A . </li></ul><ul><li>Only the friends of C think 1) C is a kind of B and 2) C is a kind of A. </li></ul><ul><li>The friends of D, the derive classes of D, and the friends of the derived classes of D think that D is a kind C , however D is not a kind of B or A . </li></ul>
17. 17. Commonality of a Set vs. Public Members of a Class <ul><li>Virtual Member Function </li></ul><ul><ul><li>A class that declares or inherits a virtual function is called a polymorphic class . </li></ul></ul><ul><ul><li>It provides a way to define the commonality across the base class and its derived classes. </li></ul></ul><ul><li>Pure Virtual Member Function </li></ul><ul><ul><li>An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function . You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration. </li></ul></ul>
18. 18. Empty Set vs. an Abstract Class <ul><li>Empty Set </li></ul><ul><ul><li>The set with no elements is called empty set and denoted by Ø [4]. </li></ul></ul><ul><li>Abstract Class </li></ul><ul><ul><li>An abstract class contains at least one pure virtual function . </li></ul></ul><ul><ul><li>An abstract class is a class that is designed to be specifically used as a base class. </li></ul></ul><ul><ul><li>You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration. </li></ul></ul>
19. 19. Single Element Set vs. Singleton <ul><li>A single emelment set can have only one element. </li></ul><ul><li>Singleton [3] </li></ul>class Singleton { public: static Singleton* Instance(); protected: Singleton(); private: static Singleton* _instance; }; <ul><li>Conclusion: A single element corresponds to the pattern of singleton. </li></ul>Singleton* Singleton::_instance = 0; Singleton* Singleton::Instance() { if (_instance == 0) { _instance = new Singleton; } return _instance; }
20. 20. the Partition of a Set vs. Single Inheritance of a Class class A0 { }; class A1: public A0 { }; class A2: public A1 { }; <ul><li>Conclusion: A new derived class inherited from its base class means that a new partition/subset is created inside its base set. </li></ul><ul><li>a0 is an instance of A0. </li></ul><ul><li>a1 is an instance of A1. </li></ul><ul><li>a2 is an instance of A2. </li></ul>
21. 21. the Partition of a Set vs. Single Inheritance of a Class <ul><li>Split a base class into 2 derived classes with the opposite semantics </li></ul>class A_0 { }; class A_1 : public A_0 { public: move(); }; class A_2 : public A_2 { public: unmove(); }; <ul><li>If A_1 has N more public member functions compared to A_0, then the number of all his sibling classes is (2**N-1). </li></ul><ul><li>For example, N = 2, </li></ul>
22. 22. the Partition of a Set vs. Single Inheritance of a Class <ul><li>Two derive classes sharing the same semantic as their base one </li></ul>class A_0 { }; class A_1 : public A_0 { }; class A_2 : public A_2 { }; <ul><li>Putting aside the public construction/destruction functions of A_1, we can think A_1 is same as A_0, and same to A_2. </li></ul><ul><li>According to the above reason, it is meaningless to define A_1 and A_2 like above. </li></ul>
23. 23. Multiple Inheritances vs. Intersection of Multiple Sets class A { … }; class B { … }; class C: public A, public B { … }; <ul><li>c0 is an instance of C. </li></ul><ul><li>c0 is an instance of A while c0 is an instance of B as well. </li></ul><ul><li>C has multiple features/commonalities from A and B. </li></ul>
24. 24. Multiple Inheritances vs. Intersection of Multiple Sets <ul><li>DAG (Directed Acyclic Graph) Inheritance from [2] </li></ul>class window { … }; class window_with_menu: public virtual window { … }; class window_with_border: public virtual windw { … }; class window_with_menu_and_border: public window_with_menu, public window_with_border { … }; A: window B: window_with_menu , C: window_with_border D: window_with_menu_and_border
25. 25. Multiple Inheritances vs. Intersection of Multiple Sets <ul><ul><li>template <class U, class V> class A : public U, public V { … }; </li></ul></ul><ul><ul><li>It is a general representation of the intersection among multiple arbitrary sets. </li></ul></ul><ul><ul><li>U or V represents a set of the arbitrary classes. </li></ul></ul><ul><ul><li>A represents a set of the sets each of which is a class derived from the instance class of U and the instance class of V. And a0 is an instance or element of A. </li></ul></ul><ul><li>Template Class </li></ul>
26. 26. Modulization vs.Union of Sets <ul><li>When we combines the different classes into a file, or combines the different classes scattered over the different files into a module, actually the file or the module constitutes a union of the different sets. </li></ul><ul><ul><li>For example, namespace in C++. </li></ul></ul>
27. 27. Single Inheritance from an Abstract Class <ul><li>Single Inheritance of a Class </li></ul><ul><ul><li>Here is an example from [1]. </li></ul></ul>class Shape { public: virtual void rotate(int) = 0; virtual void draw() = 0; virtual bool is_closed() = 0; // … }; class Circle : public Shape { public: void rotate(int) { } void draw(); bool is_closed() { return true;} Circle(Point p, int r); private: Point Center; int radius; }; class Polygon : public shape { public: bool is_closed() { return true;} // … draw and rotate not overriden… }; <ul><li>We create the concrete classes having infinite instances from an abstract class or an empty set. </li></ul><ul><li>无 , 名天地之始；有 , 名万物之母。 故常无欲，以观其妙；常有欲，以观其徼。此两者，同出而异名，同谓之玄。玄之又玄，众妙之门。 </li></ul><ul><ul><li>( 老子道德经：第一章 ) </li></ul></ul><ul><ul><li>Heaven and Earth began from the nameless (Tao), but the multitudes of things around us were created by names. </li></ul></ul><ul><ul><li>We desire to understand the world by giving names to the things we see, but these things are only the effects of something subtle. When we see beyond the desire to use names, we can sense the nameless cause of these effects. The cause and the effects are aspects of the same, one thing. </li></ul></ul><ul><ul><li>They are both mysterious and profound. At their most mysterious and profound point lies the &quot;Gate of the Great Truth&quot;. (Lao Tzu - Tao Te Ching: Chapter 1) </li></ul></ul>
28. 28. Single Inheritance from an Abstract Class <ul><li>Template Class </li></ul><ul><ul><li>Here is an example from [1]. </li></ul></ul>template <class T> class Stack { T* v; int max_size; int top; public; class Underflow { }; class Overflow() { }; Stack(int s); // constructor ~Stack(); // destructor void push(T); T pop(); }; typedef Stack<char> Stack_char; typedef Stack<dog *> Stack_pdog; typedef Stack<list<int>> Stack_li <ul><li>Stack is the union of all the instance of this template: Stack_char,Stack_pdog, and Stack_li and the other ones. </li></ul><ul><li>Stack_x is an arbitrary instance of Stack and same to Stack_y. And there is no common elements between Stack_x and Stack_y. </li></ul><ul><li>With the template class, we do not need to write the duplicate codes for different kinds of types managed by stacks. </li></ul>
29. 29. Summary <ul><li>This article adds nothing new to C++ and the set theory, but builds a concept bridge between them. </li></ul><ul><li>Here are the benefits from reading this article. </li></ul><ul><ul><li>It gives the mapping relationship between the core ideas of objected-oriented programming and the set theory so it is a great benefit to object-oriented design, analysis and programming. Actually object-oriented programming means the definitions and operations in the set theory. </li></ul></ul><ul><ul><li>Object-oriented programming should reflect the semantic in the real world, not on the programming language level. That is to say that programming should reflect the semantics of the reality. </li></ul></ul><ul><ul><li>It frees the C++ programmers from the heavy syntax of C++, for a programmer should not be a salve of the complex syntax of any programming language. </li></ul></ul><ul><ul><li>It can speed up the learning curve for the C++ novices, and unfolds a new scenario and thinking way to the advanced C++ programmers. </li></ul></ul>
30. 30. References <ul><li>[1] Bjarne Stroustrup, 2000, The C++ Programming Language, special edition, Addison-Wesley, Reading, Mass. </li></ul><ul><li>[2] Bjarne Stroustrup, 1994, The Design and Evolution of C++, Addison-Wesley, Reading, Mass. </li></ul><ul><li>[3] Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, 1995, Design Patterns: Elements of Reusable Object-Oriented Software, 1st ed, Addison-Wesley, Reading, Mass. </li></ul><ul><li>[4] William Chen, 2008, Discrete Mathematics, Available at http://www.maths.mq.edu.au/~wchen/lndmfolder/lndm.html </li></ul>
31. 31. Thank You!