G52CPP C++ Programming Lecture 15

Similar documents
G52CPP C++ Programming Lecture 14. Dr Jason Atkin

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 20

W3101: Programming Languages C++ Ramana Isukapalli

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 17

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

G52CPP C++ Programming Lecture 16

G52CPP C++ Programming Lecture 9

1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended)

CS197c: Programming in C++

CS3157: Advanced Programming. Outline

Where do we stand on inheritance?

Dr. Md. Humayun Kabir CSE Department, BUET

The University of Nottingham

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 12

Introduction to C++ Systems Programming

COEN244: Class & function templates

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

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

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

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

Variables. Data Types.

C++ Casts and Run-Time Type Identification

Increases Program Structure which results in greater reliability. Polymorphism

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

Introduction to Programming Using Java (98-388)

CS11 Advanced C++ Fall Lecture 7

AN OVERVIEW OF C++ 1

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Tokens, Expressions and Control Structures

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

CS201 - Introduction to Programming Glossary By

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Fast Introduction to Object Oriented Programming and C++

Short Notes of CS201

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

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

C++_ MARKS 40 MIN

OBJECT ORIENTED PROGRAMMING USING C++

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

C++ Programming: Polymorphism

C++ Primer for CS175

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

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

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

Exceptions, Case Study-Exception handling in C++.

TPF Users Group Spring 2005

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

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

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

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

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

CPSC 427: Object-Oriented Programming

More on Templates. Shahram Rahatlou. Corso di Programmazione++

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Chapter 17: Nested Classes

CE221 Programming in C++ Part 1 Introduction

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

AIMS Embedded Systems Programming MT 2017

Basic Types, Variables, Literals, Constants

The University of Nottingham

Final CSE 131B Spring 2004

A brief introduction to C++

Introduction to Programming

CPSC 427: Object-Oriented Programming

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

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

ECE 3574: Dynamic Polymorphism using Inheritance

Interview Questions of C++

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

Bruce Merry. IOI Training Dec 2013

CPSC 427: Object-Oriented Programming

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

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

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

The University of Nottingham

CS Lecture #14

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

C++: Overview and Features

C-style casts Static and Dynamic Casts reinterpret cast const cast PVM. Casts

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

Introduction to Programming using C++

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

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

CSE 303: Concepts and Tools for Software Development

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

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

CS93SI Handout 04 Spring 2006 Apr Review Answers

05-01 Discussion Notes

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Engineering Tools III: OOP in C++

Introducing C++ to Java Programmers

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Chapter 2. Procedural Programming

Transcription:

G52CPP C++ Programming Lecture 15 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1

IMPORTANT No optional demo lecture at 2pm this week Please instead use the time to do your coursework I will send you something about collision detection (don t worry) We will have the using app wizard and creating fully featured programs fast (with MFC) demo lecture after the Easter holidays 2

Last Lecture Inheritance and constructors Virtual destructors Namespaces and scoping Some standard class library classes String Input and output 3

Scoping/using clarification #include <string> #include <iostream> using namespace std; namespace cpp void MyPrintFunction1() // Do something using namespace cpp; int main() MyPrintFunction1(); int main() ::MyPrintFunction1(); void MyPrintFunction1() // Do something int main() cpp::myprintfunction1(); 4

Scoping/using clarification #include <string> #include <iostream> using namespace std; namespace cpp void MyPrintFunction1() // Do something void MyPrintFunction1() // Do something using namespace cpp; int main() MyPrintFunction1(); g++ namespace.cpp int main() namespace.cpp: In function int main() : ::MyPrintFunction1(); namespace.cpp:22:19: error: call of overloaded MyPrintFunction1() is ambiguous namespace.cpp:22:19: note: candidates are: int main() namespace.cpp:13:6: void MyPrintFunction1() cpp::myprintfunction1(); namespace.cpp:7:7: void cpp::myprintfunction1() 5

Scoping/using clarification #include $ <string> g++ namespace.cpp using namespace cpp; #include $ <iostream> using namespace std; int main() The other two work correctly, compiling with no errors They are unambiguous namespace cpp MyPrintFunction1(); General rule: If there is ambiguity it will NOT compile void MyPrintFunction1() int main() // Do something ::MyPrintFunction1(); void MyPrintFunction1() // Do something int main() cpp::myprintfunction1(); 6

This Lecture Standard template library overview By example NOT VITAL Conversion operator Friends Casting static cast dynamic cast const cast reinterpret cast 7

Standard Template Library (You need lectures 17 and 18 to understand how this is implemented) A large library of template classes and algorithms Gives speed guarantees for the speed 8

STL container classes vector string map list set stack queue deque multimap multiset In std namespace Know that Standard Template Library exists If you go for C++ job interview, learn basics These are template classes e.g. vector<int> for vector of ints Unlike Java, C++ vector class will check types Also have iterators Track position/index in a container e.g. to iterate through a container And algorithms (over 70 of them) Apply to containers e.g. min(), max(), sort(), search() 9

Example of using vector #include <iostream> #include <string> #include <vector> using namespace std; int main() vector<char> v(10); // 10 elements int size = v.size(); cout << "Size " << size << endl; // Set each value for( int i=0 ; i < size ; i++ ) v[i] = i; // Iterate through vector vector<char>::iterator p = v.begin(); for( ; p!= v.end() ; p++ ) *p += 97; // Output the contents for( int i=0 ; i < size ; i++ ) cout << v[i] << endl; return 0; 10

Conversion operators 11

Conversion operator Convert from a class into something else Uses operator overloading syntax See lecture 17 on operator overloading Instead of an operator symbol, the new type name and () are used e.g. convert to float: operator float() return ; This allows the compiler to convert to the class any time it wants to (without a cast) 12

Conversion constructor and operator class Converter public: // Conversion constructor // Convert INTO this class Converter( int i = 4 ); // Conversion operator // Convert FROM this class operator int(); private: int _i; ; // Conversion operator Converter::operator int() printf( "Converting to int\n" ); return _i; // Conversion constructor Converter::Converter(int i) _i = i; int main() int i = 4; // Construction from int Converter c1(5); Converter c2 = i; // Conversion to int: int j = (int)c2; int k(c2); int m = k + c2; 13

Friends 14

Accessing private data #include <cstdio> class TheFriend public: void DoSomething( Friendly& dest, const Friendly& source ) // Copy _i member dest._i = source._i + 1; ; class Friendly public: Friendly( int i = 4 ) : _i(i) private: int _i; ; Data is private! void FriendFunc( const char* message, const Friendly& thing ) // Access _i member printf( "%s : _i = %d\n", message, thing._i ); int main() Friendly d1(2), d2; TheFriend f; f.dosomething(d2,d1); FriendFunc("d2",d2); 15

Accessing private data #include <cstdio> class TheFriend public: void DoSomething( Friendly& dest, const Friendly& source ) // Copy _i member dest._i = source._i + 1; ; class Friendly public: Friendly( int i = 4 ) : _i(i) private: int _i; ; Do something to Friendly Data is private! void FriendFunc( const char* message, const Friendly& thing ) // Access _i member printf( "%s : _i = %d\n", message, thing._i ); int main() Friendly d1(2), d2; TheFriend f; f.dosomething(d2,d1); FriendFunc("d2",d2); 16

friends Classes can grant access to their private member data and functions to their friends The class still maintains control over which classes and functions have access The friends of a class are treated as class members for access purposes although they are not members Declare your friends within your class body and use the keyword friend 17

friend function class Friendly // Make function a friend friend void FriendFunc( const char* message, const Friendly& thing ); public: Friendly(int i=4) : _i(i) private: int _i; ; void FriendFunc( const char* message, const Friendly& thing ) printf( "%s : _i = %d\n", message, thing._i ); int main() Friendly d1(2), d2; FriendFunc( "d1", d1 ); FriendFunc( "d2", d2 ); 18

class Friendly; class TheFriend public: void DoSomething( Friendly& dest, ;.h file: friend class Forward declaration of class const Friendly& source); class Friendly friend class TheFriend; public: Friendly(int i=4) : _i(i) private: int _i; ;.cpp file: void TheFriend::DoSomething( Friendly& dest, const Friendly& source ) dest._i = source._i + 1; Note: Could make this a static member function since it does not need to access or alter any member data int main() Friendly d1(2), d2; TheFriend f; f.dosomething( d2, d1); 19

Breaking the rules 20

Unchangable values? Here we have constant references passed in Can we change x and y? void foo( const int& x, const int& y ) x = 5; y = 19; Can we add anything to allow us to be able to change them? 21

C++ style casts 22

Casting away the const-ness Remove the constness of a reference or pointer void foo( const int& x, const int& y ) int& xr = (int&)(x); // Since we cast away const-ness we CAN do this xr = 5; // or this int& yr = (int&)(y); yr = 19; WARNING! Do not actually do this unless there is a REALLY good reason! void const_cast_example() Casting away const-ness is usually very bad int x = 4, y = 2; foo( x, y ); printf( "x = %d, y = %d\n", x, y ); 23

const_cast <type> (var) Remove the constness of a reference or pointer void foo( const int& x, const int& y ) int& xr = const_cast<int&>(x); // Since we cast away const-ness we CAN do this xr = 5; // or this int& yr = const_cast<int&>(y); yr = 19; WARNING AGAIN Do not actually do this unless there is a REALLY good reason! void const_cast_example() Casting away const-ness is usually very bad int x = 4, y = 2; foo( x, y ); printf( "x = %d, y = %d\n", x, y ); 24

Four new casts const_cast<newtype>(?) Get rid of const ness (or volatile-ness) No cast needed to add const ness (or volatile) dynamic_cast<newtype>(?) Safely cast a pointer or reference from base-class to sub-class Checks that it really IS a sub-class object static_cast<newtype>(?) Cast between types, converting the type reinterpret_cast<newtype>(?) Interpret the bits in one type as another Mainly needed for low-level code Effects are often platform-dependent i.e. treat the thing at this address as if it was a 25

Why use the new casts? This syntax makes the presence of casts more obvious Casts mean you are bending the rules somehow It is useful to be able to find all places that you do this This syntax makes the purpose of the cast more obvious i.e. casting to remove const or to change the type Four types give more control over what you mean, and help you to identify the effects Sometimes needed: dynamic_cast provides run-time type checking Note: Casting a pointer will not usually change the stored address value, only the type. This is NOT true with multiple inheritance 26

static_cast <type> (var) static_cast<newtype>(oldvariable) Commonly used cast Attempts to convert correctly between two types Usually use this when not removing const-ness and there is no need to check the sub-class type at runtime Works with multiple inheritance (unlike reinterpret!) void static_cast_example() float f = 4.1; // Convert float to an int int i = static_cast<int>(f); printf( "f = %f, i = %d\n", f, i ); 27

dynamic_cast <type> (var) Casting from derived class to base class is easy Derived class object IS a base class object Base class object might not be a derived class object dynamic_cast<>() Safely convert from a base-class pointer or reference to a sub-class pointer or reference Checks the type at run-time rather than compile-time Returns NULL if the type conversion of a pointer cannot take place (i.e. it is not of the target type) There is no such thing as a NULL reference If reference conversion fails, it throws an exception of type std::bad_cast (see Thursday lecture) 28

static_cast example sub1 s1; sub1* ps1 = &s1; base // Fine: treat as base class base* pb1 = ps1; sub1 sub2 // Treat as sub-class sub2* ps2err = static_cast<sub2*>(pb1); // Static cast: do conversion. ps2err->func(); // This is an BAD error // Treating sub1 object as a sub2 object 29

dynamic_cast example sub1 s1; sub1* ps1 = &s1; base // Fine: treat as base class base* pb1 = ps1; sub1 sub2 // Treat as sub-class sub2* ps2safe = dynamic_cast<sub2*>(pb1); // Dynamic cast: runtime check if ( ps2safe == NULL ) printf( "Dynamic cast on pb2 failed\n" ); else ps2safe->func(); 30

Exception thrown by dynamic_cast void foo() Dynamic cast on a reference Sub1 s1; Base& rb = s1; Sub2& rs2 = dynamic_cast<sub2&>(rb); cout << "No exception was thrown by foo()" << endl; int main() try foo(); catch ( bad_cast ) class Sub1 class Base class Sub2 cout << "bad_cast exception thrown" << endl; catch (... ) cout << "Other exception thrown" << endl; Note: s1 is destroyed properly when stack frame is destroyed 31

reinterpret_cast<type>(var) reinterpret_cast<>() Treat the value as if it was a different type Interpret the bits in one type as another Including platform dependent conversions Hardly ever needed, apart from with low-level code Like saying Trust me, you can treat it as one of these e.g.: void reinterpret_cast_example() int i = 1; int* p = & i; i = reinterpret_cast<int>(p); printf( "i = %x, p = %p\n", i, p ); 32

A Casting Question Where are casts needed, and what sort of casts should be used? (Assume BouncingBall is a sub-class of BaseEngine) BouncingBall game; BaseEngine* pgame = &game; //? BouncingBall* pmgame = pgame; //? BouncingBall game; BaseEngine& rgame = game; //? BouncingBall& rmgame = rgame; //? 33

Answer : pointers BouncingBall game; BaseEngine* pgame = &game; // No cast BouncingBall* pmgame = dynamic_cast<bouncingball*>(pgame); if ( pgame==null ) /* Failed */ No cast needed to go from sub-class to base class. In this case, because the game object really is a BouncingBall, a static_cast would have worked. But would not have checked this would have been BAD! 34

Answer : references BouncingBall game; BaseGameEngine& rgame = game; // No cast try BouncingBall& rmgame = dynamic_cast<bouncingball&>(rgame); catch ( std::bad_cast b ) // Handle the exception // Happens if rgame is NOT a BouncingBall Need to check for any exceptions being thrown for references Again, in this case, because the rgame really is a BouncingBall, a static_cast would have worked. But would have been BAD! 35

Repeat: dynamic_cast Safely converts from a base-class pointer or reference to a sub-class pointer or reference Checks the type at run-time rather than compile-time, to verify it really is a sub-class Returns NULL if the type conversion of a pointer cannot take place i.e. it is not of the target type If reference conversion fails it throws an exception of type std::bad_cast There is no such thing as a NULL reference 36

Other casts questions When would you use a const_cast? What is the difference between a reinterpret_cast and a static_cast? When would you use a static_cast? 37

Answers When would you use a const_cast? To remove const or volatile qualifier This is the only C++ style cast that can do that What is the difference between a reinterpret_cast and a static_cast? reinterpret_cast says change the type of the pointer. i.e. keep the bits/bytes that it points to, but treat it as the new type. e.g. float* to int* static_cast says attempt to actually do the conversion between types (e.g. float to int) When would you use a static_cast? When none of the others apply i.e. unless casting from base to sub-class, wanting to keep the bits or removing const/volatile 38

Next lecture Exceptions and exception handling RAII (Resource Acquisition Is Initialisation) 39