The Slicing Problem CS240 Computer Science II. Some relationship between base and derived classes: data members Derived class inherits data members of.

Slides:



Advertisements
Similar presentations
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Advertisements

Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
Inheritance, Polymorphism, and Virtual Functions
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
Pointer Data Type and Pointer Variables
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
Chapter 10 Inheritance and Polymorphism
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 Polymorphism
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Memory Management in Java Mr. Gerb Computer Science 4.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Design issues for Object-Oriented Languages
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS 3370 – C++ Object-oriented Programming
Pointers, Polymorphism, and Memory Allocation
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Polymorphism.
Polymorphism & Virtual Functions
Memberwise Assignment / Initialization
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
VIRTUAL FUNCTIONS RITIKA SHARMA.
Chapter 11 Class Inheritance
Lecture 6: Polymorphism
Computer Science II for Majors
Presentation transcript:

The Slicing Problem CS240 Computer Science II

Some relationship between base and derived classes: data members Derived class inherits data members of its base class (or classes in the case of multiple inheritance). Therefore, a derived class object contains data members of it base class. The reverse is not true, i.e., the base class object does not contain additional data members defined in the derived class.

Example Based class point contains int data members x and y, its derived class circle defined an additional int data member radius. Then point p;// object p contains x and y circle c;// object c contains x, y, and radius Obviously, c is a “larger” object for it contains an additional data member.

The “Slicing” problem In the context of inheritance: when a larger circle object c is copied to a smaller point object p by value in a function call, the extra data member radius is discarded or “sliced off”: a situation known as the slicing problem. With passing by reference or through a pointer, the slicing problem does not occur because the address of the larger object instead of the larger object itself is passed to the function.

Static binding: some observations Assuming that there is a print( ) function in both the point and circle classes. point p, *pPtr = &p; circle c, *cPtr = &c; p.print( );// print function defined in point class c.print( );// print function defined in circle class pPtr->print( );// print function defined in point class cPtr->print( );// print function defined in circle class pPtr = &c;// now pPtr points to a circle obj! pPtr->print( );// still print function defined in clircle // class! Why??? Because of static // binding! At compile time, the // compiler uses the point type (class) // to generate the code!

Dynamic binding through virtual functions The whole situation is quite different if print function is declared virtual in the class (implying print will be virtual in the derived class as well). The binding of a function with the calling object occurs at run time, and the print function defined in the class (ADT) of the calling object will be activated.