1 / 8

How Inheritance Works with Memory Diagram

How Inheritance Works with Memory Diagram. CSCE 121. output. struct Person { string name; int weight; }; struct Doctor : public Person { string specialty; }; int main() { Person* p = new Person; p->name = "George"; p->weight = 187; Doctor* d = new Doctor;

jledbetter
Download Presentation

How Inheritance Works with Memory Diagram

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How Inheritance Workswith Memory Diagram CSCE 121

  2. output struct Person { string name; int weight; }; struct Doctor : public Person { string specialty; }; int main() { Person* p = new Person; p->name = "George"; p->weight = 187; Doctor* d = new Doctor; d->name = "Sam"; d->weight = 165; d->specialty = "surgeon"; Person* w; w = d; } stack identifier heap

  3. output struct Person { string name; int weight; }; struct Doctor : public Person { string specialty; }; int main() { Person* p = new Person; p->name = "George"; p->weight = 187; Doctor* d = new Doctor; d->name = "Sam"; d->weight = 165; d->specialty = "surgeon"; Person* w; w = d; } w main d p stack identifier heap

  4. output struct Person { string name; int weight; }; struct Doctor : public Person { string specialty; }; int main() { Person* p = new Person; p->name = "George"; p->weight = 187; Doctor* d = new Doctor; d->name = "Sam"; d->weight = 165; d->specialty = "surgeon"; Person* w; w = d; } w George name main d 187 weight p stack identifier heap

  5. output struct Person { string name; int weight; }; struct Doctor : public Person { string specialty; }; int main() { Person* p = new Person; p->name = "George"; p->weight = 187; Doctor* d = new Doctor; d->name = "Sam"; d->weight = 165; d->specialty = "surgeon"; Person* w; w = d; } name Sam weight 165 specialty surgeon w George name main d 187 weight p stack identifier heap

  6. output struct Person { string name; int weight; }; struct Doctor : public Person { string specialty; }; int main() { Person* p = new Person; p->name = "George"; p->weight = 187; Doctor* d = new Doctor; d->name = "Sam"; d->weight = 165; d->specialty = "surgeon"; Person* w; w = d; } name Sam weight 165 specialty surgeon w George name main d 187 weight p stack identifier heap

  7. output w can only see members of Person. Even though specialty is there, w cannot see it. struct Person { string name; int weight; }; struct Doctor : public Person { string specialty; }; int main() { Person* p = new Person; p->name = "George"; p->weight = 187; Doctor* d = new Doctor; d->name = "Sam"; d->weight = 165; d->specialty = "surgeon"; Person* w; w = d; } name Sam weight 165 specialty surgeon w George name main d 187 weight p stack identifier heap

  8. output Shading was for illustration, for an exam this is what you would draw. struct Person { string name; int weight; }; struct Doctor : public Person { string specialty; }; int main() { Person* p = new Person; p->name = "George"; p->weight = 187; Doctor* d = new Doctor; d->name = "Sam"; d->weight = 165; d->specialty = "surgeon"; Person* w; w = d; } name Sam weight 165 specialty surgeon w George name main d 187 weight p stack identifier heap

More Related