Inheritance & Dynamic Memory Allocation Suppose base class uses dynamic memory allocation. If derived class doesn’t use dynamic memory allocation then.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
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.
Operator Overloading Fundamentals
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
Dynamic Memory for Class #include #ifndef STRCLASS_H_ #define STRCLASS_H_ class strclass { private: char *str; int len; static int num_strings; public:
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.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Templates and Polymorphism Generic functions and classes
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Operator Overloading ~ and User Defined Conversions.
Inheritance One of the most powerful features of C++
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Inheritance ITK 169 Fall 2003 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.
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.
1 CS161 Introduction to Computer Science Topic #16.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Computer Science Department Inheritance & Polymorphism.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
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.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Strings: C-strings vs. Strings as Objects
Overloading Operator MySting Example
Programming with ANSI C ++
CSC241: Object Oriented Programming
CISC181 Introduction to Computer Science Dr
Inheritance & Polymorphism
LinkedList Class.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Classes with Dynamically Allocated Data
Strings: C-strings vs. Strings as Objects
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Constructors and Destructors
Inheritance: The Fundamental Functions
Object-Oriented Programming (OOP) Lecture No. 22
Chapter 19 - Inheritance Outline 19.1 Introduction
Review: C++ class represents an ADT
CMSC 341 C++ and OOP.
Destructors, Copy Constructors & Copy Assignment Operators
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
Destructors, Copy Constructors & Copy Assignment Operators
C++ support for Object-Oriented Programming
Presentation transcript:

Inheritance & Dynamic Memory Allocation Suppose base class uses dynamic memory allocation. If derived class doesn’t use dynamic memory allocation then no further step is needed, otherwise we should do sth for it.

class baseDMA { private : char * label; int rating; public : baseDMA ( const char *l=“null”, int r=0); baseDMA (const baseDMA &rs); virtual ~baseDMA(); baseDMA & operator= (const baseDMA & rs); … } ;

class lacksDMA: public baseDMA { private : char color [40]; public : … }; If we don’t define destructor, the compiler defines the default one which calls the base-class destructor after executing its own code.

Copy constructor does memberwise copying which is fine for lacksDMA member. The same for assignment operator.

class baseDMA { private : char * label; int rating; … } class hasDMA : public baseDMA { private : char * style; public : …. } baseDMA::~ baseDMA() { delete [ ] label; } hasDMA::~ hasDMA() { delete [ ] style; }

baseDMA:: baseDMA(const baseDMA &rs) { label=new char[strlen(rs.label)+1] strcpy(label, rs.label); rating=rs.rating; } hasDMA(const hasDMA &hs) : baseDAM(hs) { style=new char[strlen(hs.style)+1] strcpy(label, hs.style); } // the baseDMA constructor uses the baseDMA portion of the hasDMA argument to construct the basDMA portion of the new object;

baseDMA & baseDMA::operator=(const baseDMA &rs) { if (this == &rs ) return *this; delete [ ] label; label=new char[strlen(rs.label)+1]; strcpy(label, rs.label); rating=rs.rating; return *this; }

hasDMA & hasDMA::operator=(const hasDMA &hs) { if (this == &hs ) return *this; baseDMA :: operator=(hs); //copy base portion style=new char[strlen(hs.style)+1]; strcpy(style, hs.style); return *this; }

#ifndef DAM_H_ #define DAM_H_ class baseDMA { private : char * label; int rating; public : baseDMA ( const char *l="null", int r=0); baseDMA (const baseDMA &rs); virtual ~baseDMA(); baseDMA &operator=(const baseDMA & rs); friend ostream &operator<<(ostream &os, const baseDMA &rs); } ;

class lacksDMA : public baseDMA { private : enum{ COL_LEN =40}; char color[COL_LEN]; public : lacksDMA (const char *c ="black", const char *l ="null", int r=0); lacksDMA (const char *c, const baseDMA &rs ); friend ostream & operator << ( ostream &os, const lacksDMA &rs); };

class hasDMA : public baseDMA { private : char * style; public : hasDMA (const char *s ="none", const char *l ="null", int r=0); hasDMA (const char *s, const baseDMA &rs ); hasDMA (const hasDMA &rs ); ~hasDMA(); hasDMA & operator=(const hasDMA & rs); friend ostream & operator << ( ostream &os, const hasDMA &rs); }; #endif

#include #include "dam.h" #include baseDMA:: baseDMA(const char *l, int r) { label=new char[strlen(l)+1]; strcpy(label, l); rating=r; } baseDMA:: baseDMA(const baseDMA &rs) { label=new char[strlen(rs.label)+1]; strcpy(label, rs.label); rating=rs.rating; }

baseDMA :: ~baseDMA () { delete [ ] label; } baseDMA & baseDMA::operator=(const baseDMA &rs) { if (this == &rs ) return *this; delete [ ] label; label=new char[strlen(rs.label)+1]; strcpy(label, rs.label); rating=rs.rating; return *this; }

ostream &operator << (ostream &os, const baseDMA &rs) { os<<"label "<<rs.label<<endl; os<<"rating "<<rs.rating<<endl; return os; } lacksDMA:: lacksDMA (const char *c, const char *l, int r) : baseDMA(l,r) { strncpy(color, c, 39); color[39]='\0'; }

lacksDMA:: lacksDMA (const char *c, const baseDMA & rs) : baseDMA(rs) { strncpy(color, c, COL_LEN-1); color[COL_LEN-1]=‘\0’; } ostream &operator << (ostream &os, const lacksDMA &ls) { os<<(const baseDMA &) ls; os<<“color: “<<ls.color<<endl; return os; }

Friend function of lacksDMA has access to color ( member of lacksDMA) but this friend function has no access to to label of baseDMA, so we use type casting. We use base portion of hs to call friend function of baseDMA ( here cout<<).

hasDMA:: hasDMA (const char *s, const char *l, int r) : baseDMA (l,r) { style=new char [strlen(s)+1]; strcpy(style,s); } hasDMA:: hasDMA (const char *s, const baseDMA &rs) : baseDMA (rs) { style=new char [strlen(s)+1]; strcpy(style,s); }

hasDMA:: hasDMA(const hasDMA &hs ) : baseDMA(hs) // call base class copy constructor { style=new char [strlen(hs.style)+1]; strcpy(style,hs.style); } hasDMA::~ hasDMA() { delete [ ] style; }

hasDMA & hasDMA::operator=(const hasDMA &hs) { if (this == &hs ) return *this; baseDMA::operator=(hs); // copy base portion style=new char[strlen(hs.style)+1]; strcpy(style, hs.style); return *this; }

ostream &operator << (ostream &os, const hasDMA &hs) { os<<(const baseDMA &) hs; os<<"Style: "<<hs.style<<endl; return os; }

#include #include “dam.h” int main() { baseDMA shirt(“Portability”, 8); lacksDMA balloon( “red”, “Blimpo”,4); hasDMA map(“Mercator”, “Buffalo Keys”,5); cout<<shirt<<endl; cout<<balloon<<endl; cout<<map<<endl; lacksDMA balloon2(balloon); hasDMA map2; map2=map; cout<<balloon2<<endl; cout<<map2<<endl; return 0; }

label: Portability Rating: 8 Label : Blimpo Rating:4 Color : red Label : Buffalo Keys Rating:5 Style : Mercator Label : Blimpo Rating:4 Color : red Label : Buffalo Keys Rating :5 Style : Mercator