Upload
sebastian-brewer
View
214
Download
0
Embed Size (px)
Citation preview
C++C++one of the programming one of the programming
languages that you should languages that you should knowknow
I am Piotr Klos
• some basic Books, forums threads, documentation, source code in C++
• „Design Patterns” by the „band of four”
• „Modern C++ Design” by Andrei Alexandrescu
• A stupid game called „Kartofel”
• Object Oriented Graphical User Interface „PKgui”
• A labyrinth editor, as a part of labyrinth generator
I have read and I recommend: I have written:
This presentation is about…
• the theory behind C++
• the advantages and disadvantages of C++
• the significance of C++ in the modern programming
What is C++?
• a compiled programming language for general purpose
• object oriented language • based on C => supports procedural
programming• highly developed, standarised, language; its
quality still increases• safe <= strict static type checking• enables generic programming <= templates
History of C++
• Bjarne Stroustrup
• first, poor version in 1979 „C with classes”
• ISO standard of C++ from 1998
• growing popularity
• last standard in 2003
• new version, C++ 0x is being developed
Object oriented?
• classes • inheritance• polymorphism
main ideas of the object oriented paradigm:
Object-based languages (like Visual Basic or JavaScript) do not have at least one of them.
Object oriented?• classes – user defined types of objects with full
rights• correct example (red keywords):
class ClassName { private: OtherClassName b; public: int a; ClassName* c; void f() { cout<<a; } ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName;};
Object oriented?• have data
class ClassName { private: OtherClassName b; /// public: int a; /// ClassName* c; /// void f() { cout<<a; } ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName;};
Object oriented?• data of in-build types
class ClassName { private: OtherClassName b; public: int a; /// ClassName* c; void f() { cout<<a; } ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName;};
Object oriented?• data of user defined types
class ClassName { private: OtherClassName b; /// public: int a; ClassName* c; void f() { cout<<a; } ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName;};
Object oriented?• pointers, references, arrays, pointers to
pointers, references to arrays of pointers, etc.
class ClassName { private: OtherClassName b; public: int a; ClassName* c; /// void f() { cout<<a; } ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName;};
Object oriented?• behaviour of objects : member functions,
which process object data easily or do whatever you need
class ClassName { private: OtherClassName b; public: int a; ClassName* c; void f() { /// cout<<a; /// } /// ClassName(){/*a body*/ } /// ClassName(int arg){ a=arg; } /// ClassName(ClassName& ){/*a body*/ } /// ~ClassName(){/*a body*/ } /// ClassName operator+(ClassName & arg){ /// return ClassName(a+ arg.a); /// } /// friend class OtherClassName;};
Object oriented?• an ordinary function(prints the value of a)
class ClassName { private: OtherClassName b; public: int a; ClassName* c; void f() { /// cout<<a; /// } /// ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName;};
Object oriented?• constructors and copy constructors
class ClassName { private: OtherClassName b; public: int a; ClassName* c; void f() { cout<<a; } ClassName(){/*a body*/ } /// ClassName(int arg){ a=arg; } /// ClassName(ClassName& ){/*a body*/ } /// ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName;};
Object oriented?• destructors
class ClassName { private: OtherClassName b; public: int a; ClassName* c; void f() { cout<<a; } ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } /// ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName;};
Object oriented?• overloaded operators
• ClassName x(3),y(4); x=x+y; /*x.a==7*/
class ClassName { private: OtherClassName b; public: int a; ClassName* c; void f() { cout<<a; } ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ /// return ClassName(a+ arg.a); /// } /// friend class OtherClassName;};
Object oriented?• member protection
• avoiding bugs and useless file dependencies
class ClassName { private: /// OtherClassName b; public: /// int a; ClassName* c; void f() { cout<<a; } ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName;};
Object oriented?• selective member protection
class ClassName { private: /// OtherClassName b; public: /// int a; ClassName* c; void f() { cout<<a; } ClassName(){/*a body*/ } ClassName(int arg){ a=arg; } ClassName(ClassName& ){/*a body*/ } ~ClassName(){/*a body*/ } ClassName operator+(ClassName & arg){ return ClassName(a+ arg.a); } friend class OtherClassName; ///};
Object oriented?• inheritance
• NextClassName contains members of ClassName
class NextClassName :public ClassName ///{ int d; float z;};
Object oriented?
• exception handling• constancy of objects• inlining• easy to use dynamic memory allocation• namespaces• operator overloading• encapsulation• run-time type identification
Other OO-friendly idioms in C++ standard:
Object oriented?
• to reflect the real world
• to reflect any beeings that exist only in our imagination
• to reflect any technical beeings in computer architecture
OO, uniform, easy to use, in-build tools are used:
OO paradigm is very powerful !
Examples of C++ code:
• literally everything at Microsoft• software by Adobe Systems• Google search engine• Apple OS X• Havoc physics engine• Metrowerks tools• Mozilla Firefox• most of big games
Is C++ Perfect?
• does not support multi-threading
• does not have delegates
• does not reflect some of design patterns – (more abstract beeings that cannot be defined as objects)
most of C++ programmers complain, because C++:
Is C++ Perfect?
• many C++ libraries have it
• C++ 0x will fix it
• so far no uniform multi-threaded support
multi-threaded support:
Is C++ Perfect?
• many C++ libraries implement them
• C++ 0x may fix it
• no uniform delegates
delegates:
Is C++ Perfect?
• design pattern – named and formally described solution for a complicated problem
support for design patterns:
What are design patterns?
Is C++ Perfect?
• you can easily implement some of them using objects
• also some C++ libraries implement some of them
• those solutions are reusable
• but not very elegant and easy to use
• it will not change
support for design patterns:
Is C++ Perfect?
• problems with dependencies between behaviours of objects
• theese cannot be defined only in terms of objects
• difference between set of notions used in project and structures written in code
• code does not reflect context of problem• OO paradigm does not reflect worlds !!!
support for design patterns:
C++ is not perfect.
• we cannot easily translate some projects to a code with C++
• because C++ is an OO language
• solution for this: aspect-oriented languages*
*AO paradigm – introduced recently by Gregor Kiczales; there are already languages like AspectJ or AspectC++ which use it; visit http://www.cs.ubc.ca/~gregor/
Why C++ is so popular?
• you can compile C code in C++
• so you can use old C libraries in C++
• so C++ supports linear, procedural, object-based programming, object oriented programming and data abstraction
• compiled C++ code is very efficient
• probably the most general purpose language in the world
general purpose…
• so there is a big community
• so there are very many C++ libraries!
• so there is a competition
• so quality of code is very high
• so the quality of C++ standard is growing to meet the requirements
• other important reason…
generic programming in C++
• templates – way of programming abstractions
• compiler generates similar parts of code instead of a programmer
• big C++ community produces a lot of template libraries
• examples: STL, Boost, Loki, others• ready solutions for complicated problems• fast, easy, safe and efficient programming
conclusion
• C++ is powerful
• C++ and further versions will be used for a long time
• C++ influenced Java, C#, PHP, ADA95 etc.
• probably will influence others in the future
• it is worth to know