CS321B_week5 - CS32 Discussion Section 1B Week 5 TA Hao...

Info icon This preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CS32 Discussion Section 1B Week 5 TA: Hao Yu (Cody)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Reminder & Agenda Inheritance Recursion Homework 3 due next Tuesday 9:00 p.m.
Image of page 2
Inheritance The process of deriving a new class using another class as a base Why we need inheritance? Example Dog Age Speak Cat Age Speak Monkey Age Speak doMath
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Inheritance Dog Age Speak Cat Age Speak Monkey Age Speak doMath class Dog { public: Dog(); ~Dog(); void setAge ( int a) { m_age = m; } int getAge () { return m_age; } void speak() const { cout << "woof! \n ";} private: int m_age; } class Monkey { public: Monkey( int n); ~Monkey(); …… void speak() const { cout << "Ooh ah! \n ";} int doMath( int a) { return num + a; } private: int m_age; int m_num; } class Cat { public: Cat(); ~Cat(); void setAge ( int a) { m_age = m; } int getAge () { return m_age; } void speak() const { cout << “meow~ \n ";} private: int m_age; }
Image of page 4
Inheritance Dog Age Speak Cat Age Speak Monkey Age Speak doMath Animal Age Speak class Animal { public: Animal(); ~Animal(); void setAge ( int a) { m_age = a; } int getAge () { return m_age; } void speak() const { count << “N/A\n”; } private: int m_age; } Base class
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Inheritance Dog Age Speak Animal Age Speak Base class class Dog : public Animal { public: Dog(); ~Dog(); } Derived class class Animal { public: Animal(); ~Animal(); void setAge ( int a) { m_age = a; } int getAge () { return m_age; } void speak() const { count << “N/A\n”; } private: int m_age; }
Image of page 6
Inheritance Monkey Age Speak doMath Animal Age Speak Base class class Monkey : public Animal { public: Monkey( int n); ~Monkey(); int doMath ( int a) { return m_num + a; } } Derived class class Animal { public: Animal(); ~Animal(); void setAge ( int a) { m_age = a; } int getAge () { return m_age; } void speak() const { count << “N/A\n”; } private: int m_age; }
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
What Can You See? Public functions: setAge() getAge() speak() Private members: m_age Animal Public functions: setAge() getAge() speak() doMath() Private members: n_num Monkey Derived class: All member functions except Overloaded assignment operator Constructor/Deconstrcutor All non-private member variables Base class: Only see yourself YOU KNOW NOTHING about the derived classes!
Image of page 8
Exercise Public functions: setAge() getAge() speak() Private members: m_age Animal Public functions: setAge() getAge() speak() doMath() Private members: m_num Monkey Animal animal; Monkey monkey; Is monkey.getAge() valid?
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern