A <Basic> C++ Course

Similar documents
A <Basic> C++ Course

A <Basic> C++ Course

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

Polymorphism. Zimmer CSCI 330

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

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

C++ Programming: Polymorphism

04-24/26 Discussion Notes

C++ Inheritance and Encapsulation

Increases Program Structure which results in greater reliability. Polymorphism

Inheritance, Polymorphism and the Object Memory Model

Inheritance and Polymorphism

15: Polymorphism & Virtual Functions

Inheritance and Polymorphism

VIRTUAL FUNCTIONS Chapter 10

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

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

Polymorphism Part 1 1

COMP322 - Introduction to C++

G Programming Languages - Fall 2012

C++ Important Questions with Answers

Object Oriented Programming with c++ Question Bank

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

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

OBJECT ORIENTED PROGRAMMING USING C++

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

Absolute C++ Walter Savitch

Programming Languages: OO Paradigm, Polymorhism and Class Members

Logistics. Templates. Plan for today. Logistics. A quick intro to Templates. A quick intro to Templates. Project. Questions? Introduction to Templates

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

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

A <Basic> C++ Course

Subtyping (Dynamic Polymorphism)

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

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

Pointers and References

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

Polymorphism. Arizona State University 1

Object Oriented Programming. Solved MCQs - Part 2

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

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

C++ Inheritance II, Casting

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

G52CPP C++ Programming Lecture 13

Midterm Exam 5 April 20, 2015

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

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

Where do we stand on inheritance?

C++ (classes) Hwansoo Han

Object-Oriented Programming

Object Oriented Software Design II

COEN244: Polymorphism

Object Oriented Software Design II

1. An object of a class can be treated as an object of its corresponding class.

A <Basic> C++ Course. Julien DeAntoni. 7 - The document example. adapted from Jean-Paul Rigault courses

G52CPP C++ Programming Lecture 12

A <Basic> C++ Course. Julien Deantoni. 7 - The document example. adapted from Jean-Paul Rigault courses

COMP6771 Advanced C++ Programming

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

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

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

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

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

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

A <Basic> C++ Course

XII CS(EM) Minimum Question List N.KANNAN M.Sc., B.Ed COMPUTER SCIENCE IMPORTANT QUESTION (TWO MARKS) CHAPTER 1 TO 5 ( STAR OFFICE WRITER)

C++ Yanyan SHEN. slide 1

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

Chapter 15. Object-Oriented Programming

OOP THROUGH C++(R16) int *x; float *f; char *c;

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2015 EOM, HYEONSANG ALL RIGHTS RESERVED

IS 0020 Program Design and Software Tools

Object-Oriented Programming, Iouliia Skliarova

Introduction to Move Semantics in C++ C and C

What are the characteristics of Object Oriented programming language?

Short Notes of CS201

CSE 333 Lecture 15 - inheritance

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

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

Advanced C++ Topics. Alexander Warg, 2017

Data Abstraction. Hwansoo Han

Overriding Variables: Shadowing

Laboratorio di Tecnologie dell'informazione

CS201 - Introduction to Programming Glossary By

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

ECE 3574: Dynamic Polymorphism using Inheritance

Template Issue Resolutions from the Stockholm Meeting

Instantiation of Template class

CS11 Advanced C++ Fall Lecture 7

Review and Recursion

ECE 2400 Computer Systems Programming Fall 2017 Topic 14: C++ Inheritance

Chapter 5. Object- Oriented Programming Part I

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

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Introduction to Programming Using Java (98-388)

Dynamic Binding C++ Douglas C. Schmidt

Transcription:

adapted from Jean-Paul Rigault courses 1 A <Basic> C++ Course 8 Object-oriented programming 2 Julien Deantoni

2 Outline Dynamic Typing Truncature Cast

3 Variants of class Paragraph Definition of derived classes (1) We wish to have several sorts of paragraphs titles, sections, enumerations, items... We want to share as much as possible the common properties contents as a string possibility to (crude lay out) But specific properties should be possible numbering, bullets... page layout

4 Variants of class Paragraph Definition of derived classes (5) A derived class may add new properties data members member-functions friend functions A derived class may redefine (override) some inherited memberfunctions Derivation depth is unlimited Single and multiple inheritance Single: only one base class Multiple: several distinct base classes

If (int, int) is NON virtual 5

Paragraph::Paragraph(std::string title std::string content) { _title = title; _content = content; } If (int, int) is NON virtual 6

content depth Paragraph::Paragraph(std::string title std::string content) { _title = title; _content = content; } If (int, int) is NON virtual 7

Paragraph::Paragraph(std::string title std::string content) { _title = title; _content = content; } If (int, int) is NON virtual 8

Item_Paragraph::Item_Paragraph(string title, string content, char b) :Paragraph(title,content) { _bullet = b; } If (int, int) is NON virtual and is not redefined in Item_Paragraph 9

ip:item_paragraph content depth Item_Paragraph::Item_Paragraph(string title, string content, char b) :Paragraph(title,content) { _bullet = b; } If (int, int) is NON virtual and is not redefined in Item_Paragraph 10

ip:item_paragraph content depth Bullet Item_Paragraph::Item_Paragraph(string title, string content, char b) :Paragraph(title,content) { _bullet = b; } If (int, int) is NON virtual and is not redefined in Item_Paragraph 11

ip:item_paragraph Bullet Item_Paragraph::Item_Paragraph(string title, string content, string b) :Paragraph(title,content) { _bullet = b; } If (int, int) is NON virtual and is not redefined in Item_Paragraph 12

ip:item_paragraph Bullet='#' Item_Paragraph::Item_Paragraph(string title, string content, string b) :Paragraph(title,content) { _bullet = b; } If (int, int) is NON virtual and is not redefined in Item_Paragraph 13

ip:item_paragraph Bullet='#' If (int, int) is NON virtual and is REDEFINED in Item_Paragraph 14

ip:item_paragraph Bullet='#' p.(); ip.(); If (int, int) is NON virtual and is REDEFINED in Item_Paragraph 15

ip:item_paragraph Bullet='#' p.(); ip.(); What if we create a vector of Paragraphs?? If (int, int) is NON virtual and is REDEFINED in Item_Paragraph 16

vp:vector ip:item_paragraph Bullet='#' vector<paragraph> vp; If (int, int) is NON virtual and is REDEFINED in Item_Paragraph 17

vp:vector pbis:paragraph ip:item_paragraph Bullet='#' vector<paragraph> vp; vp.push_back(p); If (int, int) is NON virtual and is REDEFINED in Item_Paragraph 18

vp:vector pbis:paragraph iptrunc:paragraph ip:item_paragraph Bullet='#' vector<paragraph> vp; vp.push_back(p); vp.push_back(ip); If (int, int) is NON virtual and is REDEFINED in Item_Paragraph 19

vp:vector pbis:paragraph iptrunc:paragraph ip:item_paragraph Bullet='#' vector<paragraph> vp; vp.push_back(p); vp.push_back(ip); We lost the specificities of Item_Paragraph If (int, int) is NON virtual and is REDEFINED in Item_Paragraph 20

vp:vector pbis:paragraph iptrunc:paragraph ip:item_paragraph Bullet='#' vector<paragraph> vp; vp.push_back(p); vp.push_back(ip); We lost the specificities of Item_Paragraph... Forever! If (int, int) is NON virtual and is REDEFINED in Item_Paragraph 21

vptr_p:vector ip: Item_Paragraph Bullet='#' vector<paragraph*> vptr_p What happens if we create a vector of paragraph pointer? If (int, int) is NON VIRTUAL and is redefined in Item_Paragraph 22

vptr_p:vector :paragraph* :paragraph* ip: Item_Paragraph Bullet='#' vector<paragraph*> vptr_p vptr_p.push_back(&p); vptr_p.push_back(&ip); If (int, int) is NON VIRTUAL and is redefined in Item_Paragraph 23

vptr_p:vector :paragraph* :paragraph* ip: Item_Paragraph Typed by Paragraph content depth Bullet='#' vector<paragraph*> vptr_p vptr_p.push_back(&p); vptr_p.push_back(&ip); A call to a non-virtual function is resolved according to the static type of the hidden parameter If (int, int) is NON VIRTUAL and is redefined in Item_Paragraph 24

Paragraph vtable p.(); If (int, int) is VIRTUAL and is REDEFINED in Item_Paragraph 25

Paragraph vtable ip:item_paragraph Bullet='#' Item_Paragraph vtable p.(); ip.(); If (int, int) is VIRTUAL and is REDEFINED in Item_Paragraph 26

Paragraph vtable ip:item_paragraph Bullet='#' Item_Paragraph vtable p.(); ip.(); What happens if we create a vector of Paragraphs?? If (int, int) is VIRTUAL and is REDEFINED in Item_Paragraph 27

vp:vector pbis: Paragraph Paragraph vtable iptrunc:paragraph ip:item_paragraph Bullet='#' Item_Paragraph vtable vector<paragraph> vp; vp.push_back(p); vp.push_back(ip); We lost the specificities of Item_Paragraph If (int, int) is VIRTUAL and is REDEFINED in Item_Paragraph 28

vptr_p:vector Paragraph vtable ip:item_paragraph Bullet='#' Item_Paragraph vtable vector<paragraph*> vptr_p What happens if we create a vector of paragraph pointer? If (int, int) is VIRTUAL and is REDEFINED in Item_Paragraph 29

vptr_p:vector :paragraph* Paragraph vtable ip:item_paragraph Bullet='#' Item_Paragraph vtable vector<paragraph*> vptr_p vptr_p.push_back(&p); If (int, int) is VIRTUAL and is REDEFINED in Item_Paragraph 30

vptr_p:vector :paragraph* :paragraph* Paragraph vtable ip:item_paragraph Bullet='#' A call to a virtual function is resolved according to the type dynamic type of the hidden parameter Item_Paragraph vtable vector<paragraph*> vptr_p vptr_p.push_back(&p); vptr_p.push_back(&ip); If (int, int) is VIRTUAL and is REDEFINED in Item_Paragraph 31

vptr_p:vector :paragraph* :paragraph* :paragraph* ip:item_paragraph Bullet='#' :Paragraph Paragraph vtable Item_Paragraph vtable vector<paragraph*> vptr_p vptr_p.push_back(&p); vptr_p.push_back(&ip); vptr_p.push_back(new Paragraph(ip)); If (int, int) is VIRTUAL and is REDEFINED in Item_Paragraph 32

vptr_p:vector :paragraph* :paragraph* :paragraph* ip:item_paragraph Bullet='#' :Paragraph Paragraph vtable Item_Paragraph vtable vector<paragraph*> vptr_p vptr_p.push_back(&p); vptr_p.push_back(&ip); vptr_p.push_back(new Paragraph(ip)); You have to use the clone() virtual function (course 8-OO.pdf) If (int, int) is VIRTUAL and is REDEFINED in Item_Paragraph 33

34 Abstract classes and pure virtual functions Object is abstract It cannot be instantiated Some of its behaviors cannot be defined at least one of its member function is a pure virtual function

35 Abstract classes and pure virtual functions Object is abstract It cannot be instantiated Some of its behaviors cannot be defined at least one of its member function is a pure virtual function pure virtual functions

36 Safe downward cast (1) Downward cast may be dangerous class A {... }; class B : public A { void f() {... } }; // f() not defined in A

37 Safe downward cast (1) Downward cast may be dangerous class A {... }; class B : public A { void f() {... } }; // f() not defined in A A* pa = new B(); // OK pa:a* :B f()

38 Safe downward cast (1) Downward cast may be dangerous class A {... }; class B : public A { void f() {... } }; // f() not defined in A A* pa = new B(); // OK pa >f(); pa:a* :B f()

39 Safe downward cast (1) Downward cast may be dangerous class A {... }; class B : public A { void f() {... } }; // f() not defined in A A* pa = new B(); // OK pa >f(); // KO Typed by pa:a* A :B f()

40 Safe downward cast (1) Downward cast may be dangerous class A {... }; class B : public A { void f() {... } }; // f() not defined in A A* pa = new B(); // OK pa >f(); // KO ((B*)pa) >f(); // OK casted and then typed by (B*)pa:B* pa:a* B f() :B f()

41 Safe downward cast (1) Downward cast may be dangerous class A {... }; class B : public A { void f() {... } }; // f() not defined in A A* pa = new A(); // OK pa >f(); // KO ((B*)pa) >f(); // OK casted and then typed by (B*)pa:B* pa:a* :B f() :B :A f() Unsafe!!

42 Safe downward cast (1) Downward cast may be dangerous class A {... }; class B : public A { void f() {... } }; // f() not defined in A B* pa = new A(); // KO pa:b* pa:a* :B :A f()

43 Safe downward cast (1) Downward cast may be dangerous class A {... }; class B : public A { void f() {... } }; // f() not defined in A A* pa = new B(); // OK pa >f(); // KO ((B*)pa) >f(); // OK casted and then typed by pa:a* :B :B f() static_cast<b*>(pa) >f();// OK Unsafe!!

44 Safe downward cast (2) Operator dynamic_cast B *pb = dynamic_cast<b*>(pa); if (pb!= nullptr) { pb >f(); // OK and safe }

45 Safe downward cast (2) Operator dynamic_cast B *pb = dynamic_cast<b*>(pa); if (pb!= nullptr) pb >f(); // OK and safe try { dynamic_cast<b*>(pa) >f(); } catch(bad_cast) { cerr << "bad conversion" << endl; }

46 Safe downward cast (3) Limitation of dynamic_cast Work only on classes with virtual functions (polymorphic types) Invoking dynamic_cast from a constructor or a destructor dynamic_cast behaves like a virtual function it is statically bound in a constructor or a destructor