Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Inheritance

Description:

os 'Style: ' hs.style endl; return os; #include iostream.h ... Style : Mercator. Label : Blimpo. Rating:4. Color : red. Label : Buffalo Keys. Rating :5 ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 23
Provided by: Ara8
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
Inheritance Dynamic Memory Allocation
  • Suppose base class uses dynamic memory
    allocation.
  • If derived class doesnt 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 lnull, int r0)
  • baseDMA (const baseDMA rs)
  • virtual baseDMA()
  • baseDMA operator (const baseDMA rs)

3
  • class lacksDMA public baseDMA
  • private
  • char color 40
  • public
  • If we dont 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)
  • labelnew charstrlen(rs.label)1
  • strcpy(label, rs.label)
  • ratingrs.rating
  • hasDMA(const hasDMA hs) baseDAM(hs)
  • stylenew charstrlen(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 baseDMAoperator(const baseDMA rs)
  • if (this rs )
  • return this
  • delete label
  • labelnew charstrlen(rs.label)1
  • strcpy(label, rs.label)
  • ratingrs.rating
  • return this

8
  • hasDMA hasDMAoperator(const hasDMA hs)
  • if (this hs )
  • return this
  • baseDMA operator(hs)
  • //copy base portion
  • stylenew charstrlen(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 r0)
  • baseDMA (const baseDMA rs)
  • virtual baseDMA()
  • baseDMA operator(const baseDMA rs)
  • friend ostream operatorltlt(ostream os,
    const baseDMA rs)

10
  • class lacksDMA public baseDMA
  • private
  • enum COL_LEN 40
  • char colorCOL_LEN
  • public
  • lacksDMA (const char c "black", const char l
    "null",
  • int r0)
  • lacksDMA (const char c , const baseDMA rs )
  • friend ostream operator ltlt ( ostream os ,
    const lacksDMA rs)

11
  • class hasDMA public baseDMA
  • private
  • char style
  • public
  • hasDMA (const char s "none", const char l
    "null",
  • int r0)
  • hasDMA (const char s , const baseDMA rs )
  • hasDMA (const hasDMA rs )
  • hasDMA()
  • hasDMA operator(const hasDMA rs)
  • friend ostream operator ltlt ( ostream os ,
    const hasDMA rs)
  • endif

12
  • include ltiostream.hgt
  • include "dam.h"
  • includeltcstringgt
  • baseDMA baseDMA(const char l , int r)
  • labelnew charstrlen(l)1
  • strcpy(label, l)
  • ratingr
  • baseDMA baseDMA(const baseDMA rs)
  • labelnew charstrlen(rs.label)1
  • strcpy(label, rs.label)
  • ratingrs.rating

13
  • baseDMA baseDMA ()
  • delete label
  • baseDMA baseDMAoperator(const baseDMA rs)
  • if (this rs )
  • return this
  • delete label
  • labelnew charstrlen(rs.label)1
  • strcpy(label, rs.label)
  • ratingrs.rating
  • return this

14
  • ostream operator ltlt (ostream os, const baseDMA
    rs)
  • osltlt"label "ltltrs.labelltltendl
  • osltlt"rating "ltltrs.ratingltltendl
  • return os
  • lacksDMA lacksDMA (const char c, const char
    l,
  • int r) baseDMA(l,r)
  • strncpy(color, c, 39)
  • color39'\0'

15
  • lacksDMA lacksDMA (const char c, const baseDMA
    rs) baseDMA(rs)
  • strncpy(color, c, COL_LEN-1)
  • colorCOL_LEN-1\0
  • ostream operator ltlt (ostream os, const lacksDMA
    ls)
  • osltlt(const baseDMA ) ls
  • osltltcolor ltltls.colorltltendl
  • 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 coutltlt).

17
  • hasDMA hasDMA (const char s, const char l ,
    int r) baseDMA (l,r)
  • stylenew char strlen(s)1
  • strcpy(style,s)
  • hasDMA hasDMA (const char s, const baseDMA
    rs) baseDMA (rs)
  • stylenew char strlen(s)1
  • strcpy(style,s)

18
  • hasDMA hasDMA(const hasDMA hs )
  • baseDMA(hs) // call base class copy constructor
  • stylenew char strlen(hs.style)1
  • strcpy(style,hs.style)
  • hasDMA hasDMA()
  • delete style

19
  • hasDMA hasDMAoperator(const hasDMA hs)
  • if (this hs )
  • return this
  • baseDMAoperator(hs) // copy base portion
  • stylenew charstrlen(hs.style)1
  • strcpy(style, hs.style)
  • return this

20
  • ostream operator ltlt (ostream os, const hasDMA
    hs)
  • osltlt(const baseDMA ) hs
  • osltlt"Style "ltlths.styleltltendl
  • return os

21
  • includeltiostream.hgt include dam.h
  • int main()
  • baseDMA shirt(Portability, 8)
  • lacksDMA balloon( red, Blimpo,4)
  • hasDMA map(Mercator, Buffalo Keys,5)
  • coutltltshirtltltendl
  • coutltltballoonltltendl
  • coutltltmapltltendl
  • lacksDMA balloon2(balloon)
  • hasDMA map2
  • map2map
  • coutltltballoon2ltltendl
  • coutltltmap2ltltendl
  • return 0

22
  • label Portability
  • Rating 8
  • Label Blimpo
  • Rating4
  • Color red
  • Label Buffalo Keys
  • Rating5
  • Style Mercator
  • Label Blimpo
  • Rating4
  • Color red
  • Label Buffalo Keys
  • Rating 5
  • Style Mercator
Write a Comment
User Comments (0)
About PowerShow.com