History C++11 : First ContactE. Conteslide 2 C++ 981998 First international standard of the C++ language. Published as ISO/IE C 14882:1998 C++ 032003.

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Operator Overloading and Type Conversions
1 Lecture 3 Part 1 Functions with math and randomness.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
3. Controlling Program Flow Methods, parameters, and return values Boolean expressions Conditional branching Loops.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Generic Programming and Library Design Brian Bartman
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
Chapter 15 - C++ As A "Better C"
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
FUNCTIONS In C++.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointer Basics Psst… over there.
Arrays Kingdom of Saudi Arabia
A First Book of ANSI C Fourth Edition
Variables have attributes
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Arrays Arrays A few types Structures of related data items
Pointer Basics Psst… over there.
Class: Special Topics Overloading (methods) Copy Constructors
C Language B. DHIVYA 17PCA140 II MCA.
4.1 Introduction Arrays A few types Structures of related data items
INTRODUCTION TO C.
Presentation transcript:

History C++11 : First ContactE. Conteslide 2 C First international standard of the C++ language. Published as ISO/IE C 14882:1998 C New standard replacing C Published as ISO/IEC 14882:2003. C++ Technical Report 1 (TR1)2007 Addition to the STL : regular expressions, smart pointers, hash tables, random number generator. Published as ISO/IEC TR 19768:2007. C New standard. Published as ISO/IEC 14882:2011 C ??? C ???

Changes into the C++ language C++11 : First ContactE. Conteslide 3

Strongly type enumeration C++11 : First ContactE. Conteslide 4 enum ParticleType {LEPTON,PARTON,VECTOR,HIGGS}; ParticleType part = LEPTON; std::cout << part << std::endl; Before After enum class ParticleType {LEPTON,PARTON,VECTOR,HIGGS}; ParticleType part = ParticleType::LEPTON; std::cout (part) << std::endl;

Right angle bracket C++11 : First ContactE. Conteslide 5 std::vector > toto; Before Do not compile ! How to define a vector of vector ?

Right angle bracket C++11 : First ContactE. Conteslide 6 How to define a vector of vector ? std::vector > toto; Before Do not compile ! std::vector > toto; Only allowed : After Both syntaxes are allowed !

Null pointer C++11 : First ContactE. Conteslide 7 NULL = C macro = ((void*)0) How to define a null pointer ? C-like: int * pointer = NULL; C++98/03-like: int * pointer = 0; C++11-like: int * pointer = nullptr; backward compatibility NULL = C macro = 0 NULL = C macro = nullptr nullptr is a new language keyword.

Uniform initialization C++11 : First ContactE. Conteslide 8 int numbers[] = { 1, 2, 3 }; C-like How to define and initialize a vector ? std::vector numbers; numbers.push_back(1); numbers.push_back(2); numbers.push_back(3); C++98/03-like std::vector numbers = { 1, 2, 3 }; C++11-like

Initializer list C++11 : First ContactE. Conteslide 9 int a = 0; int b = 1; int c = 2; … std::vector numbers; numbers.push_back(a); numbers.push_back(b); numbers.push_back(c); MyFunction(numbers); C++98/03-like void MyFunction(const std::vector & numbers) { for (unsigned int i=0;i<numbers.size();i++) { … } }

Initializer list C++11 : First ContactE. Conteslide 10 int a = 0; int b = 1; int c = 2; … MyFunction( {a,b,c} ); C++11-like void MyFunction(const initializer_list & numbers) { for (unsigned int i=0;i<numbers.size();i++) { … } } Do not forget : #include

Extension of the for loop C++11 : First ContactE. Conteslide 11 Before 2 allowed syntaxes std::vector numbers = { 1, 2, 3, 4, 5 }; for (unsigned int i=0;i<numbers.size();i++) { std::cout << numbers[i] << std::endl; // defining a function MyFunctionPrint std::for_each(numbers.begin();numbers.end(); MyFunctionPrint) After std::for_each syntax by another syntax of for for (int& item : numbers) { std::cout << item << std::endl;

Keyword auto C++11 : First ContactE. Conteslide 12 Possibility to not specify the type of the variable. The type is automatically set according to the initial value. Two examples : auto number = 5; // integer auto value = 3.14; // float std::map MyMap; auto iter = MyMap.const_iterator; Possibility to define a variable with the same type of another variable with the keyword decltyp (without initializing the variable). auto number = 5; // integer decltype(number) value; // integer

New syntax for function return C++11 : First ContactE. Conteslide 13 With the auto keyword a new syntax for function is given. int multiply(x,y) { return x*y; } Common New syntax for C++11 auto multiply(x,y) -> int { return x*y; } Advantages: writing simplification in the case of a function defined in a class and the return value type is also defined int the class. MyClass::MyType MyClass::multiply(x,y) { … } auto MyClass::multiply(int x,int y) -> MyType { … }

Lambda functions C++11 : First ContactE. Conteslide 14 Writing a function directly where it is called. No need to give a name to this function. Before void print(int i) { … }; std::vector numbers; std::for_each(numbers.begin();numbers.end();Print); After std::vector numbers; std::for_each(numbers.begin();numbers.end(); [](int i) { … } ); Syntax of lambda function : [capture] (parameters) -> return-type { body }

Closure C++11 : First ContactE. Conteslide 15 Variables can be given to the function without declaring it in arguments. The variables is captured. std::vector numbers; int x=0; double y=0.; std::for_each(numbers.begin();numbers.end(); [&x,y](int i) { … } ); Special cases of capture : x and y are now known by the lambda function. x can be modified (pass by reference). y is a copy of the initial variable (pass by copy). [&] : all external variables are captured by reference [=] : all external variables are captured by value [&, x] : x is captured by value; the other variables are captured by reference

New STL classes C++11 : First ContactE. Conteslide 16

New containers C++11 : First ContactE. Conteslide 17 Tuple #include std::tuple myTuple (1, 3.14, «boson»); Extension of std::pair to n observables of different types. A small example: How to access to a variable of the ntuple : std::cout (myTuple) << std::endl;// 1 std::cout (myTuple) << std::endl;// 3.14 std::cout (myTuple) << std::endl;// boson

New containers C++11 : First ContactE. Conteslide 18 Array #include std::array myTable; Warning: the number of items must be a const value. int const size = 5; std::array myTable = { 1, 2, 3, 4, 5 }; Hash tables std::unordered_set std::unordered _multiset std::unordered_map std::unordered_multimap

Timing C++11 : First ContactE. Conteslide 19 #include auto clock1 = std::chrono::system_clock::now(); usleep(1000); auto clock2 = std::chrono::system_clock::now(); STL header Time measurement Duration calculation auto last = std::chrono::duration_cast (clock1-clock2); std::cout << last << std::endl; Display (in µs) In the previous C++ No method independent from the framework. With the ANSI methods, the clock resolution was limited to the millisecond.

Random C++11 : First ContactE. Conteslide 20 #include Before Two functions : std::rand()gives a random value between 0 and RAND_MAX std::srand(…)initialize the generator with a seed After #include std::mt19937 gen; // choosing a generator gen.seed(…); std::uniform_int_distribution uint_dist(0,10); std::normal_distribution normal_dist(mean,sigma); std::cout << uint_dist(rng) << std::endl;

Concurrency std::async C++11 : First ContactE. Conteslide 21 #include void write_message(std::string const& message) { std::cout<< message; } int main() { auto f = std::async(write_message, "hello world from std::async\n"); write_message("hello world from main\n"); f.wait(); }

Concurrency std::thread C++11 : First ContactE. Conteslide 22 #include void write_message(std::string const& message) { std::cout<< message; } int main() { auto t = std::thread(write_message, "hello world from std::thread\n"); write_message("hello world from main\n"); t.join(); }