cs32w11dis3

cs32w11dis3 - CS32: Introduction to Computer Science II...

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

View Full Document Right Arrow Icon
Discussion 2C Notes (Week 4, January 28) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Inheritance Basics Inheritance in C++ refers to the process of deriving a new class using another class as a base . The old class used as a base is called the base class and the new class is called the derived class . In other contexts they may be called the parent class and the child class . Let’s do an example: class Dog : public Animal ” indicates that Dog inherits Animal . All members (both functions and variables) of Animal are also members of Dog , but not vice versa. Here is how you would use these classes: Dog d1; d1.setName(“puppy”); d1.getAge(); // from Animal d1.speak(); // from Animal Animal a1; a1.speak(); //a1.setName(“10”); // error! 1. The derived class inherits all member functions of the base class except the assignment operator ( operator= ), constructors, and the destructor. 2. The derived class cannot access the private members of the base class directly. ( Dog cannot access m_age directly, although it is part of its member.) 3. When we say class D : public B , we mean “a D object is a kind of B ” or “a D is a B .” This means you should not use public inheritance if such relationship does not hold (Is a dog an animal?). Take a look at the description of “ is-a relationship” in the textbook (p. 396). 4. As always, data members should always be private, unless there is a strong reason not to. CS32: Introduction to Computer Science II Winter 2011 Copyright 2011 Brian Choi Week 4, Page 1/6 class Animal { public: Animal(); ~Animal(); int getAge() const; 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; }; setName(), getName() m_name getAge(), speak() m_age Animal Dog
Background image of page 1

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

View Full DocumentRight Arrow Icon
Construction/Destruction Dog objects are Animal objects. Therefore, when we create a Dog object, we are also creating an Animal object. So when an instance of Dog is created, the Animal ’s constructor is called to create an Animal first, and then the Dog constructor is called to complete the construction of a Dog . More generally, when a class object is constructed: 1. The base part of the object is constructed (by calling the base class’s constructor). 2. The data members of the object are created.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

cs32w11dis3 - CS32: Introduction to Computer Science II...

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

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