Practical Programming Methodology. Michael Buro. Class Inheritance (CMPUT-201)



Similar documents
C++FA 5.1 PRACTICE MID-TERM EXAM

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

C++ Overloading, Constructors, Assignment operator

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Scan Conversion of Filled Primitives Rectangles Polygons. Many concepts are easy in continuous space - Difficult in discrete space

Object Oriented Software Design II

Advanced Data Structures

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Open_polyline. Display Engine. window. Rectangle. draw() attach() draw() attach() draw()

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Java (12 Weeks) Introduction to Java Programming Language

Object Oriented Software Design II

16 Collection Classes

Constructor, Destructor, Accessibility and Virtual Functions

Compile-time type versus run-time type. Consider the parameter to this function:

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Construction of classes with classes

C++ INTERVIEW QUESTIONS

History OOP languages Year Language 1967 Simula Smalltalk

El Dorado Union High School District Educational Services

C++ Crash Kurs. C++ Object-Oriented Programming

Chapter 13 - Inheritance

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

Java Programming Language

CIS 190: C/C++ Programming. Polymorphism

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Object Oriented Software Design II

CS193j, Stanford Handout #10 OOP 3

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

BCS2B02: OOP Concepts and Data Structures Using C++

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {


Introduction to Programming Block Tutorial C/C++

1. Polymorphism in C++...2

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C++FA 3.1 OPTIMIZING C++

The Interface Concept

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES CONSTRUCTORS AND DESTRUCTORS

Implementation Aspects of OO-Languages

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

AP Computer Science Java Subset

13 Classes & Objects with Constructors/Destructors

Chapter 13 Storage classes

explicit class and default definitions revision of SC22/WG21/N1582 = and SC22/WG21/ N

Introducing Variance into the Java Programming Language DRAFT

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

Part I. Multiple Choice Questions (2 points each):

Object Oriented Programming and the Objective-C Programming Language 1.0. (Retired Document)

Java Interview Questions and Answers

PHP Object Oriented Classes and objects

15.1 Factoring Polynomials

Building Java Programs

Hands-On Practice. Using Notebook Software in the Office

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

expression is written horizontally. The Last terms ((2)( 4)) because they are the last terms of the two polynomials. This is called the FOIL method.

EP241 Computer Programming

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

How to create pop-up menus

Konzepte objektorientierter Programmierung

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

Course Title: Software Development

An Internet Course in Software Development with C++ for Engineering Students

Compiler Construction

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

Introduction to Object Oriented Programming (OOP)

Sources: On the Web: Slides will be available on:

Object Oriented Programming

MODULE 17 POLYMORPHISM

The CLIPS environment. The CLIPS programming language. CLIPS production rules language - facts. Notes

Introduction to Object-Oriented Programming

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Glossary of Object Oriented Terms

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Introduction: Abstract Data Types and Java Review

Canterbury Maps Quick Start - Drawing and Printing Tools

Part 7: Object Oriented Databases

OpenCL Static C++ Kernel Language Extension

Inheritance in Programming Languages

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Johannes Sametinger. C. Doppler Laboratory for Software Engineering Johannes Kepler University of Linz A-4040 Linz, Austria

Introduction to Object Oriented Programming (OOP)

Subject Name: Object Oriented Programming in C++ Subject Code:

Help on the Embedded Software Block

Java CPD (I) Frans Coenen Department of Computer Science

Classes and Pointers: Some Peculiarities (cont d.)

Short Introduction to the Concepts of Programming in Java Overview over the most important constructs

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Teaching Non-majors Computer Programming Using Games as Context and Flash ActionScript 3.0 as the Development Tools

MATH 21. College Algebra 1 Lecture Notes

Welcome to CorelDRAW, a comprehensive vector-based drawing and graphic-design program for the graphics professional.

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Description of Class Mutation Mutation Operators for Java

Transcription:

Practical Programming Methodology (CMPUT-201) Lecture 16 Michael Buro C++ Class Inheritance Assignments ctor, dtor, cctor, assignment op. and Inheritance Virtual Functions Class Inheritance Object Oriented Programming Paradigm Derive new class from existing base-class(es) Inherits data and function members from baseclass(es) Code/data reuse Code adaption (make use of base-class impl.) Single inheritance (inherit from one base-class) Multiple inheritance (more than one base-class, not in Java/C#!) Lecture 16 : Overview 1 / 18 Inheritance Example Sub-class/derived class specializes super-class/base-class Usually models is-a relationship. E.g. a Rectangle is a Shape a Square is a Shape an Ellipse is a Shape a Circle is a Shape Type hierarchy Shape <---+--- Rectangle +--- Square +--- Ellipse +--- Circle Lecture 16 : C++ Class Inheritance 3 / 18 Lecture 16 : C++ Class Inheritance 2 / 18 class Shape { // base-class // all shapes have a color float area() const { return 0; // and area, too class Rectangle : public Shape { Rectangle(int xl_, int xr_, int yt_, int yb_) : xl(xl_), xr(xr_), yt(yt_), yb(yb_) { float area() const { return (xr-xl)*(yb-yt); // overrides Shape::area() int xl, xr, yt, yb; // describes Rectangle (left,right,top,bottom) // also inherits color Circle(int x_, int y_, int r_) : x(x_), y(y_), r(r_) { float area() const { return r * r * PI; // overrides Shape::area() int x, y, r; // describes a Circle, also inherits color Lecture 16 : C++ Class Inheritance 4 / 18

Inheritance Types Derived class inherits all data and function members from base-class(es) Access permissions depend on qualifiers class Y : public X {... Y is an X Sub-class Y can access public and protected members of X, cannot access private members of X class Y : protected X {... Y is implemented in terms of X public members of X become protected in Y Lecture 16 : C++ Class Inheritance 5 / 18 Assignments Across Class Hierarchy class Y : public X {... Y inherits data and function members from X Public inheritance: is-a relationship public and protected X members visible in Y X a; Y b; Assignments: a = b; or b = a; meaningful? How to implement Y assignment operator and copy constructor? X *pa; Y *pb; Assignments: pa = pb; or pb = pa; meaningful? Lecture 16 : Assignments 7 / 18 class X { int a; // visible to all: users of X, void fa(); // X itself, and derived classes protected: int b; // visible to derived classes & X, void fb(); // but not to users of class X! int c; // only visible to member functions void fc(); // of X class Y : public X // Y "is an" X { void foo() { a = 0; fa(); // OK b = 0; fb(); // OK c = 0; fc(); // NOT OK! int main() { X x; Y y; x.a = 0; // OK y.a = 0; // OK x.b = 0; // NOT OK x.c = 0; // NOT OK Lecture 16 : C++ Class Inheritance 6 / 18 Object Assignment class Y : public X {... X a; Y b; a = b; // OK - but slicing! assignment operator is called with reference to b X-parts of b are copied to a, Y parts are not b = a; // not OK Y can contain more data than X How to fill the rest? Y assignment op. and copy constructor can make use of X operators Lecture 16 : Assignments 8 / 18

Pointer Assignment Reusing Base-Class Operators class Y : public X {... X *pa; Y *pb; pa = &b; or pa = pb; // OK pa now points to b, or *pb respectively information about Y is not available when accessing *pa pb = &a; or pb = pa; // not OK *pb is object of type Y again, where would the additional data come from? struct X { int x; X() { x = 0; struct Y : public X { int y; Y() { y = 0; Y(const Y &a) : X(a) { y = a.y; Y &operator=(const Y &a) { X::operator=(a); y = a.y; return *this; // X copy constructor, copy X-part // copy Y-part // X assignment operator, copy X-part // copy Y-part X a, *pa; Y b; a = b; pa = &b; // a.x = b.x; b.y not copied (object "slicing") // OK, *pa is object of type X. Y-parts invisible Lecture 16 : Assignments 9 / 18 Inheritance and Constructors class X { X(int a_=0) {... class Y : public X { Y() { /* X() is called here */... Y(int b_) : X(b_) {... // explicit X(int) call Base-class constructors, copy constructors, and assignment operators are not inherited! Derived class constructor calls the base-class constructor first to initialize base-class members If ommitted, the default derived class constructor is the base-class constructor Lecture 16 : Inheritance and Contructors/Destructors 11 / 18 Lecture 16 : Assignments 10 / 18 Inheritance and Destructors struct X { // struct = class... int *p; X() { p = new int[100]; ~X() { delete [] p; struct Y : public X { int *q; Y() { /*X() called here*/ q = new int[200]; ~Y(){ delete [] q; /* ~X() called here*/ Are called in reverse order of constructor calls Derived class destructor ~Y() calls base-class destructor ~X() at the end ~Y() only deals with resources allocated in Y! ~X() takes care of the rest Lecture 16 : Inheritance and Contructors/Destructors 12 / 18

Using Inheritance: Graphics Example Class Graphics contains a list of pointers to objects to be drawn: Circles, Rectangles,... 1st solution: Objects contain an id to identify type class Shape { int type_id; enum { CIRCLE, RECTANGLE, TRIANGLE,... int x,y,r; Circle() { x=y=r=0; type_id = CIRCLE; void draw(screen *s) const {... Lecture 16 : Virtual Functions 13 / 18 Virtual Functions For a base-class pointer, execute member functions in the current object context: Shape *p; p = new Circle; p->draw(); Would be nice if this calls Circle::draw! Polymorphism: same function name, different action Requires that objects know their type! Solution: Virtual Functions Lecture 16 : Virtual Functions 15 / 18 class Graphics { void draw() { // draw all objects for (int i=0; i < n_objs; ++i) { Shape *p = objs[i]; switch(p->type_id) { case CIRCLE: static_cast<circle*>(p)->draw(screen); break; case RECTANGLE: static_cast<rectangle*>(p)->draw(screen); break;... Shape **objs; // array of pointers to Shapes int n_objs; // number of objects Screen *screen; Problems: slow, need to change code when adding new shapes, hard to maintain Lecture 16 : Virtual Functions 14 / 18 Graphics 2 class Shape { // abstract base class virtual void draw(screen *s) const = 0; //abstract // = 0: derived classes must implement function Circle() { x = y = r = 0; void draw(screen *s) const {... // implements // virtual function int x,y,r; Second solution: virtual function draw Keyword virtual indicates that the function in sub-classes is accessible via base-class pointers Lecture 16 : Virtual Functions 16 / 18

class Graphics { void draw() { // draw all objects for (int i=0; i < n_objs; ++i) { objs[i]->draw(screen); Shape **objs; // array of pointers to Shapes int n_objs; // number of objects Screen *screen; No type_id, no switch. Faster and easy to maintain Type of *objs[i] known at runtime the correct draw function can be called. HOW? Lecture 16 : Virtual Functions 17 / 18 Virtual Function Implementation New data-member is added to class variables: pointer to virtual function table (VFTP) One virtual function table is created for each class The virtual function table contains addresses of virtual functions Two stage access: Shape *p; p->draw(screen); replaced by (*p->vftp[c_draw])(p, screen); Circle x; Virtual Function Table for Circle Circle::draw _VFTP -----------> draw --> ---- _color ----... ---- Lecture 16 : Virtual Functions 18 / 18