160 likes | 212 Views
What’s new in C++?. V1.1 dan@dmicsa.com , Dan Micsa, 2008. What is C++?.
E N D
What’s new in C++? V1.1 dan@dmicsa.com, Dan Micsa, 2008
What is C++? C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup in 1979 at Bell Labs as an enhancement to the C programming language and originally named "C with Classes". It was renamed to C++ in 1983.
Why was C++ need it? C was designed as a simple low level language used to port Unix to various platforms suitable for small to medium size applications. C++ was designed as an extension of C language intended to be useful in developing larger scale programs. It improves in many areas where C language was lacking: • Safer code. • Easier to develop. • Easier to understand. • Easier to maintain.
Where is improving C++ over C? Some of the areas where C++ improves over C are: • Safe, generic, dynamic and efficient Strings, Streams, Containers, Algorithms, Adaptors, Functors, Binders. • Templates and generic programming instead of macros. • Object Oriented Programming. • References instead of pointers. • Namespaces. • Exceptions. • Overloading and Operator overloading to better simulate natural type behavior.
What are C++ drawbacks? • C++ as whole is a very complex language. Compilers required more than 10 years of evolution until they have started to compile (somehow) correct C++. • C++ is a dynamic language. It still evolves at serious rate and requires a lot of learning. • Looking like C many developers coming from C world tries to use coding techniques and guidelines common in C without realizing that C++ makes obsolete near everything that was considered common practice in C. Because of this during the transition period the code start to look like an amalgam of C and C++ making it difficult to understand for both pure C and C++ programmers.
It is worth it to bother with it? Oh YES! After I used for over 15 years pure procedural languages like Fortran, C, Pascal, Basic before jumping to C++ I can say C++ is by far the most powerful language I ever used. Correct C++ can be much easier and faster to develop, understand and maintain when compared with other approaches.
Is C++ an OOP language? C++ is a general purpose language not a specialized Object Oriented one like Java, C# or Smalltalk nor a procedural one like Pascal, C. It was designed to be as efficient as C if is required and this enforces it to don’t use OOP techniques as default. C++ is a language alone in its class being possible to program like: • Generic languages(like C macros); • Procedural languages (like C, Pascal, Fortran); • Object Oriented languages (like C#, Java, Smalltalk);
Standard input- and output streams • In analogy to C, C++ defines standard input- and output streams which are opened when a program is executed. The streams are: • cout, analogous to stdout, • cin, analogous to stdin, • cerr, analogous to stderr.
References Besides the normal declaration of variables, C++ allows ‘references’ to be declared as synonyms for variables. A reference to a variable is like an alias; the variable name and the reference name can both be used in statements which affect the variable: { Integer integer(0); Integer& reference = integer; reference = 1; // now integer = 1 }
Overloading Same function name can be overloaded to create uniform behavior. enum Axes{X, Y, Z, U = 0, V = 1}; const Point operator*(const Point& p1, const Point& p2)throw() {return Point(p1[X] * p2[X], p1[Y] * p2[Y], p1[Z] * p2[Z]);} const Point operator*(const Point& p, Real k)throw() {return Point(p[X] * k, p[Y] * k, p[Z] * k);}//operator* overloading Void Example() { std::cout << Point(1, 2, 3) * Point(1, 2, 3);//Writes: Point{1, 4, 9} std::cout << Point(1, 2, 3) * 5; //Writes: Point{5, 10, 15} }
Default parameters Default parameters offers usage convenience. class Point { Real d[3]; public: explicit Point(Real x = 0, Real y = 0, Real z = 0) {d[0] = x; d[1] = y; d[2] = z;} }; Void Example() { Point p1; // p1 = {0, 0, 0} Point p2(1); // p2 = {1, 0, 0} Point p3(1, 2, 3); // p3 = {1, 2, 3} • Point p4(p3); // p4 = {1, 2, 3} uses the automatic CopyConstructor generated for you }
The keyword const The keyword const very often occurs in C++ programs, even though it is also part of the C grammar, where it's much less used. enum Axes{X, Y, Z, U = 0, V = 1}; const Point operator+(const Point& p1, const Point& p2)throw(){return Point(p1[X]+p2[X], p1[Y]+p2[Y], p1[Z]+p2[Z]);} const Point operator-(const Point& p1, const Point& p2)throw(){return Point(p1[X]-p2[X], p1[Y]-p2[Y], p1[Z]-p2[Z]);} const Point operator*(const Point& p1, const Point& p2)throw(){return Point(p1[X]*p2[X], p1[Y]*p2[Y], p1[Z]*p2[Z]);} const Point operator/(const Point& p1, const Point& p2)throw(){return Point(p1[X]/p2[X], p1[Y]/p2[Y], p1[Z]/p2[Z]);} const Point operator*(const Point& p, Real k)throw(){return Point(p[X]*k, p[Y]*k, p[Z]*k);}//operator* overloading const Point Scale(const Point& p, Real k) throw(){p * k;}//Scales in origin const Point Scale(const Point& p, const Point& origin, Real k) throw(){return (p – origin) * k + origin;} Void Example() { const Point p1(1, 2, 3), p2(p1); const Point p = p1 + p2; std::cout << Scale(p, p1, 5); std::cout << (p – p1) * 5 + p1; //same thing p[X] = 1.1;//ERROR p is constant! }
Templates • Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. • //some template functions • template <typename T> Max(const T& t1, const T& t2) {return t1 > t2 ? t1 : t2;} • template <typename T> Min(const T& t1, const T& t2) {return t1 < t2 ? t1 : t2;} • template <typename T> Swap(T& t1, T& t2) {T t(t1); t1 = t2; t2 = t;} • template <typename Numeric> class Point //a template class • { • Numeric data [3]; • public: • explicit Point(const Numeric& x = Numeric(), const Numeric& y = Numeric(), const Numeric& z = Numeric()) • {data[0] = x; data[1] = y; data[2] = z;} • template <typename Numeric2> Void From(const Numeric2&){…} //template generic converter • Numeric& operator[](Integer ix){return data[ix];}//mutable flavor • const Numeric& operator[](Integer ix)const {return data[ix];}//constant flavor • };
Standard Library In C++, the Standard Library (SL) is a collection of classes and functions, which are written in the core language. The Standard Library provides several generic containers, functions to utilize and manipulate these containers, function objects, generic strings and streams (including interactive and file I/O), support for some language features, and every day functions for tasks such as finding the square root of a number. The C++ Standard Library also incorporates the ISO C90 C Standard Library. Features of the Standard Library are declared within the std namespace. The Standard Template Library (STL) is a subset of the C++ standard library, and contains the containers, algorithms, iterators, function objects, etc.; although some people use the term STL interchangeably with the C++ standard library.
Object Oriented Programming template <typename Container> DeallocatorWrapper public: Container//used to safely free dynamic objects in a polymorphic container {public: ~DeallocatorWrapper(){for(auto it(begin()); it != end(); ++it) delete *it;}}; class Object{ virtual ~Object()throw(){} //remember that we have to have the virtual destructor in the root object to proper deallocate memory virtual ToString() const throw() =0; //must allow conversion to a sting for debugging purposes! }; class Shape public: Object{public: virtual Void Draw()const = 0; };//All the Shapes can be Draw() class Point public: Shape{…};//implements at least required: Object::ToString() and Shape::Draw() class Line public: Shape{…}; //implements at least required: Object::ToString() and Shape::Draw() class Triangle public: Shape{…}; //implements at least required: Object::ToString() and Shape::Draw() Void Example() { DeallocatorWrapper<std::deque<Shape*>> shapes = {new Point(1, 2, 3), new Line(), new Triangle(Point(2, 3, 4))}; std::cout << shapes; } Writes something like: DeallocatorWrapper<deque> { Point{1, 2, 3}, Line{Point{0, 0, 0}, Point{0, 0, 0}}, Triangle{Point{2, 3, 4}, Point{0, 0, 0} , Point{0, 0, 0}} }