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 Animal{ class Dog : public Animal{ public: public: Animal(); Dog(); ~Animal(); ~Dog(); int getAge() const; string getName() const; void speak() const; void setName(string name); private: private: int m_age; string m_name; }; }; Dog inherits Animal. Umut Oztok CS32 - Week 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
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
Inheritance How to implement Constructor? Dog objects are Animal objects. So, when we create a Dog object, we also create an Animal object. In general, when a class object is constructed: Base class’s constructor is called. 1 Data members of the object are created. 2 Body of the constructor is executed. 3 Umut Oztok CS32 - Week 4
Inheritance Is Dog’s constructor valid? class Animal{ class Dog : public Animal{ public: public: Animal(){} Dog(int age, string name){ ... m_age = age; private: m_name = name; int m_age; } }; ... private: int m_name; }; NO, m age cannot be accessed by Dog. Umut Oztok CS32 - Week 4
Inheritance This one works for now. class Animal{ class Dog : public Animal{ public: public: Animal(){} Dog(int age, string name){ void setAge(int age); setAge(age); ... m_name = name; private: } int m_age; ... }; private: int m_name; }; Umut Oztok CS32 - Week 4
Inheritance What if Animal’s constructor takes one argument? class Animal{ class Dog : public Animal{ public: public: Animal(int age){ Dog(int age, string name){ m_age = age; setAge(age); } m_name = name; void setAge(int age): } ... ... private: private: int m_age; int m_name; }; }; Does not work anymore! Umut Oztok CS32 - Week 4
Inheritance Solution is to use initializer list. class Animal{ class Dog : public Animal{ public: public: Animal(int age){ Dog(int age, string name) m_age = age; : Animal(age){ } m_name = name; ... } private: ... int m_age; private: }; int m_name; }; Umut Oztok CS32 - Week 4
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
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
Overriding We can override member functions. Example: class Dog : public Animal{ public: Animal a1; ... a1.speak(); void speak() const; Output: ... private: string m_name; }; Dog d1; void Dog::speak() const{ d1.speak(); cout << "Woof!" << endl; Output: Woof! } Umut Oztok CS32 - Week 4
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
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 original 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
Polymorphism class Animal{ class Dog : public Animal{ public: public: Animal(); Dog(); virtual ~Animal(); virtual ~Dog(); int getAge() const; string getName() const; virtual void speak() const; void setName(string name); private: virtual void speak() const; int m_age; private: }; string m_name; }; Compiler figures out which object ani points to and calls the corresponding function. Umut Oztok CS32 - Week 4
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
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
Polymorphism class Animal{ main() { public: Animal* ani= new Dog; void tickle () { ani->tickle(); speak(); delete ani; } } virtual void speak() { What is the output? cout << "..."; } }; class Dog: public Animal { public: virtual void speak() { cout << "Woof!"; } }; Umut Oztok CS32 - Week 4
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
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
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
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
Slides Slides will be available at http://www.cs.ucla.edu/~umut/cs32 Umut Oztok CS32 - Week 4
Recommend
More recommend