C++ Programming Lecture 6 Software Engineering Group

Similar documents
C++ Programming Lecture 4 Software Engineering Group

C++ Programming Lecture 7 Software Engineering Group

C++ Programming Lecture 7 Software Engineering Group

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

CS

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CSE 303: Concepts and Tools for Software Development

CE221 Programming in C++ Part 1 Introduction

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

C++ Programming Lecture 1 Software Engineering Group

Introducing C++ to Java Programmers

QUIZ. What are 3 differences between C and C++ const variables?

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Function Overloading

Topics. bool and string types input/output library functions comments memory allocation templates classes

6.096 Introduction to C++ January (IAP) 2009

Interview Questions of C++

AN OVERVIEW OF C++ 1

CS3157: Advanced Programming. Outline

Overloading Operators in C++

III. Classes (Chap. 3)

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Topic 6: A Quick Intro To C

Short Notes of CS201

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

PIC 10A Objects/Classes

Have examined process Creating program Have developed program Written in C Source code

C The new standard

Fast Introduction to Object Oriented Programming and C++

CS201 - Introduction to Programming Glossary By

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

A brief introduction to C++

6.S096 Lecture 4 Style and Structure

Motivation was to facilitate development of systems software, especially OS development.

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

CSCI 123 Introduction to Programming Concepts in C++

TEMPLATES AND EXCEPTION HANDLING

Lecture 7. Log into Linux New documents posted to course webpage

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Operator overloading

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

Lab 2: ADT Design & Implementation

COMP322 - Introduction to C++ Lecture 01 - Introduction

UEE1302 (1102) F10: Introduction to Computers and Programming

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

Structured bindings with polymorphic lambas

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

C++_ MARKS 40 MIN

Programming Abstractions

Basic Templates Intro

C++ Programming Lecture 5 Software Engineering Group

Object Oriented Software Design II

Templates. Zoltán Porkoláb: C++11/14 1

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Due Date: See Blackboard

Reliable C++ development - session 1: From C to C++ (and some C++ features)

QUIZ. What is wrong with this code that uses default arguments?

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

C++ for numerical computing - part 2

Come and join us at WebLyceum

Templates and Vectors

G52CPP C++ Programming Lecture 9

y

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Lambda functions. Zoltán Porkoláb: C++11/14 1

Problem Solving with C++

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

PIC 10A. Lecture 17: Classes III, overloading

Elevate your Code to Modern C++ with Automated Tooling. Peter Sommerlad

Motivation was to facilitate development of systems software, especially OS development.

Friendship in Service of Testing

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

EL2310 Scientific Programming

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Absolute C++ Walter Savitch

The C Preprocessor (and more)!

Object-Oriented Programming in C++

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Intermediate Programming, Spring 2017*

Implementing an ADT with a Class

September 10,

The Automated Analysis of Header Files for Support of the Standardization Process

EECE.3220: Data Structures Spring 2017

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

G52CPP C++ Programming Lecture 20

And Even More and More C++ Fundamentals of Computer Science

boost::enable_if Deep Down Boostimagical Fun Christian Bay and Kasper A Andersen Department of Computer Science University of Copenhagen

Bruce Merry. IOI Training Dec 2013

Functions. Ali Malik

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Outline. 1 About the course

Ch. 12: Operator Overloading

eingebetteter Systeme

Midterm Review. PIC 10B Spring 2018

OBJECT ORIENTED PROGRAMMING USING C++

Transcription:

C++ Programming Lecture 6 Software Engineering Group Philipp D. Schubert

Dynamic memory allocation Quick reminder: Do not confuse between the (de)allocation operators! void* operator new ( std::size_t count ); void operator delete ( void* ptr ); // for objects // for objects void* operator new[]( std::size_t count ); void operator delete[]( void* ptr ); // for arrays // for arrays

Contents 1. C/C++ preprocessor 2. Templates 3. Variadic function arguments 4. Functors 5. Lambda functions

The C/C++ preprocessor: CPP Remember the # directives Preprocessor directives are executed by the preprocessor CPP is just a text processing program Pre because it processes the program text before the compiler gets program text Great power Great opportunity for really subtle bugs Do not overuse/ misuse CPP Only use in rare cases C++ has better mechanisms to replace it In modern C++, CPP rarely has to be used anymore Except for organizing files into header & implementation file [Figure taken from A Tour of C++, Stroustrup 2013]

The C/C++ preprocessor Remember the language processing system In C++ we use module-wise compilation of implementation files (.cpp) Header files (.h) are not compiled at all Header files are only included in implementation files Each implementation file becomes preprocessed an then compiled CPP can be viewed as a second language performs simple text processing/ replacement Problem: a programmer has to deal with 2 languages! Template metaprogramming is a 3th language (inside C++) Template metaprogramming is even Turing complete TMP next time [Figures taken from A Tour of C++, Stroustrup 2013]

The C/C++ preprocessor Why use such a complicated system for modularity? Header files are included in every implementation files that needs parts of it! Compile same parts over and over again E.g. iostream header! But why? Compatibility C had this system, so C++ adopted it A real module-system is planned for the future (like in Java) [Figure A Tour of C++, Stroustrup 2013]

CPP directives Important CPP directives #include #define #if #elif #endif #else #ifdef #ifndef ## // a dark corner of CPP Variadic macros // another dark corner of CPP [Figure taken from https://c1.staticflickr.com/3/2117/1792490622_5de0a1d609_b.jpg]

CPP directives #include header files Example #include <iostream> using namespace std; int main() { cout << "Hello!\n"; #define symbols Example #include <iostream> #define VALUE 10 using namespace std; int main() { cout << VALUE << \n ;

CPP directives #ifdef example #include <iostream> // this is one way to define a symbol #define LINUX using namespace std; int main() { #ifdef LINUX cout << "is linux\n"; #elif WINDOWS cout << "is windows\n"; #elif MAC cout << "is mac\n"; #else cout << "is something else\n"; #endif Another way to define a symbol Tell the compiler directly g++ -D MAC test.cpp o test Program produces: "is mac" g++ test.cpp o test Program produces: "is linux" Use option U to undefined a symbol Remember preprocessing-only, can be done by calling the cpp program Use option E or P (or both)

CPP directives #if example #include <iostream> #define VALUE 10 using namespace std; int main() { #if VALUE == 10 cout << "VALUE is 10\n"; #else cout << "VALUE is not 10\n"; #endif

CPP: Software product lines Using #if, #ifdef, #elif, #else, #endif one can build software product lines maintain huge software product lines within same codebase Linux kernel has many #if directives Can be compiled for many platforms Almost all big software projects use them to establish SPL s Configuration for CPP symbols are usually maintained in a huge (documented) table Symbols are looked up for product needed Car system software for VW Passat Use corresponding CPP symbols for Passat and compile If software for another model is needed Use the appropriate CPP symbols

[Slide from Designing Code Analysis, Eric Bodden]

[Slide from Designing Code Analysis, Eric Bodden]

Finding bugs & testing SPL s It is like hell! It is just a program? No! Every used #if or #ifdef Leads to 2 different programs One program that contains the code inside #if One program that does not contain the code inside #if There are 2 n different versions of your program n is the number of preprocessor #if used What does that mean?

[Slide from Designing Code Analysis, Eric Bodden]

[Slide from Designing Code Analysis, Eric Bodden]

Writing CPP macros Writing a macro #include <iostream> #define FACTORIAL(X,R) { \ R = 1; \ while (X > 0) { \ R *= X; \ --X; \ \ using namespace std; int main() { int i = 5; int result; FACTORIAL(i, result); cout << result << \n ; Do not abuse macros for function in-lining! This leads to subtle bugs Compiler only sees the expanded macro Type information is missing Use C++ keyword inline instead Macro expands to: using namespace std; int main() { int i = 5; int result; { result = 1; while (i > 0) { result *= i; --i; ; cout << result << \n ;

Macros gone wrong Very subtle bugs #include <iostream> using namespace std; #define ABS(X) ((X) < 0)? (X) : X; int main() { int i = ABS(-42); int j = ABS(42); int t = 10; int k = ABS(--t); cout << i << \n ; cout << j << \n ; cout << k << \n ; What will be printed? using namespace std; int main() { int i = (-42 < 0)? - -42 : -42;; int j = (42 < 0)? -42 : 42;; int t = 10; int k = (--t < 0)? - --t : --t;; cout << i << \n ; cout << j << \n ; cout << k << \n ; Output: 42, 42, 8

Predefined CPP macros Predefined macros #include <iostream> using namespace std; int main() { cout << "File : " << FILE << \n ; cout << "Date : " << DATE << \n ; cout << "Time : " << TIME << \n ; cout << "Line : " << LINE << \n ; cout << "ANSI : " << STDC << \n ; Output File : cpp.cpp Date : Nov 25 2016 Time : 09:09:16 Line : 8 ANSI : 1 using namespace std; int main() { UNRECERROR(1, "uups"); #include <iostream> #include <cstdlib> #define UNRECERROR(BOOL_EXPR, MESSAGE) { \ if (BOOL_EXPR) { \ cout << "Detected unrecoverable error: " << MESSAGE << \n ; \ cout << "File : " << FILE << \n ; \ cout << "Abort program!" << \n ; \ exit(1); \ ; \

Notes on the preprocessor Include mechanism is fine (essential even) #ifndef MODULE_H_ #define MODULE_H_ // some code #endif #include Try to avoid using CPP s #define #if as much as possible

Templates Template allows further abstraction Templates are handled by the compiler, not the CPP A template is a C++ entity that defines one of the following: 1. a family of classes (class template), which may be nested classes 2. a family of functions (function template), which may be member functions 3. an alias to a family of types (alias template) 4. a family of variables (variable template) Templates are parameterized by one or more template parameters of three kinds: 1. type template parameters 2. non-type template parameters 3. template template parameters [http://en.cppreference.com/w/cpp/language/templates] If template parameters are specified (template instantiation) one obtains a template specialization

Templates Templates are the C++ way to do things that the CPP does Caution template code cannot be compiled up-front / until template instantiation Put template code into header files Include them where you need the template code Always prefer templates over some macro directives Use templates if you do not know information up-front if a class/ function/ variable can be used more general (like sorting in the exercises) Can be abused to compute values at compile time: template meta-programming (next lecture) Since C++14 prefer constexpr functions But: Not everything can be expressed with constexpr functions

Class template & function template #include <iostream> #include <type_traits> using namespace std; template<typename A, typename B> struct Tuple { A first; B second; Tuple(A a, B b) : first(a), second(b) { ; template<typename T> T add(t a, T b) { static_assert(is_arithmetic<t>::value, "wrong"); return a + b; int main() { Tuple<int, string> t(5, "Hello!"); cout << t.first << \n ; cout << t.second << \n ; int result_int = add<int>(5, 5); double result_double =add<double>(1.11, 2.22); cout << result_int << \n ; cout << result_double << \n ;

Variable template Note: Only integer types are allowed for variable templates #include <iostream> #include <algorithm> #include <cassert> using namespace std; template<typename T, size_t S> class MyArray { private: static constexpr size_t elements = S; T data[s]; public: MyArray() { MyArray(T ival) { fill(&data[0], &data[elements], ival); size_t size() const { return elements; T& operator[] (size_t idx) { assert(idx < elements); return data[idx]; const T& operator[] (size_t idx) const { assert(idx < elements); return data[idx]; friend ostream& operator<< (ostream& os, const MyArray& a) { for (size_t i = 0; i < a.elements; ++i) os << a.data[i] << " "; return os; ; int main() { MyArray<int, 10> a; MyArray<double, 5> b(1.11); cout << a << \n ; cout << b << \n ;

C++ s oddities template<typename T> class MyClass { ; //... versus template<class T> class MyOtherClass { ; //... Summary: Stroustrup originally used class to specify types in templates to avoid introducing a new keyword. Some in the committee worried that this overloading of the keyword led to confusion. Later, the committee introduced a new keyword typename to resolve syntactic ambiguity, and decided to let it also be used to specify template types to reduce confusion, but for backward compatibility, class kept its overloaded meaning. [http://stackoverflow.com/questions/213121/use-class-or-typename-for-template-parameters]

Variadic function arguments More flexibility on function parameters How can we pass arbitrary many parameters into a function? void print_ints(int i) { cout << i << \n ; void print_ints(int i, int j) { cout << i << " " << j << \n ; Solution Use initializer_list (as you did for certain ctors) void print_ints(initializer_list<int> varargs); The user can pass arbitrary many integers to print_ints() now But caution You have to pass arguments using the initializer_list braces {

Variadic function arguments #include <iostream> #include <initializer_list> using namespace std; void print_ints( initializer_list<int> args) { for (int i : args) { cout << i << " "; template<typename T> void print_generic( initializer_list<t> args) { for (const T &arg : args) { cout << arg << " "; int main() { print_ints({1, 2, 3, 4); print_ints({1, 2); print_generic<double>({1.111, 2.222, 3.333); Use initializer_list for variable argument list Cleanest way to achieve flexibility Another way is possible va_arg Hard to read Hard to understand NO! Next time: variadic arguments of different types!

Function object or functor A functor is a class/ struct that implements operator() The call operator A variable of that type can be called like a function Basically a function that can store variables & data It has state You can wrap tasks using a functor Fits perfectly into object oriented programming (later on)

Functor Example function object #include <iostream> using namespace std; class Task { private: int i; int j; // perform extensive task of adding // two numbers int do_hard_work() { return i + j; public: Task(int i, int j) : i(i), j(j) { int operator() () { return do_hard_work(); ; int main() { Task t(100, 200); int result = t(); cout << result << \n ;

Lambda functions A lambda function is an unnamed function object capable of capturing variables in scope [http://en.cppreference.com/w/cpp/language/lambda] Syntax 1. [ capture-list ] ( params ) -> ret { body 2. [ capture-list ] ( params ) { body 3. [ capture-list ] { body What is a capture-list? A comma-separated list of zero or more captures [a,&b] where a is captured by value and b is captured by reference. [this] captures the this pointer by value [&] captures all automatic variables odr-used in the body of the lambda by reference [=] captures all automatic variables odr-used in the body of the lambda by value [] captures nothing

Lambda functions #include <iostream> #include <functional> using namespace std; int bin_operation(int a, int b, function<int(int,int)> f) { return f(a, b); int main() { int result = bin_operation(2, 3, [](int a, int b) { return a + b; ); int other = bin_operation(2, 3, [](int a, int b) { return a * b; ); function<int(int)> f_sq = [](int a) { return a * a; ; // or use auto to be shorter auto f_sub = [](int a, int b) { return a - b; ; cout << f_sq(10) << \n ; cout << f_sub(10, 5) << \n ; cout << result << \n ; cout << other << \n ;

Lambda functions Example using for_each() algorithm (which is a fully generic function) #include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5, 6; for_each(v.begin(), v.end(), [](int &i) -> void { i *= i; ); for_each(v.begin(), v.end(), [](int i) { cout << i << " "; ); cout << \n ;

Lambda functions Now we can introduce predicates Example binary predicate #include <iostream> #include <vector> #include <functional> using namespace std; bool check_pred(vector<int>& v1, vector<int>& v2, function<bool(int,int)> pred){ for (size_t i = 0; i < v1.size(); ++i) if (!pred(v1[i],v2[2])) return false; return true; int main() { vector<int> v1(5, 1); vector<int> v2 = {1, 1, 1, 1, 1; bool equals = check_pred(v1, v2, [](int a, int b) { return a == b; ); cout << equals << \n ;

Lambda functions #include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; void trafo(vector<int>& from, vector<int>& to, const int epsilon) { transform(from.begin(), from.end(), to.begin(), [epsilon](int d) -> int { if (d <= epsilon) { else { return d; ); int main() { vector<int> from(10); vector<int> to(10); // fills with 1, 2,..., 10 iota(from.begin(), from.end(), 1); for_each(from.begin(), from.end(), [](int i) { cout << i << " "; ); cout << \n ; trafo(from, to, 5); for_each(to.begin(), to.end(), [](int i) { cout << i << " "; ); cout << \n ;

Recap C/C++ preprocessor Modularity in C++ Software Product Lines (SPL) Exponential #if variability Macros Macros gone wrong Predefined macros Templates Variadic function arguments Functor: function objects Lambda functions

Thank you for your attention Questions?