Advanced C++ Topics. Alexander Warg, 2017

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

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

Instantiation of Template class

C++ Important Questions with Answers

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

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

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

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

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

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

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

Polymorphism. Zimmer CSCI 330

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Fast Introduction to Object Oriented Programming and C++

Structs. Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: Memory Layout

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

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

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

CS3157: Advanced Programming. Outline

Overview. 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43

C++11 and Compiler Update

eingebetteter Systeme

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Chapter 02 Building Multitier Programs with Classes

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

C++ Inheritance and Encapsulation

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

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

COEN244: Class & function templates

Short Notes of CS201

Special Member Functions

Introduction to Programming Using Java (98-388)

CS201 - Introduction to Programming Glossary By

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

Object Oriented Programming. Solved MCQs - Part 2

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

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

CS250 Final Review Questions

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

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CS250 Final Review Questions

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Polymorphism Part 1 1

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

Data Structures using OOP C++ Lecture 3

C++ (classes) Hwansoo Han

VIRTUAL FUNCTIONS Chapter 10

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Absolute C++ Walter Savitch

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

C[exam] [2007] [Based On The Lectures Of Moti Freiman && Ofir Pele] Collected, extended upon and written by: Aviad Pines && Lev Faerman.

Syllabus of C++ Software for Hands-on Learning: This course offers the following modules: Module 1: Getting Started with C++ Programming

C The new standard

Object-Oriented Concept

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Object Oriented Programming with c++ Question Bank

Programsystemkonstruktion med C++: Övning 2. Karl Palmskog september 2010

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

CS105 C++ Lecture 7. More on Classes, Inheritance

Operator overloading. Conversions. friend. inline

Advanced Programming & C++ Language

Advanced Systems Programming

Chapter 5 Object-Oriented Programming

More C++ : Vectors, Classes, Inheritance, Templates

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Lesson 11: Generic Programming. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

CS304 Object Oriented Programming

Object Oriented Programming

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

15: Polymorphism & Virtual Functions

September 10,

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Intermediate C++ 1/83

Programming in C++: Assignment Week 8

Object-Oriented Principles and Practice / C++

C++ Programming: Polymorphism

Names, Scopes, and Bindings II. Hwansoo Han

Rvalue References & Move Semantics

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions

CS304 Object Oriented Programming Final Term

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

Modernizing legacy C++ code

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

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

Tokens, Expressions and Control Structures

Object Oriented Design

Evaluation Issues in Generic Programming with Inheritance and Templates in C++

CPSC 427: Object-Oriented Programming

Transcription:

www.kernkonzept.com Advanced C++ Topics Alexander Warg, 2017 M I C R O K E R N E L M A D E I N G E R M A N Y

Overview WHAT IS BEHIND C++ Language Magics Object Life Time Object Memory Layout INTRODUCTION TO TEMPLATES Template Function Template Class WHAT I DO NOT EXPLAIN Standard C++ Library

C++ vs C Some more keywords new, delete, class, virtual, mutable, explicit... Stricter type system e.g. no automatic conversion from void * custom class types strictly typed enums (C++11) Function overloading / Operator overloading multiple functions with the same name but different arguments Extensible hierarchical type system classes and inheritance Generic programming via templates

CONSTRUCTION AND DESTRUCTION Constructors: Special Member Functions for object initialization Same name as the class No return type

CONSTRUCTION AND DESTRUCTION Constructors: Special Member Functions for object initialization Same name as the class No return type Destructors: Special Member Functions for object destruction Name: ~Classname() No return type No arguments

CONSTRUCTORS (CLASS FOO)

CONSTRUCTORS (CLASS FOO) Foo() -> Default Constructor No arguments Generated by Compiler if no other Constructors

CONSTRUCTORS (CLASS FOO) Foo() -> Default Constructor No arguments Generated by Compiler if no other Constructors Foo(Type x) -> Conversion Constructor Is used to cast type Type to Foo (implicitly) (see keyword explicit)

CONSTRUCTORS (CLASS FOO) Foo() -> Default Constructor No arguments Generated by Compiler if no other Constructors Foo(Type x) -> Conversion Constructor Is used to cast type Type to Foo (implicitly) (see keyword explicit) Foo(Foo const &o) -> Copy Constructor Always generated by Compiler if not provided (related to operator = (Foo const &o), see later)

CONSTRUCTORS (CLASS FOO) Foo() -> Default Constructor No arguments Generated by Compiler if no other Constructors Foo(Type x) -> Conversion Constructor Is used to cast type Type to Foo (implicitly) (see keyword explicit) Foo(Foo const &o) -> Copy Constructor Always generated by Compiler if not provided (related to operator = (Foo const &o), see later) Foo(Type a, Type b, Type c) -> Normal Constructor

CONSTRUCTORS (CLASS FOO) C++0X Foo(Foo &&o) -> Move Constructor (related to operator = (Foo &&o), see later)

CONSTRUCTORS (CLASS FOO) C++0X Foo(Foo &&o) -> Move Constructor (related to operator = (Foo &&o), see later) Type &&x -> RValue refernce Fast optimized move operations Support for perfect forwarding

TYPE CONVERSION / CAST Implicit type conversion among integer types (incl. enum) conversion ctor? conversion operator? from pointers/references of derived classes to pointers/references to base classes Explicit type conversion (casts) C++ has three (actually four) types of casts static_cast<type>(...) reinterpret_cast<type>(...) dynamic_cast<type>(...) const_cast<type>(...)

POLYMORPHISM (ADVANCED) Virtual Functions Support for Overriding functions in C++ Pure Virtual Functions (Abstract Function) class A { void func() = 0; }; <A> cannot be instantiated (is abstract) Multiple Inheritance class A : public B, public C {...};

Virtual deletion... POLYMORPHISM VIRTUAL DTOR

POLYMORPHISM II MULTIPLE INHERITANCE

POLYMORPHISM II MULTIPLE INHERITANCE Car name()

POLYMORPHISM II MULTIPLE INHERITANCE Car name() Boat name()

POLYMORPHISM II MULTIPLE INHERITANCE Object cnt Car name() Boat name()

POLYMORPHISM II MULTIPLE INHERITANCE Object cnt Car name() Boat name() Amphi

POLYMORPHISM II MULTIPLE INHERITANCE Amphi Car Object Boat Object

POLYMORPHISM II MULTIPLE INHERITANCE Amphi Amphi Car Boat Car Boat Object Object Object

TEMPLATES FUNCTION TEMPLATES Functions that operate on a Generic Type (e.g. T)

TEMPLATES FUNCTION TEMPLATES Functions that operate on a Generic Type (e.g. T) int max(int a, int b) { return a>b?a:b; }

TEMPLATES FUNCTION TEMPLATES Functions that operate on a Generic Type (e.g. T) int max(int a, int b) { return a>b?a:b; } int a, b; int x = max(a, b);

TEMPLATES FUNCTION TEMPLATES Functions that operate on a Generic Type (e.g. T) int max(int a, int b) { return a>b?a:b; } int a, b; int x = max(a, b); double a, b; double x = max(a, b);

TEMPLATES FUNCTION TEMPLATES Functions that operate on a Generic Type (e.g. T) template< typename T > T max(t a, T b) { return a>b?a:b; } int a, b; int x = max(a, max<int>(a, b); b); double a, b; double x = max(a, max<double>(a, b); b);

TEMPLATES FUNCTION TEMPLATES Functions that operate on a Generic Type (e.g. T) template< typename T > T max(t a, T b) { return a>b?a:b; } int a, b; int x = max(a, b); double a, b; double x = max(a, b);

TEMPLATES CLASS TEMPLATES Classes with members of Generic Types (e.g. T)

TEMPLATES CLASS TEMPLATES Classes with members of Generic Types (e.g. T) class List_item { List_item *_next, *_prev; void *_data; };

TEMPLATES CLASS TEMPLATES Classes with members of Generic Types (e.g. T) template< typename T > class List_item { List_item *_next, *_prev; void T *_data; *_data; };

DON'T DO THIS... Too Much operator overloading Keep usual semantics Avoid implicit conversion operators using namespace <X> in Header Files #define... Use enum's for constant values Use templates for functions