Inheritance & Destructors

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
CSE 332: C++ copy control II Copy Control (Part II) Review: copy control consists of 5 distinct operations –A copy constructor initializes an object by.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
CSCI-383 Object-Oriented Programming & Design Lecture 21.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle Circle & Rectangle could use toString() but chose.
Learners Support Publications Constructors and Destructors.
A First Book of C++ Chapter 12 Extending Your Classes.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Stacks. Stack ADT where we can only work with "top" – Top() : get value on top – Pop() : remove value on top – Push(value) : put new value on top.
Chapter 2 Objects and Classes
Constructors and Destructors
Memory Management with Classes
QUESTION AND ANSWERS On C++.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Programming (OOP) Lecture No. 45
CSC241: Object Oriented Programming
Inheritance and Run time Polymorphism
Constructors & Destructors.
Constructor & Destructor
Chapter 2 Objects and Classes
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance Constructors & Overriden Functions
Foundational Data Structures
The Big Three If you need one of them, you need all of them
The Assignment Operator
Constructors and destructors
Constructors and Destructors
Array-Based Implementations
Destruction and Copying
Indirection.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
CISC/CMPE320 - Prof. McLeod
Jeff West - Quiz Section 12
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Object-Oriented Programming (OOP) Lecture No. 22
CISC/CMPE320 - Prof. McLeod
Essential Class Operations
Destructor CSCE 121 J. Michael Moore.
Destructor CSCE 121.
Review Chapter 10 PPT for full coverage.
Destruction and Copying
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Inheritance & Destructors
COP 3330 Object-oriented Programming in C++
Essential Class Operations
Destructors, Copy Constructors & Copy Assignment Operators
Inheritance & Destructors
Rule of Three Part 1 & 2.
Destructors, Copy Constructors & Copy Assignment Operators
Rule Of Three Part 3.
Classes Class Data Members & Initializers
SPL – PS4 C++ Advanced OOP.
Presentation transcript:

Inheritance & Destructors

Constructing/Destructing Object built/destroyed in layers Base class constructed Derived class constructed Geometric Object Circle

Constructing/Destructing Object built/destroyed in layers Base class constructed Derived class constructed Derived class destroyed – clean up after itself Geometric Object

Constructing/Destructing Object built/destroyed in layers Base class constructed Derived class constructed Derived class destroyed – clean up after itself Base class destroyed – clean up after itself

Example Simple list has array of ints LessSimpleList is a SimpleList with one extra char on heap Each new has a matching delete

Example Base created Derived created Derived destroyed Base destroyed 1 5 3 2 4

Example Base created Derived created Derived destroyed Base destroyed 1 5 3 2 4

Copy Constructor Issue No copy constructor = trouble:

Copy Constructor Issue Copying child class defaults to no-arg construction of parent:

Copy Constructor Fix Child copy ctor should explicitly call parent one

Assignment Operator Assignment operator also needs to call parent version:

Virtual Messiness Also need to worry about virtual dispatch to handle: SimpleList* list = new LessSimpleList(); delete list; Fixes: Virtual destructor Child classes need to override parent Copy/Assignment https://stackoverflow.com/questions/669818/virtual-assignment-operator-c