G52CPP C++ Programming Lecture 12

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

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Introducing C++ David Chisnall. March 15, 2011

G52CPP C++ Programming Lecture 16

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

C++ Inheritance and Encapsulation

G52CPP C++ Programming Lecture 20

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

G52CPP C++ Programming Lecture 15

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

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

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

The University of Nottingham

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

The University of Nottingham

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

Java and C CSE 351 Spring

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

Polymorphism Part 1 1

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Inheritance, Polymorphism and the Object Memory Model

Lecture 5: Inheritance

04-24/26 Discussion Notes

1d: tests knowing about bitwise fields and union/struct differences.

VIRTUAL FUNCTIONS Chapter 10

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Fast Introduction to Object Oriented Programming and C++

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Kurt Schmidt. October 30, 2018

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

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

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

G52CPP C++ Programming Lecture 18

Data Abstraction. Hwansoo Han

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Supporting Class / C++ Lecture Notes

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

G52CPP C++ Programming Lecture 17

ECE 15B COMPUTER ORGANIZATION

CSE 303: Concepts and Tools for Software Development

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

6.096 Introduction to C++ January (IAP) 2009

Java and C. CSE 351 Autumn 2018

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

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Advanced Systems Programming

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

Fundamental of Programming (C)

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

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

C++ Modern and Lucid C++ for Professional Programmers

Lecture 2: C Programm

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

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

Object-Oriented Programming, Iouliia Skliarova

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

Extending Classes (contd.) (Chapter 15) Questions:

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

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

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

Objectives. External declarations Pointers Dynamic data structures using pointers Function pointers

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Dynamic Binding C++ Douglas C. Schmidt

CS3157: Advanced Programming. Outline

G Programming Languages - Fall 2012

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Introduction to Programming Using Java (98-388)

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

C++ Important Questions with Answers

C++ Inheritance and Encapsulation

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

C Programming Review CSC 4320/6320

Polymorphism. Zimmer CSCI 330

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

What are the characteristics of Object Oriented programming language?

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Object Oriented Programming. Solved MCQs - Part 2

C Praktikum. Advanced Pointers. Eugen Betke, Nathanael Hübbe, Michael Kuhn, Jakob Lüttgau, Jannek Squar

Tokens, Expressions and Control Structures

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS250 Final Review Questions

Static Vulnerability Analysis

C++ (classes) Hwansoo Han

JAVA MOCK TEST JAVA MOCK TEST II

CS11 Advanced C++ Fall Lecture 7

Introduction to C: Pointers

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

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

Variables and literals

Short Notes of CS201

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Derived and abstract data types. TDT4205 Lecture 15

Transcription:

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

Last lecture this and static members References Act like pointers Look like values More const And mutable 2

This lecture Function pointers Virtual and non-virtual functions 3

Function pointers 4

Function pointers Functions are stored in memory You can ask for the address of them You can store these in function pointers Used a lot in low-level programming Operating system calls often want function pointers And for callback functions Allows something to call you back Call this function when an event happens Event driven programming is VERY common 5

Simplest functions some code void f1() printf( "f1(); " ); void f2() printf( "f2(); " ); int main() void (*g1)() = NULL; void (*g2)() = NULL; Note: Not function calls, but strings! g1 and g2 are function pointers of type void function() printf("test 1: " ); g1 = &f1; (*g1)(); g1(); // Short way printf("\ntest 2: " ); g2 = &f2; (*g2)(); g2(); // Short way Note: The & is optional. As for arrays. Function name is a pointer to it. 6

Simplest functions the output void f1() printf( "f1(); " ); void f2() printf( "f2(); " ); int main() void (*g1)() = NULL; void (*g2)() = NULL; printf("test 1: " ); g1 = &f1; (*g1)(); g1(); // Short way Test 1: f1(); f1(); printf("\ntest 2: " ); g2 = &f2; (*g2)(); g2(); // Short way Test 2: f2(); f2(); 7

Assignment of pointers void f1() printf( "f1(); " ); void f2() printf( "f2(); " ); int main() void (*g1)() = NULL; void (*g2)() = NULL; g1 = &f1; g2 = &f2; // Assignment of function pointers printf("\ntest 3: " ); g2 = g1; (*g2)(); g2(); // Short way 8

Assignment void f1() printf( "f1(); " ); void f2() printf( "f2(); " ); int main() void (*g1)() = NULL; void (*g2)() = NULL; g1 = &f1; g2 = &f2; // Assignment of function pointers printf("\ntest 3: " ); g2 = g1; (*g2)(); Test 3: f1(); f1(); g2(); // Short way 9

Returning values int f3() printf( "f3(); " ); return 3; int f4() printf( "f4(); " ); return 4; int main() int (*g3)() = NULL; int (*g4)() = NULL; printf("\ntest 4: " ); g3 = &f3; g4 = &f4; printf( "Result = %d ", g3() ); printf( "Result = %d ", g4() ); 10

Returning values int f3() printf( "f3(); " ); return 3; int f4() printf( "f4(); " ); return 4; int main() int (*g3)() = NULL; int (*g4)() = NULL; printf("\ntest 4: " ); g3 = &f3; g4 = &f4; printf( "Result = %d ", g3() ); printf( "Result = %d ", g4() ); Test 4: f3(); Result = 3 f4(); Result = 4 11

Passing parameters int f3() printf( "f3(); " ); return 3; int f5( int i ) printf( "f5( %d ); ", i ); return i+1; int f6( int i ) printf( "f6( %d ); ", i ); return i-2; int main() int (*g3)() = &f3; int (*g5)( int ) = &f5; int (*g6)( int ) = &f6; printf("\ntest 5: " ); g5( 1 ); g6( 2 ); printf("\ntest 6: " ); g5( g3() ); printf("\ntest 7: " ); g6( g4() ); printf("\ntest 8: " ); g6( g5( g3() ) ); 12

Passing parameters int f3() printf( "f3(); " ); return 3; int f5( int i ) printf( "f5( %d ); ", i ); return i+1; int f6( int i ) printf( "f6( %d ); ", i ); return i-2; int main() int (*g3)() = &f3; int (*g5)( int ) = &f5; int (*g6)( int ) = &f6; Test 5: f5( 1 ); f6( 2 ); Test 6: f3(); f5( 3 ); Test 7: f4(); f6( 4 ); Test 8: f3(); f5( 3 ); f6( 4 ); printf("\ntest 5: " ); g5( 1 ); g6( 2 ); printf("\ntest 6: " ); g5( g3() ); printf("\ntest 7: " ); g6( g4() ); printf("\ntest 8: " ); g6( g5( g3() ) ); 13

Callback function int f5( int i ) printf("f5( %d ); ",i); return i+1; // Execute a callback function ten times int Do10( int (*func)(int) ) int i = 0, j = 0; for ( ; i < 10 ; i++ ) j = func( j ); return j; int f6( int i ) printf("f6( %d ); ",i); return i-2; int main() int (*g5)( int ) = &f5; int (*g6)( int ) = &f6; printf("\ntest 9: " ); Do10( g5 ); printf("\ntest 10: " ); Do10( g6 ); 14

Callback function int f5( int i ) printf("f5( %d ); ",i); return i+1; // Execute a callback function ten times int Do10( int (*func)(int) ) int i = 0, j = 0; for ( ; i < 10 ; i++ ) j = func( j ); return j; int f6( int i ) printf("f6( %d ); ",i); return i-2; int main() int (*g5)( int ) = &f5; int (*g6)( int ) = &f6; printf("\ntest 9: " ); Do10( g5 ); printf("\ntest 10: " ); Do10( g6 ); Test 9: f5( 0 ); f5( 1 ); f5( 2 ); f5( 3 ); f5( 15 4 ); f5( 5 ); f5( 6 ); f5( 7 ); f5( 8 ); f5( 9 );

Callback function int f5( int i ) printf("f5( %d ); ",i); return i+1; // Execute a callback function ten times int Do10( int (*func)(int) ) int i = 0, j = 0; for ( ; i < 10 ; i++ ) j = func( j ); return j; int f6( int i ) printf("f6( %d ); ",i); return i-2; int main() int (*g5)( int ) = &f5; int (*g6)( int ) = &f6; printf("\ntest 9: " ); Do10( g5 ); printf("\ntest 10: " ); Do10( g6 ); Test 10: f6( 0 ); f6( -2 ); f6( -4 ); f6( -6 ); f6( -8 16 ); f6( -10 ); f6( -12 ); f6( -14 ); f6( -16 ); f6( -18 );

Function pointer typedef You can using typedef to create a new type which can simplify the code considerably: /* Make name fptr1 mean function pointer: return int, int parameter */ typedef int (*fptr1)(int); /* return void, float parameter */ typedef void (*fptr2)(float); /* Usage: */ fptr1 f= ; fptr2 g=, myfptr = ; 17

Arrays of function pointers #include <cstdio> int f1( int i ) return i; int f2( int i ) return i*2; int f3( int i ) return i*3; int main() int i,j,k; fptr fptrarray[6]; fptrarray[0] = &f1; fptrarray[1] = &f2; fptrarray[2] = &f3; /* typedef makes the name fptr mean a function pointer: int <f> (int) */ typedef int (*fptr)(int); 1. Addresses of functions are stored in an array 2. Can call functions by index in the array rather than by name i = fptrarray[2](2); j = fptrarray[0](i); k = fptrarray[2](j); printf( "%d %d %d\n", i, j, k ); return 0; 18

In an exam Any exam question will have a similar type of form to the last few slides i.e. What is the output of this code 1. Work out what points to what 2. And resolve the function calls 19

v-tables 20

Example: virtual functions class BaseClass public: char* foo() return "BaseFoo"; virtual char* bar() return "BaseBar"; ; class SubClass : public BaseClass public: char* foo() return "SubFoo"; virtual char* bar() return "SubBar "; ; int main() SubClass* psub = new SubClass; BaseClass* psubasbase = psub; printf( "psubasbase->foo() %s\n", psubasbase->foo() ); printf( "psubasbase->bar() %s\n", psubasbase->bar() ); delete psub; 21

Virtual and non-virtual functions For normal/default (non-virtual) functions: Type of pointer determines function to call i.e. the apparent type of the object, from pointer type Use the type of the object the compiler thinks it is: Type of pointer (or reference) to the object Type of the member function making the call (hidden this ptr) Easier for the compiler, type is known at compile-time Virtual function: Finds out the actual function to call based upon the object type AT RUNTIME - much more difficult - slower i.e. look-up: which function should I really call Works in the same way as Java functions 22

Possible Implementation How could virtual functions be implemented? One possible implementation uses vtables (virtual function tables) and vpointers (pointers to a vtable) This is equivalent to having a hidden pointer: struct EMPLOYEE void** vpointer; char strname[64]; int iemployeeid; Employee; Pointer to array of function pointers

The vtable (virtual function table) class BaseClass public: virtual void foo1(); virtual void foo2(); virtual void foo3(); class SubClass1 : public BaseClass public: virtual void foo1(); virtual void foo2(); virtual void foo4(); class SubClass2 : public SubClass1 public: virtual void foo1(); virtual void foo3(); virtual void foo5(); Object has a hidden pointer to the vtable for its class (vpointer) BaseClass object SubClass1 object SubClass2 object Class has a vtable (which functions to call) BaseClass::foo1() BaseClass::foo2() BaseClass::foo3() SubClass1::foo1() SubClass1::foo2() BaseClass::foo3() SubClass1::foo4() SubClass2::foo1() SubClass1::foo2() SubClass2::foo3() SubClass1::foo4() SubClass2::foo5() 24

The vtable (virtual function table) class BaseClass public: virtual void foo1(); virtual void foo2(); virtual void foo3(); Object has a hidden pointer to the vtable for its class (vpointer) BaseClass object class SubClass1 : public BaseClass public: virtual void foo1(); SubClass1 virtual void foo2(); object virtual void foo4(); class SubClass2 : public SubClass1 public: SubClass2 virtual void foo1(); object virtual void foo3(); virtual void foo5(); Class has a vtable (which functions to call) BaseClass::foo1() BaseClass::foo2() BaseClass::foo3() SubClass1::foo1() SubClass1::foo2() BaseClass::foo3() SubClass1::foo4() SubClass2::foo1() SubClass1::foo2() SubClass2::foo3() SubClass1::foo4() The index in the array matters!!! The caller only needs to know which index is which function. Sub-classes keep the index the same as the base class and just add new functions. SubClass2::foo5() 25

Possible Implementation (1) One possible implementation for vtables (virtual function tables) and vpointers (pointers to a vtable) can be simulated in as follows: struct EMPLOYEE void** vpointer; char strname[64]; int iemployeeid; Employee; Pointer to array of function pointers

Possible Implementation (2) Define the functions that could be called char* vgetmanagertypename() return Manager ; char* vgetemployeetypename() return Employee ; char* vgetdirectortypename() return Director ; 27

Possible Implementation (3) Define the functions that could be called Create the arrays of function pointers (void* pointers) void* pemployeefunctions[] = &vgetemployeetypename, &vgetnameemployee, &vgetemployeeid ; void* pmanagerfunctions[] = &vgetmanagertypename, &vgetnamemanager, &vgetmanageremployeeid ; void* pdirectorfunctions[] = &vgetdirectortypename, &vgetnamedirector, &vgetdirectoremployeeid ; void* so that Function types can differ Must cast to correct type before use 28

Possible Implementation (4) typedef struct EMPLOYEE void** vtable; char strname[64]; int iemployeeid; Employee; Add the vtable pointer to the class void* pemployeefunctions[3] = &vgetemployeetypename, &vgetname, &vgetemployeeid ; [0] [1] [2] Fake the vtable An array of pointers // Create the structs Employee e1 = pemployeefunctions, "Employee 1", 1 ; Employee e2 = pemployeefunctions, "Employee 2", 2 ; When you create the objects set the vtable pointer

Using the vptr to find the function How to use a vtable: 1. Extract the pointer to the array of virtual functions from the object 2. Find function pointer at the correct place in the array 3. Cast the function pointer to the correct type 4. Call the function You need to know: array index, function return type and parameter value types Char* GetTypeName( Employee* e ) void* vfn = e->vtable[0]; char* (*fn)(/*params*/) = vfn; return (*fn)(); // Extract // Cast // Call 30

What to know Some equivalent of a vpointer exists in objects with virtual functions Just one pointer is needed in each object Only virtual functions appear in vtables No need to record non-virtual functions Looking up which function to call is slower than calling a non-virtual function But can be done! 1. Go to the object itself 2. Go to the vtable (following the vpointer) 3. Look up which function to call from index 4. Call the function 31

Example: virtual functions #include <cstdio> class BaseClass public: BaseClass vtable: virtual char* bar() return "BaseBar"; char* bar2() return this->bar(); BaseClass::bar() ; class SubClass : public BaseClass public: char* bar() return "SubBar "; SubClass vtable: ; int main() SubClass::bar() BaseClass* pbase = new BaseClass; SubClass* psub = new SubClass; BaseClass* psubasbase = psub; printf( "bar B=%s S=%s SaB=%s\n", pbase->bar(), psub->bar(), psubasbase->bar() ); printf( "bar2 B=%s S=%s SaB=%s\n", pbase->bar2(), psub->bar2(), psubasbase->bar2() ); delete pbase; delete psub; 32

Notes on virtual functions virtual-ness is inherited If a function is virtual in the base class then the function is virtual in the derived class(es) Including destructor! (If virtual in base class then destructors of derived classes are virtual) Even when the keyword virtual is not used in the derived class All functions in Java are virtual by default Unless you make them final Java, final functions are different: they cannot be re-implemented in sub-classes 33

Pure virtual functions A function in a base class may be available ONLY for re-implementation in sub-class Like abstract function in Java Rather than giving a dummy implementation for a function in a base class, make it pure virtual In declaration in class body use =0 not or ; e.g. virtual void purevirtfoo() = 0; It exists as a place-holder in vtable, but with no function to call (function pointer = NULL/0?) A class with at least one pure virtual function is an abstract class (rather than a concrete class) 34

Should a function be virtual? If member function is called from a base class function or through a base class pointer AND the behaviour should depend on class type then the member function has to be virtual Otherwise when it is called by the base class, or through a base-class pointer, the base-class version will be called, not your modified version Utility functions will often not be virtual When functionality is not expected to be changed in sub-classes Faster to call these functions no look-up needed Makes it easier for function to be inline, which can make code even faster: no function call needed 35

To be clear sizeof() Functions act on objects of the class type Even member functions They are not actually in the objects The just have a hidden this parameter saying which object they apply to The object is the collection of data But also includes any hidden data and pointers that the compiler adds to implement features (e.g. vtable) Adding a member function to an existing class will not usually make the objects bigger Exception: adding the first virtual function may add a vtable pointer (or equivalent) Understand why this is the case! 36

Next lecture Automatically created methods: Default Constructor Copy Constructor Assignment operator Destructor Conversion constructors Conversion Operators Friends 37