Tutorial 7. Y. Bernat. Object Oriented Programming 2, Spring Y. Bernat Tutorial 7

Similar documents
Tutorial 6. Y. Bernat. Object Oriented Programming 2, Spring Y. Bernat Tutorial 6

C++ (Non for C Programmer) (BT307) 40 Hours

Outline. 1 About the course

CS93SI Handout 04 Spring 2006 Apr Review Answers

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

Tokens, Expressions and Control Structures

use static size for this buffer

C The new standard

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Fast Introduction to Object Oriented Programming and C++

Absolute C++ Walter Savitch

G52CPP C++ Programming Lecture 13

Making New Pseudo-Languages with C++

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

A brief introduction to C++

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

CS

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

Object-Oriented Programming

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Constructors, Destructors, and Assignment Operators. Literature: Effective C++, Third Edition Chapter 2

Interview Questions of C++

AN OVERVIEW OF C++ 1

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Lesson 13 - Vectors Dynamic Data Storage

Programming 2. Object Oriented Programming. Daniel POP

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

COEN244: Class & function templates

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Problem Solving with C++

VALLIAMMAI ENGINEERING COLLEGE

Vector and Free Store (Vectors and Arrays)

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

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

Lecture 13: more class, C++ memory management

C++11/14 Rocks. Clang Edition. Alex Korban

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Let return Be Direct and explicit

Advanced C++ Topics. Alexander Warg, 2017

C++11 and Compiler Update

7 TEMPLATES AND STL. 7.1 Function Templates

G52CPP C++ Programming Lecture 20

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Instantiation of Template class

September 10,

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

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

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

During the course of writing the Matrix class we will cover some interesting C++ topics. Specically: constructors and destructors, operator

Object Oriented Programming with c++ Question Bank

Lecture 10: building large projects, beginning C++, C++ and structs

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

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

The Compositional C++ Language. Denition. Abstract. This document gives a concise denition of the syntax and semantics

CS 376b Computer Vision

CE221 Programming in C++ Part 1 Introduction

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

CS 162, Lecture 25: Exam II Review. 30 May 2018

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

Auto - a necessary evil?

Cpt S 122 Data Structures. Introduction to C++ Part II

Midterm Review. PIC 10B Spring 2018

Chapter 15 - C++ As A "Better C"

Time : 3 hours. Full Marks : 75. Own words as far as practicable. The questions are of equal value. Answer any five questions.

Data Abstraction. Hwansoo Han

2. COURSE DESIGNATION: 3. COURSE DESCRIPTIONS:

Assignment 1: grid. Due November 20, 11:59 PM Introduction

ECE 3574: Dynamic Polymorphism using Inheritance

04-19 Discussion Notes

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

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Lecture Topics. Administrivia

Separate Compilation of Multi-File Programs

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

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

Computer Science 306 Study Guide

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

2 ADT Programming User-defined abstract data types

CS 360 Programming Languages Day 14 Closure Idioms

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Project structure - working with multiple les

Outline. Computer Science 331. Information Hiding. What This Lecture is About. Data Structures, Abstract Data Types, and Their Implementations

CS193D Handout 10 Winter 2005/2006 January 23, 2006 Pimp Your Classes

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

Structured bindings with polymorphic lambas

CMSC202 Computer Science II for Majors

C++ Important Questions with Answers

Programming 2. Object Oriented Programming. Daniel POP

San José State University Department of Computer Science CS-144, Advanced C++ Programming, Section 1, Spring 2018

Transcription:

Tutorial 7 Y. Bernat Object Oriented Programming 2, Spring 2016

Exercise 4 STL Outline Part I Today's Topics 1 Exercise 4 2 STL Containers - continue Lambda functions Algorithms

Exercise 4 STL Exercise 4 Let's read the exercise

Exercise 4 STL Containers - continue Lambda functions Algorithms Unordered associative containers std::unordered_map, std::unordered_set, std::unordered_multimap, std::unordered_multiset Performance

Exercise 4 STL Containers - continue Lambda functions Algorithms How to choose which container to use The most natural for the problem If there is a performance issue and the proler shows it's around the container access: Try converting to vector Use reserve() for better vector performance (But rst think about your algorithms and access patterns!)

Exercise 4 STL Containers - continue Lambda functions Algorithms Intro Not part of STL, part of the language (since 11, with great enhancements in 14) Needed as a tool for the next topic for STL, algorithms The problem: Sometimes it's very useful to get a function as a parameter The trivial choice is a function pointer Example code Initial solution: Functor (function object) Example code

Exercise 4 STL Containers - continue Lambda functions Algorithms Lambda functions Lambda parts: [] - Lambda function introducer (and capture list) Capture by value, by reference, this, default capturing init-capture (14) The parameter list (can be ommitted if empty; can use auto since 14) mutable The return type (trailing return type style) - usually not needed The (lambda) function body

Exercise 4 STL Containers - continue Lambda functions Algorithms Summary Lambda function is a functor that is written easily and inline All the logic is local, no need to nd the correct scope to dene the function when writing and nd the function when reading the code Don't overuse/abuse! If it's long, it's probably better as a regular function or functor

Exercise 4 STL Containers - continue Lambda functions Algorithms Overview Most of them in <algorithm>, some in <numeric>, and specic groups in <functional> or even <iterator> Implement common algorithms in a generic way Generic AKA using iterators What if we need an action to be done on the container? 3 options: 1 Specic algorithm version as a member (e.g. sort() and remove() for std::list) 2 Using iterator that knows how to do it (e.g. std::back_insert_iterator) 3 Giving us the needed info and we'll do it (e.g. std::remove())

Exercise 4 STL Containers - continue Lambda functions Algorithms Examples std::reverse() - Takes two iterators, rst and last, and replace every pair of values between rst and last-1 std::count() - Takes two iterators and a value and counts how many times the value appears in the range std::count_if() - Takes two iterators and a predicate and counts for how many values the predicate returns true std::all_of() / std::any_of() / std::none_of() - Take two iterators and a predicate and returns if the predicate holds for all/any/none of the values in the range. Note the dierences about empty range!

Exercise 4 STL Containers - continue Lambda functions Algorithms Examples 2 std::remove() - Takes two iterators and a value and removes this value from the range Returns the new end Be careful! It doesn't do what we'd think it does! It doesn't know anything about the container, so it just takes all the not-to-be-removed items to the front, optionally overwriting some to-be-removed items, and tells us where is the new end Now we can use the returned information to really delete the unneeded items Remove-erase idiom std::remove_if() - Of course...

Exercise 4 STL Containers - continue Lambda functions Algorithms Examples 3 std::copy() / std::copy_if() - Takes two input iterators and an output iterator (and predicate, in case of the _if version) We CAN'T just give it an iterator for the container. It doesn't know how to add new objects there! Two approaches: 1 Prepare the container to have enough size to overwrite with the new copied objects 2 Use an iterator that knows how to insert new objects, e.g. std::back_inserter_iterator

Outline Part II Backlog 3 4 5 6 7 Class Special Member Functions 8 9

Misc typename for template-depended types For default value, use c-tor calling syntax / brace initialization instead of a simple assignment (of 0, for example) for allowing types with explicit c-tors Use of global (not-in-class) template operators Declaration should be in class (as friend), if we want implicit conversion to work (e.g. for operator* for Zp class)

Template specializations Template explicit specialization (full specialization) - relevant for classes and functions (but see below) Template partial specialization - relevant for classes but not for functions For functions, it may seems like a partial specialization, but it's overloading But explicit specialization is not part of the overloading resolution! Better to use only overloading (without specialization) or specialize inside a struct

sf::view, sf::viewport sf::view controlls the displayed part of the window Denes which part of the window coordinate system is displayed sf::viewport controlls which part of the window will be used to show the relevant sf::view Converting mouse click position to window coordinates is a must if you start using views and viewports

Converting values (numbers) to/from text Converting to text is easy: std::to_string() Before 11 we used std::(o)stringstream. It's still useful in some cases Converting text to values: Don't use atoi() or even strtoll()! (or sscanf()) Use the safe alternative: stoi() etc. The second argument, pos, can become handy std::(i)stringstream is more useful in many cases

One of the main concepts of OOP Will be more eective when we'll see polymorphism (which is one of OOP principles) vs. real world categories Pinguin example Rectangle and square example

Polymorphism virtual functions Understand how it works (usually) compile-time dispatch vs. run-time dispatch Possible pitfalls Using dierent arguments (override keyword) Usage in c-tor/d-tor Overriding of non-virtual functions Default arguments Overriding part of an overloading set of virtual functions (using) Virtual d-tor nal keyword (but only for virtual functions) Abstract Base Class principle assignment issue

Class special member functions Class Special Member Functions Destructor Frees the class resources Copy c-tor Controls the creation of a new object from another (e.g. for deep copy) Assignment operator Controls copying one object over an existing one (again, for deep copy and freeing the old resources) Move c-tor Controls the creation of a new object from another, when we can steal resources from the original Move assignment operator Controls the copying one object over an existing one, when we can steal resources from the original Default c-tor Controls the creation of an object when no parameter has given

More about special member functions Class Special Member Functions Explicitly defaulting or deleting special functions Copy/Move sometimes suppressed by RVO/NRVO Rule-of-thumb to identify when move can't happen: if there are two names to access the same object e.g. returning a function static variable

A common misconcept about c-tors Class Special Member Functions Calling a c-tor from within another c-tor body doesn't do what you maybe think it does! It isn't the same as calling a c-tor from another c-tor member initializer list (the term for such a call is delegation)

Compiler errors vs. Linker errors Compiler errors include source line number; Linker errors - only (object) le name Linker errors are usually more complicated and less understandable

2 most common Linker errors Double denition Unresolved externals (unresolved reference)

vs. C Line comments (// vs. block comments - /* comment */) References (so don't use pointers, until we'll have to) const (ported back to C, too) - use it wisely wherever you can; don't use #dene Function overloading Default values for function arguments Namespaces Exceptions OOP (class, public, private, etc.) Generic programming (Templates) Much richer standard library (including STL)

Introduction 11/14 Feels like a new language (Bjarne Stroustrup) We'll see where there are diereneces and what the 98/03 way to do things was VS2015 implements almost all 11/14 and even a bit from 17 Pros and Cons Course objectives