G52CPP C++ Programming Lecture 13

Similar documents
G52CPP C++ Programming Lecture 12

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 16

G52CPP C++ Programming Lecture 20

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

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

Fast Introduction to Object Oriented Programming and C++

Short Notes of CS201

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

CS201 - Introduction to Programming Glossary By

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

Introduction Of Classes ( OOPS )

AN OVERVIEW OF C++ 1

G52CPP C++ Programming Lecture 15

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

Object-Oriented Programming

Object-Oriented Programming, Iouliia Skliarova

Polymorphism Part 1 1

G52CPP C++ Programming Lecture 17

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

C++ Important Questions with Answers

COMP 2355 Introduction to Systems Programming

6.096 Introduction to C++ January (IAP) 2009

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

04-19 Discussion Notes

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

CPSC 427: Object-Oriented Programming

Object oriented programming with C++

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

CSE 303: Concepts and Tools for Software Development

Object Oriented Software Design II

Object Oriented Software Design II

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

PIC 10A Objects/Classes

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

A brief introduction to C++

C++ 8. Constructors and Destructors

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

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

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

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

C++ Inheritance and Encapsulation

Financial computing with C++

Computer Programming Inheritance 10 th Lecture

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

CS304 Object Oriented Programming Final Term

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Interview Questions of C++

C++FA 6.1 PRACTICE FINAL EXAM

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Pointers, Dynamic Data, and Reference Types

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

04-24/26 Discussion Notes

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

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

Object Oriented Design

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

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

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

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

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

Object-Oriented Programming, Iouliia Skliarova

CS250 Final Review Questions

CE221 Programming in C++ Part 1 Introduction

Inheritance, Polymorphism and the Object Memory Model

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

Recharge (int, int, int); //constructor declared void disply();

CS 247: Software Engineering Principles. ADT Design

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

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Special Member Functions

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

2 ADT Programming User-defined abstract data types

Where do we go from here?

Object-Oriented Programming for Scientific Computing

CS250 Final Review Questions

15: Polymorphism & Virtual Functions

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

A <Basic> C++ Course

10. Object-oriented Programming. 7. Juli 2011

CSE 401/M501 Compilers

Programming in C and C++

A <Basic> C++ Course

Instantiation of Template class


Inheritance, and Polymorphism.

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

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

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

Evolution of Programming Languages

CPSC 427: Object-Oriented Programming

Object Oriented Software Design II

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

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

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

Transcription:

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

Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and vptr Virtual functions are slower to call, more work 2

What to know about vpointers 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. Retrieve the vtable (following the vpointer) 3. Look up which function to call from index 4. Call the function 3

Example: virtual functions #include <cstdio> class BaseClass public: virtual char* bar() return "BaseBar"; char* bar2() return this->bar(); ; class SubClass : public BaseClass public: char* bar() return "SubBar "; ; BaseClass vtable: BaseClass::bar() SubClass vtable: SubClass::bar() Virtual-ness is inherited Calling a function from a base class function is like using a base class pointer to call it 4

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 Do not expect to change functionality 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 5

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! 6

This lecture Automatically created methods: Default Constructor Copy Constructor Assignment operator Destructor Conversion constructors Conversion Operators Friends 7

Reminders about object creation 8

Reminders If you want to create objects in dynamic memory then you must go through new (NOT malloc()) You cannot construct correct objects yourself e.g. You cannot set the hidden data, e.g. vpointer to vtable You cannot call the constructor manually Function overrides Provide an alternative implementation in a sub-class Non-virtual functions : type of pointer or calling function determines which function to call Virtual functions : type of object the function applies to determines the function to call Has to look up which function to call (e.g. in vtable?) You can use new on basic types (e.g. int) By default they are NOT initialised Array new [] uses default constructor for objects, and does not initialise basic types 9

Where to put objects? Objects can be created on the stack MyClass ob1; // Use default constructor MyClass ob2(3); // Provide initial value MyClass obarray[4]; // Array of 4 elements Or in dynamic memory MyClass* pob1 = new MyClass; MyClass* pob2 = new MyClass(5); MyClass* pobarray = new MyClass[6]; In which case they need deleting delete pob1; delete pob2; delete [] pobarray; A good rule of thumb (or heuristic): create things on the stack if you can, so that you don t need to worry about deleting them So, when the stack frame is destroyed, the objects will be destroyed 10

Default member functions 11

Automatically generated functions 4 functions created by default if needed You can make them unavailable (e.g. private) Or change their behaviour If they are needed, you will get: 1. A default constructor (no parameters needed) 2. A copy constructor (copy one object to another) 3. An assignment operator ( = operator ) We will see general operator overloading later 4. A destructor 12

1: A default constructor A constructor which takes no parameters Automatically created if and only if you do NOT create any other constructors If you create any constructor, compiler will not create a default one for you The generated default constructor is empty Does nothing: lets members construct themselves (using their default constructors) This is why you can still create objects, even when classes appear to have no constructors To prevent this, create your own (private?) 13

2: The Copy Constructor The copy constructor is used to initialise one object from another of the same type This includes when a copy is implicitly made: Passing object as a parameter into a function Returning object by value from a function A copy constructor is created by default Unless you create your own Default behaviour copies each member in turn i.e. calls copy constructor for each member Note: To avoid having a copy constructor, declare a private one without implementation (so the linker causes an error if it is used) 14

Creating a copy constructor You can define your own copy constructor, for example: MyClass( const MyClass& rhs ) Takes a constant reference to the object to copy from (or a non-constant reference) Has to be a reference! (to avoid needing to copy) If not a reference then copying parameter value (to pass in) means copying the object i.e. would need to have copy constructor to implement the copy constructor 15

All of these are initialisation All five of these are initialisation: MyClass ob1( 1, 2, 3 ); MyClass ob2 = MyClass( 1, 2, 3 ); MyClass ob3( ob2 ); MyClass ob4 = ob2; MyClass ob5 = MyClass( ob2 ); Identical: call (int,int,int) constructor Identical: call (const MyClass&) constructor First two are same. Last three are same. The last three all use copy constructor! Why? Because it is defined in the standard to be so It is faster to initialise than initialise+assign 16

Example : Copy(ish) constructor class Example public: Example( int ival = 1) : m_ival(ival) // WARNING not exact copy! Example( const Example& rhs) : m_ival(rhs.m_ival+1) int main() Example eg1; Example eg2(2); // Initialisation: Example eg3 = eg2; Example eg4; // Assignment eg4 = eg2; void print() printf("%d\n", m_ival); private: int m_ival; ; eg1.print(); eg2.print(); eg3.print(); eg4.print(); return 0; 17

3: Assignment operator Used when value of one object is assigned to another Assignment operator will be created by default, if needed Unless you create one yourself Default one does member-wise assignment i.e. calls assignment operator for each member To prevent this, declare private one without implementation Create your own using operator overloading: MyClass& operator=( const MyClass& rhs ) /* Assign the members here */ return *this; Takes a reference to the one we are getting values from Returns a reference to *this, so we can chain these e.g.: ob1 = ob2 = ob3 = ob4; 18

Example : Assignment operator class Example public: Example( int ival = 1) : m_ival(ival) Example& operator=( const Example& rhs) m_ival = rhs.m_ival + 10; return *this; int main() Example eg5(4); Example eg6(5); Example eg7(6); // Assignment eg7 = eg6 = eg5; eg5.print(); eg6.print(); eg7.print(); void print() printf("%d\n", m_ival); private: int m_ival; ; return 0; 19

4: Destructor A destructor is created if you do not create one yourself Default destructor does nothing Member destructors get called as members get destroyed Destructors for objects are called Basic data types (e.g. int) just get destroyed No need for destructors Pointers just get destroyed The thing they point to will NOT! 20

A default implementation class MyClass public: // Constructor MyClass() // Destructor ~MyClass() ; // Copy constructor MyClass( const MyClass& rhs ) // Initialise each member : i( rhs.i ) // Assignment operator MyClass& operator=( const MyClass& rhs ) // Copy each member return *this; 21

General rule (rule of three) If you need to create one of: a copy constructor or an assignment operator then you probably need to create the other, plus a destructor as well Decide: Do you need to implement them? If you control resources (or memory on the heap that you need to free) then you probably do Decide: Should users be able to copy the objects at all and, if so, then will the default copy mechanism be adequate? 22

Conversion constructors 23

Reminder: implicit functions class MyClass private: int i; public: // Constructor MyClass() // Destructor ~MyClass() // Copy constructor MyClass( const MyClass& rhs ) // Initialise each member : i( rhs.i ) // Assignment operator MyClass& operator=( const MyClass& rhs ) // Copy each member i = rhs.i; return *this; ; 24

Conversion constructor A conversion constructor is a constructor with one parameter. e.g. Constructor for MyClass: MyClass( char c ) do something with c Then you can use the following code: MyClass ob = h ; Conversion constructor converts from one type of object to another Can be used implicitly to convert between types (unless you say otherwise) The conversion constructor is very similar to the copy constructor, i.e. MyClass( const MyClass& rhs ) Copy the members 25

Conversion constructor class Converter public: // Conversion constructor // Convert INTO this class Converter( int i = 4 ); private: int _i; ; int main() int i = 4; // Construction from int Converter c1(5); Converter c2 = i; // Conversion constructor Converter::Converter(int i) : _i(i) // Set value cout << "Constructing from int\n"; 26

Forcing explicit construction Providing a one-parameter constructor provides a conversion constructor This allows compiler to use it to convert to the type whenever it wants/needs to do so To avoid this, use the keyword explicit Constructor can then ONLY be used explicitly class MyClass public: explicit MyClass( int param ); ; 27

Example of explicit struct MyClass MyClass( int ); ; struct defaults to public MyClass::MyClass( int i ) cout << "Constructor M " << i << endl; struct ExplicitClass explicit ExplicitClass( int ); ; ExplicitClass:: ExplicitClass( int i ) cout << "Constructor E " << i << endl; int main() // Call constructor MyClass m1(1); MyClass m7 = 5; // Call constructor ExplicitClass e1(100); // Cannot do this: ExplicitClass e7 = 300; 28

Next Lecture Inheritance and constructors Virtual destructors Namespaces and scoping Some standard class library classes String Input and output Container classes 29