the set theory in c++

31
Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net 1 the Set Theory in C++ Andrew Zhuang [email protected] Aug.7, 2008

Upload: vin

Post on 17-Jan-2016

25 views

Category:

Documents


3 download

DESCRIPTION

the Set Theory in C++. Andrew Zhuang [email protected] 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: the Set Theory in C++

Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net 1

the Set Theory in C++

Andrew [email protected]

Aug.7, 2008

Page 2: the Set Theory in C++

2Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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

Page 3: the Set Theory in C++

3Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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)}.

Page 4: the Set Theory in C++

4Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

Page 5: the Set Theory in C++

5Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

Page 6: the Set Theory in C++

6Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

Commonality of a Set vs. Public Members of a Class

Access Control [1]

A member of a class can be private, protected, or public:

a) If it is private, its name can be used only by member functions and friends of the class in which it is declared.

b) 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.

c) 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.

Page 7: the Set Theory in C++

7Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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:

a) 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 *.

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 *.

c) 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 *.

Page 8: the Set Theory in C++

8Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

Commonality of a Set vs. Public Members of a Class

The Visible Level of Member Functions in a Class to Other Guys than Himself

a) Low: private members can only been seen by his friends.

b) Medium: protected members can only been seen by his friends, his derived classes, and the friends of his derived classes.

c) 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.

a) 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.

b) 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.

c) When B is a public base, both the visible levels of the public and protected members are maintained as before.

Page 9: the Set Theory in C++

9Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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();};

Page 10: the Set Theory in C++

10Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

Commonality of a Set vs. Public Members of a Class

B is private baseThe 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.

Page 11: the Set Theory in C++

11Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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();};

Page 12: the Set Theory in C++

12Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

Page 13: the Set Theory in C++

13Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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();};

Page 14: the Set Theory in C++

14Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

Page 15: the Set Theory in C++

15Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

Page 16: the Set Theory in C++

16Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

Page 17: the Set Theory in C++

17Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

Page 18: the Set Theory in C++

18Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

Page 19: the Set Theory in C++

19Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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;}

Page 20: the Set Theory in C++

20Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

the Partition of a Set vs. Single Inheritance of a Class

21,11,01 AaAaAa

22,12,02 AaAaAa

20,10,00 AaAaAa

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.

0a

A0

1a

A1

2a

A2

0120

AAAA

Page 21: the Set Theory in C++

21Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

the Partition of a Set vs. Single Inheritance of a Class

A_1 A_2

A_0

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();

}; 2_1_0_0_

AAAA

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).

4_3_2_1_0_ AAAAA For example, N = 2,

Page 22: the Set Theory in C++

22Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

A_0 = A_1 = A_2

Page 23: the Set Theory in C++

23Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

Multiple Inheritances vs. Intersection of Multiple Sets

CA B0c

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.

BAC

CcBcAc

BA

0,0,0

,

Page 24: the Set Theory in C++

24Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

Multiple Inheritances vs. Intersection of Multiple Sets

window

window_with_menu window_with_border

window_with_menu_and_border

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

DB C

A

DCB

ACAB

A

,

Page 25: the Set Theory in C++

25Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

Multiple Inheritances vs. Intersection of Multiple Sets

VUAVU

,

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.

AU V

0a

Template Class

Page 26: the Set Theory in C++

26Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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++.

Page 27: the Set Theory in C++

27Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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 "Gate of the Great Truth". (Lao Tzu - Tao Te Ching: Chapter 1)

Page 28: the Set Theory in C++

28Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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_pdog

Stack_char Stack_li

Stack_x Stack_y

Stack

yStackxStackliStackpdogStackcharStackStack

yStackxStack

__...___

__

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.

Page 29: the Set Theory in C++

29Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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.

Page 30: the Set Theory in C++

30Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net

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

Page 31: the Set Theory in C++

Copyright © 2004 Juniper Networks, Inc. Proprietary and Confidential www.juniper.net 31

Thank You!