{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

dis3 - CS32Discussion Sec.on1B Week4 TA:BrianChoi...

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

View Full Document Right Arrow Icon
CS32 Discussion Sec.on 1B Week 4 TA: Brian Choi
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
Inheritance The process of deriving a new class using another class as a base. Our example: But there might be some common features in the two… Dog Features of a dog Cat Features of a cat
Image of page 2
Inheritance The process of deriving a new class using another class as a base. Our example: Animal Implement common features of all animals here Dog Implement features specific to dogs here Cat Implement features specific to cats here derive derive
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
Deriving a class from another Dog inherits Animal . class Animal { public: Animal(); ~Animal(); int getAge() const; void setAge(int age); void speak() const; private: int m_age; }; class Dog : public Animal { public: Dog(); ~Dog(); string getName() const; void setName(string name); private: string m_name; }; base class derived class
Image of page 4
Deriving a class from another Dog d1; d1.setName(“puppy”); d1.setAge(1); Animal a1; a1.setAge(2); a1.setName(“abc”);
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
Deriving a class from another What’s inherited: all member func.ons except the overloaded assignment operator (operator=), constructors, and the destructor all member variables However, the derived class cannot access the private members of the base class directly (e.g. Dog cannot access m_age ). class D : public B “a D object is a kind of B”
Image of page 6
Construc.on So, a Dog is an Animal. What happens when we construct a Dog? 1. The base part of the class is constructed. Animal m_age 0
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
So, a Dog is an Animal. What happens when we construct a Dog? 2. The member variables are created. m_name Construc.on Animal m_age 0 “”
Image of page 8
So, a Dog is an Animal. What happens when we construct a Dog?
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