CS32 - Week 4 Umut Oztok Jul 15, 2016 Umut Oztok CS32 - Week 4 - - PowerPoint PPT Presentation

cs32 week 4
SMART_READER_LITE
LIVE PREVIEW

CS32 - Week 4 Umut Oztok Jul 15, 2016 Umut Oztok CS32 - Week 4 - - PowerPoint PPT Presentation

CS32 - Week 4 Umut Oztok Jul 15, 2016 Umut Oztok CS32 - Week 4 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Umut Oztok CS32 - Week 4 Inheritance class


slide-1
SLIDE 1

CS32 - Week 4

Umut Oztok Jul 15, 2016

Umut Oztok CS32 - Week 4

slide-2
SLIDE 2

Inheritance

Process of deriving a new class using another class as a base. Base/Parent/Super Class − → Derived/Child/Sub Class

Umut Oztok CS32 - Week 4

slide-3
SLIDE 3

Inheritance

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; };

Dog inherits Animal.

Umut Oztok CS32 - Week 4

slide-4
SLIDE 4

Inheritance

When to use? If there exists an ”is-a relationship”.

Dog is an Animal. Student is a Person. SpeedShip is a Ship.

What is inherited? All member variables. All member funtions except assignment operator, constructors and destructor.

Umut Oztok CS32 - Week 4

slide-5
SLIDE 5

Inheritance

What is accessable? A derived class cannot access private member variables of its base class. A derived class cannot access private member functions of its base class. A derived class can access public member variables of its base class. A derived class can access public member functions of its base class. What if we want to access private functions of the base class? Use keyword protected instead of private. Making private member variables protected violates encapsulation!!

Umut Oztok CS32 - Week 4

slide-6
SLIDE 6

Inheritance

How to implement Constructor? Dog objects are Animal objects. So, when we create a Dog

  • bject, we also create an Animal object.

In general, when a class object is constructed:

1

Base class’s constructor is called.

2

Data members of the object are created.

3

Body of the constructor is executed.

Umut Oztok CS32 - Week 4

slide-7
SLIDE 7

Inheritance

Is Dog’s constructor valid?

class Animal{ public: Animal(){} ... private: int m_age; }; class Dog : public Animal{ public: Dog(int age, string name){ m_age = age; m_name = name; } ... private: int m_name; };

NO, m age cannot be accessed by Dog.

Umut Oztok CS32 - Week 4

slide-8
SLIDE 8

Inheritance

This one works for now.

class Animal{ public: Animal(){} void setAge(int age); ... private: int m_age; }; class Dog : public Animal{ public: Dog(int age, string name){ setAge(age); m_name = name; } ... private: int m_name; };

Umut Oztok CS32 - Week 4

slide-9
SLIDE 9

Inheritance

What if Animal’s constructor takes one argument?

class Animal{ public: Animal(int age){ m_age = age; } void setAge(int age): ... private: int m_age; }; class Dog : public Animal{ public: Dog(int age, string name){ setAge(age); m_name = name; } ... private: int m_name; };

Does not work anymore!

Umut Oztok CS32 - Week 4

slide-10
SLIDE 10

Inheritance

Solution is to use initializer list.

class Animal{ public: Animal(int age){ m_age = age; } ... private: int m_age; }; class Dog : public Animal{ public: Dog(int age, string name) : Animal(age){ m_name = name; } ... private: int m_name; };

Umut Oztok CS32 - Week 4

slide-11
SLIDE 11

Inheritance

Destructor : reverse order of constructor

1 Body of the destructor is executed. 2 Member variables are removed. 3 Base class’s destructor is called.

We will revisit destructors!!

Umut Oztok CS32 - Week 4

slide-12
SLIDE 12

Overriding

Assume speak() is implemented as follows: void Animal::speak() const{ cout << "..." << endl; } Dog inherits this function. But we expect a dog to make a specific sound. What to do?

Umut Oztok CS32 - Week 4

slide-13
SLIDE 13

Overriding

We can override member functions. class Dog : public Animal{ public: ... void speak() const; private: string m_name; }; void Dog::speak() const{ cout << "Woof!" << endl; } Example: Animal a1; a1.speak(); Output: ... Dog d1; d1.speak(); Output: Woof!

Umut Oztok CS32 - Week 4

slide-14
SLIDE 14

Overriding

We can still call the base class’s speak() function on a Dog object. Dog d1; d1.Animal::speak();

Umut Oztok CS32 - Week 4

slide-15
SLIDE 15

Polymorphism

Is the following allowed?: Animal* ani = new Dog; Yes, as ”Dog is an Animal”. Does the following output Woof!?: ani->speak(); No, because ani is a pointer to an Animal instance. But, I really want ani to say ”Woof!”. Solution: insert the virtual keyword in front of both the

  • riginal and the replacement functions!

Polymorphism: using a base pointer/reference to access any variable that is of a type derived from the base class.

Umut Oztok CS32 - Week 4

slide-16
SLIDE 16

Polymorphism

class Animal{ public: Animal(); virtual ~Animal(); int getAge() const; virtual void speak() const; private: int m_age; }; class Dog : public Animal{ public: Dog(); virtual ~Dog(); string getName() const; void setName(string name); virtual void speak() const; private: string m_name; };

Compiler figures out which object ani points to and calls the corresponding function.

Umut Oztok CS32 - Week 4

slide-17
SLIDE 17

Polymorphism

Virtual functions allow late binding or dynamic binding: Appropriate version of a method is decided at execution time (runtime), instead of at compilation time. Called as polymorphism as ani is allowed to have multiple forms.

Umut Oztok CS32 - Week 4

slide-18
SLIDE 18

Polymorphism

Animal* ani; int x; cin >> x; switch (x){ case 1: ani = new Dog; break; case 2: ani = new Cat; break; default: ani = new Animal; break; } ani->speak();

Umut Oztok CS32 - Week 4

slide-19
SLIDE 19

Polymorphism

class Animal{ public: void tickle () { speak(); } virtual void speak() { cout << "..."; } }; class Dog: public Animal { public: virtual void speak() { cout << "Woof!"; } }; main() { Animal* ani= new Dog; ani->tickle(); delete ani; } What is the output?

Umut Oztok CS32 - Week 4

slide-20
SLIDE 20

Virtual Destructor

Always make destructors virtual if you use inheritance. Otherwise, when you use polymorphism, only base class’s destructor will be called.

Umut Oztok CS32 - Week 4

slide-21
SLIDE 21

Pure Virtual Functions

virtual void speak() { cout << "..."; } speak function of Animal class doesn’t make sense. Make it pure virtual by putting ”=0” in declaration of base class. virtual void speak() = 0; You don’t implement pure virtual functions in base class. However, derived classes should implement pure virtual functions to be able to be instantiated.

Umut Oztok CS32 - Week 4

slide-22
SLIDE 22

Abstract Base Classes

If a base class has at least one pure virtual function, it is called abstract base class (ABC). An ABC cannot be instantiated. So, Animal ani; is not allowed.

Umut Oztok CS32 - Week 4

slide-23
SLIDE 23

Abstract Base Classes

Why do we use pure virtual functions? Avoid implementing ”dummy” functions when it doesn’t make sense. Force the programmer to implement functions in a derived class to prevent bugs.

Umut Oztok CS32 - Week 4

slide-24
SLIDE 24

Slides

Slides will be available at http://www.cs.ucla.edu/~umut/cs32

Umut Oztok CS32 - Week 4