• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Set Theory In C++
 

Set Theory In C++

on

  • 5,046 views

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

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

Statistics

Views

Total Views
5,046
Views on SlideShare
5,038
Embed Views
8

Actions

Likes
3
Downloads
49
Comments
0

1 Embed 8

http://www.slideshare.net 8

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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++ Set Theory In C++ Presentation Transcript

  • Set Theory in C++ Andrew Zhuang [email_address] Aug.7, 2008
  • Outline
    • the Concept of a Class in C++ and a Set in the Set Theory
    • Commonality of a Set vs. Public Members of a Class
    • Empty Set vs. an Abstract Class
    • Single Element Set vs. Singleton
    • the Partition of a Set vs. Single Inheritance of a Class
    • Multiple Inheritances vs. Intersection of Multiple Sets
    • Modulization vs. Union of Sets
    • Single Inheritance of an Abstract Class vs. Template Class
    • Summary
    • Reference
  • the Concepts of a class in C++ and a Set in the Set Theory
    • 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]
    • - Bjarne Stroustrup
    • A set represents a group of the objects with the same character.
    • A set is usually described in one of the two following ways [4]:
      • By enumeration, e.g. {1, 2, 3} denotes the set consisting of the number 1,2,3 and nothing else;
      • 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)}.
  • Commonality of a Set vs. Public Members of a Class
    • A class is discerned or classified by its public members : public data members and public member functions.
      • Normally the data member is always declared as private one.
    • The public (or visible) interfaces of a class represent the commonality of a set.
    • Equal Sets
      • Two sets P and Q are equal, denoted by P = Q , if they contain the same elements. [4]
    • Equal Classes in C++
      • There is no statement to judge whether the two arbitrary classes can be treated as same or duplicate.
      • It is meaningless to define the duplicate classes each of which presents the same interfaces or characters to the general users.
  • Commonality of a Set vs. Public Members of a Class
      • From the above declarations of class A and class B, we can see it is unnecessary to declare the duplicate classes.
      • class A {
      • private:
      • static int a;
      • protected:
      • A();
      • public:
      • static int init();
      • static int draw();
      • };
      • class B {
      • private:
      • static int b;
      • protected:
      • B();
      • public:
      • static int init();
      • static int draw();
      • };
    • 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.
    • Considering the below two classes:
      • class A and class B present the same public interfaces to the general users.
  • Commonality of a Set vs. Public Members of a Class
    • Access Control [1]
      • A member of a class can be private, protected, or public:
        • If it is private, its name can be used only by member functions and friends of the class in which it is declared.
        • 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.
        • If it is public, its name can be used by any function.
      • 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.
  • Commonality of a Set vs. Public Members of a Class
    • Access to Base Classes [1]
      • Like a member, a base class can be declared private, protected, or public. For example:
        • class X: public B { /* …*/ };
        • class Y: protected B { /*…*/ };
        • class Z: private B { /*…*/ };
      • 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:
        • 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 *.
        • 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 *.
        • 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 *.
  • Commonality of a Set vs. Public Members of a Class
    • The Visible Level of Member Functions in a Class to Other Guys than Himself
    • Low: private members can only been seen by his friends.
    • Medium: protected members can only been seen by his friends, his derived classes, and the friends of his derived classes.
    • High: public members can be seen by anyone.
    • Class D is derived from class B.
      • The visible level of the public and protected members in B can be maintained as before, or downgraded to the lower.
    • 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.
    • 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.
    • When B is a public base, both the visible levels of the public and protected members are maintained as before.
  • Commonality of a Set vs. Public Members of a Class
    • B is private base
    • Just considering only the public and protected members of B, D1 can be supposed as below.
      • class D1 {
      • … // something handling with B’s private members that can be seen or
      • // used by member functions and friends of B.
      • private:
      • int b;
      • int c;
      • int B();
      • int foo();
      • int goo();
      • public:
      • D1() { B(); }
      • int koo();
      • };
      • Class E1 is derived from D1 with public inheritance, or protected inheritance or protected inheritance.
      • class B {
      • private:
      • int a;
      • protected:
      • int b;
      • int foo();
      • public:
      • int c;
      • int B();
      • int goo();
      • };
      • class D1: private B {
      • public:
      • D1():B() { }
      • int koo();
      • };
  • Commonality of a Set vs. Public Members of a Class
    • B is private base
      • The public interfaces of D1 are presented differently to the different guys.
      • 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.
      • The friends of B think D1 is not a kind of B.
      • The friends of D1 think D1 is a kind of B.
      • 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.
      • The general users think D1 is a not kind of B.
    • Conclusion: Only the friends of D1 think D1 is a kind of B which is the private base of D1.
  • Commonality of a Set vs. Public Members of a Class
    • B is protected base
    • Just considering only the public and protected members of B, D2 can be supposed as below.
      • class D2 {
      • … // something handling with B’s private members that can be seen or
      • // used by member functions and friends of B.
      • protected:
      • int b;
      • int c;
      • int B();
      • int foo();
      • int goo();
      • public:
      • D1() { B(); }
      • int loo();
      • };
      • Class E2 is derived from D2 with public inheritance, or protected inheritance or protected inheritance.
    • class B {
      • private:
      • int a;
      • protected:
      • int b;
      • int foo();
      • public:
      • int c;
      • int B();
      • int goo();
      • };
      • class D2: protected B {
      • public:
      • D2():B() { }
      • int loo();
      • };
  • Commonality of a Set vs. Public Members of a Class
    • B is protected base
      • The friends of B think D2 is a not kind of B.
      • 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.
      • The general users think D2 is a not kind of B.
    • 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.
  • Commonality of a Set vs. Public Members of a Class
    • B is public base
    • Just considering only the public and protected members of B, D2 can be supposed as below.
      • class D3
      • {
      • … // something handling with B’s private members that can be seen or
      • // used by memnber functions and friends of B.
      • protected:
      • int b;
      • int foo();
      • public:
      • int c;
      • B();
      • D3() { B(); }
      • int goo();
      • int moo();
      • };
      • Class E3 is derived from D3 with public inheritance, or protected inheritance or protected inheritance.
    • class B {
      • private:
      • int a;
      • protected:
      • int b;
      • int foo();
      • public:
      • int c;
      • int B();
      • int goo();
      • };
      • class D3: public B {
      • public:
      • D3():B() { }
      • int moo();
      • };
  • Commonality of a Set vs. Public Members of a Class
    • B is public base
      • The friends of B think D3 is a kind of B.
      • 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.
      • The general users think D3 is a kind of B.
    • Conclusion: Everyone think D3 is a kind of B which is the public base of D3.
  • Commonality of a Set vs. Public Members of a Class
    • B is public base
      • The friends of B think D3 is a kind of B.
      • 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.
      • The general users think D3 is a kind of B.
    • Conclusion: Everyone think D3 is a kind of B which is the public base of D3.
  • Commonality of a Set vs. Public Members of a Class
    • An Example on Class Hierarchies
      • class A {
      • private:
      • int a1;
      • protected:
      • int a2;
      • int aoo_1();
      • public:
      • int a3;
      • int A();
      • int aoo_2();
      • };
      • class B : public A {
      • private:
      • int b1;
      • protected:
      • int b2;
      • int boo_1();
      • public:
      • int b3;
      • B():A() { };
      • int boo_2();
      • };
      • class C : private B {
      • private:
      • int c1;
      • protected:
      • int c2;
      • int coo_1();
      • public:
      • int c3;
      • C():B() { };
      • int coo_2();
      • };
      • class D : protected C {
      • private:
      • int d1;
      • protected:
      • int d2;
      • int doo_1();
      • public:
      • int d3;
      • D():C() { };
      • int doo_2();
      • };
      • class D : {
      • private:
      • int d1;
      • protected:
      • int c2;
      • int c3;
      • int d2;
      • int C():B() { };
      • int coo_1();
      • int coo_2();
      • int doo_1();
      • public:
      • int d3;
      • D():C() { };
      • int doo_2();
      • };
    • Everyone thinks B is a kind A .
    • Only the friends of C think 1) C is a kind of B and 2) C is a kind of A.
    • 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 .
  • Commonality of a Set vs. Public Members of a Class
    • Virtual Member Function
      • A class that declares or inherits a virtual function is called a polymorphic class .
      • It provides a way to define the commonality across the base class and its derived classes.
    • Pure Virtual Member Function
      • 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.
  • Empty Set vs. an Abstract Class
    • Empty Set
      • The set with no elements is called empty set and denoted by Ø [4].
    • Abstract Class
      • An abstract class contains at least one pure virtual function .
      • An abstract class is a class that is designed to be specifically used as a base class.
      • You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
  • Single Element Set vs. Singleton
    • A single emelment set can have only one element.
    • Singleton [3]
    class Singleton { public: static Singleton* Instance(); protected: Singleton(); private: static Singleton* _instance; };
    • Conclusion: A single element corresponds to the pattern of singleton.
    Singleton* Singleton::_instance = 0; Singleton* Singleton::Instance() { if (_instance == 0) { _instance = new Singleton; } return _instance; }
  • the Partition of a Set vs. Single Inheritance of a Class class A0 { }; class A1: public A0 { }; class A2: public A1 { };
    • Conclusion: A new derived class inherited from its base class means that a new partition/subset is created inside its base set.
    • a0 is an instance of A0.
    • a1 is an instance of A1.
    • a2 is an instance of A2.
  • the Partition of a Set vs. Single Inheritance of a Class
    • Split a base class into 2 derived classes with the opposite semantics
    class A_0 { }; class A_1 : public A_0 { public: move(); }; class A_2 : public A_2 { public: unmove(); };
    • 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).
    • For example, N = 2,
  • the Partition of a Set vs. Single Inheritance of a Class
    • Two derive classes sharing the same semantic as their base one
    class A_0 { }; class A_1 : public A_0 { }; class A_2 : public A_2 { };
    • 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.
    • According to the above reason, it is meaningless to define A_1 and A_2 like above.
  • Multiple Inheritances vs. Intersection of Multiple Sets class A { … }; class B { … }; class C: public A, public B { … };
    • c0 is an instance of C.
    • c0 is an instance of A while c0 is an instance of B as well.
    • C has multiple features/commonalities from A and B.
  • Multiple Inheritances vs. Intersection of Multiple Sets
    • DAG (Directed Acyclic Graph) Inheritance from [2]
    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
  • Multiple Inheritances vs. Intersection of Multiple Sets
      • template <class U, class V> class A : public U, public V { … };
      • It is a general representation of the intersection among multiple arbitrary sets.
      • U or V represents a set of the arbitrary classes.
      • 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.
    • Template Class
  • Modulization vs.Union of Sets
    • 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.
      • For example, namespace in C++.
  • Single Inheritance from an Abstract Class
    • Single Inheritance of a Class
      • Here is an example from [1].
    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… };
    • We create the concrete classes having infinite instances from an abstract class or an empty set.
    • 无 , 名天地之始;有 , 名万物之母。 故常无欲,以观其妙;常有欲,以观其徼。此两者,同出而异名,同谓之玄。玄之又玄,众妙之门。
      • ( 老子道德经:第一章 )
      • Heaven and Earth began from the nameless (Tao), but the multitudes of things around us were created by names.
      • 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.
      • 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)
  • Single Inheritance from an Abstract Class
    • Template Class
      • Here is an example from [1].
    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
    • Stack is the union of all the instance of this template: Stack_char,Stack_pdog, and Stack_li and the other ones.
    • 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.
    • With the template class, we do not need to write the duplicate codes for different kinds of types managed by stacks.
  • Summary
    • This article adds nothing new to C++ and the set theory, but builds a concept bridge between them.
    • Here are the benefits from reading this article.
      • 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.
      • 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.
      • 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.
      • It can speed up the learning curve for the C++ novices, and unfolds a new scenario and thinking way to the advanced C++ programmers.
  • References
    • [1] Bjarne Stroustrup, 2000, The C++ Programming Language, special edition, Addison-Wesley, Reading, Mass.
    • [2] Bjarne Stroustrup, 1994, The Design and Evolution of C++, Addison-Wesley, Reading, Mass.
    • [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.
    • [4] William Chen, 2008, Discrete Mathematics, Available at http://www.maths.mq.edu.au/~wchen/lndmfolder/lndm.html
  • Thank You!