Lecture 5. Function Pointers

Similar documents
Computer Programming with C++ (21)

Object Oriented Design

Chapter 9 Classes : A Deeper Look, Part 1

Introduction to Programming session 24

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

IS 0020 Program Design and Software Tools

clarity. In the first form, the access specifier public : is needed to {

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Classes: A Deeper Look

57:017, Computers in Engineering C++ Classes

Classes and Data Abstraction

Lecture 18 Tao Wang 1

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

Chapter 16: Classes and Data Abstraction

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

Classes: A Deeper Look. Systems Programming

Chapter 6: Classes and Data Abstraction

A Deeper Look at Classes

Chapter 10 Introduction to Classes

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools

Function Overloading

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

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

Computer Programming Class Members 9 th Lecture

More C++ Classes. Systems Programming

Constructor - example

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Object-Oriented Programming

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

C++ Classes, Constructor & Object Oriented Programming

CS201 Some Important Definitions

Object Oriented Design

COEN244: Class & function templates

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Cpt S 122 Data Structures. Templates

CS313D: ADVANCED PROGRAMMING LANGUAGE

Angela Chih-Wei Tang Visual Communications Lab Department of Communication Engineering National Central University JhongLi, Taiwan.

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

AN OVERVIEW OF C++ 1

Programming C++ Lecture 2. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Midterm Review. PIC 10B Spring 2018

Intermediate Programming & Design (C++) Classes in C++

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Make Classes Useful Again

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

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

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

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

Constructors for classes

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

Fig. 7.1 Fig. 7.2 Fig. 7.3 Fig. 7.4 Fig. 7.5 Fig. 7.6 Fig. 7.7 Fig. 7.8 Fig. 7.9 Fig. 7.10

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

CS201 Latest Solved MCQs

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

9.1 Introduction. Integrated Time class case study Preprocessor wrapper Three types of handles on an object. Class functions

Praktikum: Entwicklung interaktiver eingebetteter Systeme

by Pearson Education, Inc. All Rights Reserved. 2

Short Notes of CS201

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Design

Launchpad Lecture -10

CS201 - Introduction to Programming Glossary By

Object-Oriented Principles and Practice / C++

Functions and Recursion

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

Faculty of Information and Communication Technologies

OBJECT ORIENTED PROGRAMMING USING C++

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

An Introduction to C++

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Object-Oriented Programming

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Chapter 17 - C++ Classes: Part II

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

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

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

INHERITANCE: CONSTRUCTORS,

Copy Control 2008/04/08. Programming Research Laboratory Seoul National University

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Programming in C and C++

Lab 2: ADT Design & Implementation

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

Chapter 13: Copy Control. Overview. Overview. Overview

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

Object-Based Programming (Deitel chapter 8)

Introduction to C++ Systems Programming

Lecture 15a Persistent Memory & Shared Pointers

CSCE 110 PROGRAMMING FUNDAMENTALS

Transcription:

Lecture 5 Pointers to functions Complicated declarations Allocating and deallocating memory Classes const member functions Constructors and destructor this pointer static members Templates Lec 5 Programming in C++ 1 Function Pointers Pointers to functions Contain address of function Function name is starting address of code that defines function Function pointers can be Passed to functions Returned from functions Stored in arrays Assigned to other function pointers Lec 5 Programming in C++ 2 1

Function Pointers Calling functions using pointers Assume parameter: bool (*compare) (int, int) Execute function with (*compare) (4,5) Dereference pointer to function to execute Example: multipurpose bubble sort See Fig05_25.cpp Lec 5 Programming in C++ 3 Complicated Declarations It is important to understand them for using library functions etc. In Open GL you have the following functions: void glpolygonstipple(const Glubyte *mask); void glutdisplayfunc(void (*f)(void)); Whatis this? Lec 5 Programming in C++ 4 2

Complicated Declarations (cont.) int* f; int* f(void); int (*f)(void); //pointer to a function int* (*f)(void); int a[10]; int* a[10]; //array of pointers Lec 5 Programming in C++ 5 Complicated Declarations (cont.) //array of 10 pointers to functions int (*f[10])(void); int** f; Could write more clear: typedef int (*funptr)(void); funptr F[10]; Lec 5 Programming in C++ 6 3

Complicated Declarations (cont.) int* ptr; int x; //non constant pointer to constant data const int* ptr; *ptr = 100; //ERROR //constant pointer to nonconstant data int* const ptr = &x; ptr = &y; //ERROR //constant pointer to constant data const int* const ptr = &x; Lec 5 Programming in C++ 7 What Are Classes? Classes See Fig06_6.6 Model objects Attributes (data members) Behaviors (member functions) Defined using keyword class Member functions also called methods Member access specifiers public: Accessible wherever object of class in scope private: Accessible only to member functions of class protected: Lec 5 Programming in C++ 8 4

Initializing Class Objects: Constructors Constructor function Special member function Initializes data members Same name as class Called when object instantiated Several constructors Function overloading No return type Lec 5 Programming in C++ 9 Declaring Objects Class name new type specifier Time sunset; // object of type Time // default constructor is called Time sunset1(5,10,12); Time arrayoftimes[ 5 ]; Time *pointertotime; Time &dinnertime = sunset; // array of Time objects // pointer to a Time object // reference to a Time object sunset1.printstandard(); pointertotime->printstandard(); (*pointertotime).printstandard(); dinnertime.printstandard(); Equivalent statements Lec 5 Programming in C++ 10 5

Destructors Destructors Special member function Performs termination housekeeping Before system reclaims object s memory Reuse memory for new objects Same name as class Preceded with tilde (~) No arguments See Fig06_15_17 No return value Cannot be overloaded No explicit destructor Compiler creates empty destructor Lec 5 Programming in C++ 11 Accessing Class Members Data members, member functions Immediately accessible by all member functions Referenced by name Outside class scope Can only access public data members and public functions Referenced through object name, reference to object, pointer to object Lec 5 Programming in C++ 12 6

Default Memberwise Assignment Assigning objects Assignment operator (=) Can assign one object to another of same type Default: memberwise assignment Each right member assigned individually to left member Time sunset1(5,10,12); Time sunset; sunset = sunset1; Lec 5 Programming in C++ 13 Separating Interface from Implementation Header files Class definitions and function prototypes Included in each file using class #include.h File extension.h See Fig06_09_11 Source-code files Member function definitions Same base name (.cpp) Convention Lec 5 Programming in C++ 14 7

Dynamic Memory Management Control allocation and deallocation of memory Reserves/frees memory for an object/ variable Operators new and delete Include standard header <new> Access to standard version of new Lec 5 Programming in C++ 15 Dynamic Memory Management new Time *timeptr; timeptr = new Time; new operator Creates object of proper size for type Time Error if no space in memory for object Calls default constructor for object Returns pointer of specified type Providing initializers double *ptr = new double(3.14159); Time *timeptr = new Time(12,0,0); Allocating arrays int *gradesarray = new int[10]; Lec 5 Programming in C++ 16 8

Dynamic Memory Management delete Destroy dynamically allocated object and free space delete timeptr; Operator delete Calls destructor for object See Fig07_17_19 Deallocates memory associated with object Memory can be reused to allocate other objects Deallocating arrays delete [] gradesarray; Deallocates array to which gradesarray points If pointer to array of objects 1. First calls destructor for each object in array 2. Then, deallocates memory Lec 5 Programming in C++ 17 const Member Functions Member functions declared const cannot modify objects In C++, const objects can only be passed as arguments to member functions declared const See Fig07_14_16 Lec 5 Programming in C++ 18 9

Objects and Functions Passing, returning objects Objects passed as function arguments Objects returned from functions Default: pass-by-value Copy of object passed, returned Copy constructor called automatically» Copy original values into new object Time::Time(const Time &t){ hour = t.hour; minute = t.minute; second = t.second; } const Time t1(12,3,0); Time t2(t1); Copy constructor is called Lec 5 Programming in C++ 19 A copy constructor for Employee class Employee::Employee(const Employee &e) { firstname = new char[strlen(e.firstname) + 1]; strcpy(firstname, e.firstname); lastname = new char[strlen(e.last) + 1]; strcpy(lastname, e.lastname); } ++count; // increment static variable Lec 5 Programming in C++ 20 10

Using the this Pointer Allows object to access own address (refer to itself) Cascaded member function calls Multiple functions invoked in same statement Function returns reference pointer to same object { return *this; } or { return this; } Other functions operate on that pointer Time Time::setHour(int h); Time t, *T; t.sethour(18).setminute(30).setsecond(22); T->setHour(18)->setMinute(30)->setSecond(22); See Fig07_14_16 Lec 5 Programming in C++ 21 static Class Members static class variable Class-wide data Property of class, not specific object of class May seem like global variables, but have class scope Only accessible to objects of same class Initialized exactly once at file scope See Fig07_17_19 Exist even if no objects of class exist Can be public, private or protected Lec 5 Programming in C++ 22 11

static Class Members static member functions Cannot access non-static data or functions No this pointer for static functions static data members and static member functions exist independent of objects Lec 5 Programming in C++ 23 Function Templates template < typename T > // or template< class T > T maximum( T value1, T value2, T value3 ) { T max = value1; if ( value2 > max ) max = value2; if ( value3 > max ) max = value3; return max; Function calls written as usual: int max = maximum(4,3,6); } // end function template maximum Lec 5 Programming in C++ 24 12

Function Templates The compiler constructs a new function for every call and replaces T with the correct data. Called function-template specialization Standard Template Library (STL) is based on templates You can have different template parameters in functions and classes template < typename T1, T2 > T1 f(t1 a, T2 b) {...} Class templates are also possible Lec 5 Programming in C++ 25 13