Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Inheritance & Dynamic Memory Allocation Suppose base class uses dynamic memory allocation. If derived class doesn’t use dynamic memory allocation then."— Presentation transcript:

1 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.

2 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); … } ;

3 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.

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

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

6 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;

7 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; }

8 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; }

9 #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); } ;

10 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); };

11 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

12 #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; }

13 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; }

14 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'; }

15 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; }

16 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<<).

17 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); }

18 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; }

19 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; }

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

21 #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; }

22 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


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

Similar presentations


Ads by Google