dis3

dis3 - CS32Discussion Sec.on3A Week4 TA:BrianChoi Reminder...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
CS32 Discussion Sec.on 3A Week 4 TA: Brian Choi
Background image of page 1

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

View Full DocumentRight Arrow Icon
Reminder Homework 2 Due 2/3 Tuesday Stacks and queues
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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 speciFc to dogs here Cat Implement features speciFc to cats here derive derive
Background image of page 4
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: int m_name; }; base class derived class
Background image of page 5

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

View Full DocumentRight Arrow Icon
Deriving a class from another Dog d1; d1.setName(“puppy”); d1.setAge(1); Animal a1; a1.setAge(2); a1.setName(“abc”);
Background image of page 6
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”
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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 “”
Background image of page 9

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

View Full DocumentRight Arrow Icon
So, a Dog is an Animal. What happens when we construct a Dog? 3. The body of constructor is executed. ! m_name Construc±on Animal m_age 0 Ini±alized
Background image of page 10
Just reverse the order of construc.on± 1± The body of destructor is executed± 2± The member variables are removed± 3± The base part of the class is destructed± !
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/07/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 31

dis3 - CS32Discussion Sec.on3A Week4 TA:BrianChoi Reminder...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online