Inheritance What is Inheritance Overriding vs Overloading Access
















- Slides: 16
Inheritance • What is Inheritance? • Overriding vs. Overloading • Access Control – Inheritance Types • Virtual Functions • Abstract Classes 11/23/2020 Copyright 2005, The Ohio State University 1
What is Inheritance ? • A method to form new classes using the ones that are already defined • Defines an hierarchy • Defines a is-a relationship – Student is-a Person – Apple is-a Fruit – Circle is-a Shape • Also known as Generalization – Person is an abstraction/generalization of Student – Fruit is a generalization of Apple • Derived Class vs. Base Class 11/23/2020 Copyright 2005, The Ohio State University 2
Derived Classes • Also known as Sub classes • Derived class inherits attributes and behavior from Base class => Inheritance Employee Base class • Manager is-a Employee Inheritance Type class Employee { string name; Date hiring_date; int emp_no; … } 11/23/2020 Manager Derived class Manager : public Employee { // name, hiring_date, emp_no are inherited int level; Employee *group; … } Copyright 2005, The Ohio State University 3
Examples Polygon Rectangle Point Circle Triangle Mammal Pet 3 D-Point Sphere Cat 11/23/2020 Copyright 2005, The Ohio State University 4
• Derived class is generally bigger than Base class – Adds extra attributes and behavior => Specialization – Ex: class Bank_Account - account #, owner, balance class Saving_Account : public Bank_Account - interest rate, interest earned • Manager can be used wherever Employee can be used void foo (Manager mm, Employee ee) { Employee * pe = &mm; // ok: every Manager is an Employee Manager * pm = ⅇ // error: Employee need not be a Manager pm->level = 2; // not possible: pm does not know about level pm = static_cast<Manager *> (pe); // ok: pe points to a Manager pm->level = 2; // ok ! } Base Class pointer can point to a Derived Class but not vice versa !! 11/23/2020 Copyright 2005, The Ohio State University 5
• Derived Classes can choose to modify the behavior of inherited methods Þ Overriding • In case of Specialization, modification can be necessary class Bank_Account { private: string address; int account_no; float balance; public: string name; void print () const; … } 11/23/2020 class Savings_Acc : public Bank_Account { private: float interest_rate, interest_earned; Date balance_date; Overriding public: void print () const { Bank_Account: : print ( ); cout << interest_rate … } Copyright 2005, The Ohio State University 6
Overriding vs. Overloading class Point { class Circle : public Point { int x, y; int radius; public: void update (int xx, int yy); Overriding update ( int xx, int yy, int rr ); void update (int xx); … void update (int yy); } … Overloading } • Overriding operates across scopes • Overloading operates within a scope 11/23/2020 Copyright 2005, The Ohio State University 7
Access Control on members • Access control of inherited members in derived classes are governed by: 1. Access control of members 2. Inheritance Type • Access control of members – Derived class can access public and protected members of base class – protected: Acts as public to derived classes and private to others 11/23/2020 Copyright 2005, The Ohio State University 8
Inheritance Types Access control in A A B_priv B_public • • • Access control in B Type of Inheritance private protected public private - - - protected private protected public B_prot Inheritance type defines the access control in derived class No derived class can access private of A In B_priv, public and protected parts of A are private In B_prot, public and protected parts of A are protected In B_public, public parts of A are public and protected parts of A are protected 11/23/2020 Copyright 2005, The Ohio State University 9
Virtual Functions (1) class Employee { // data members public: void print() const; … } class Manager : public Employee { // data members public: void print() const; … } void global_print ( Employee *pe) { pe->print(); } Which print() is invoked? ? ? Given a pointer Base_class *, to which derived type does the object pointed to belong? 11/23/2020 Copyright 2005, The Ohio State University 10
Virtual Functions (2) Intelligent way! - Type Fields class Employee { char type; Employee() : type(‘E’) { … } // data members public: void print() const; … } class Manager : public Employee { Manager() { type = ‘M’; } // data members public: void print() const; … } void global_print ( Employee *pe) { if (pe->type == ‘E’) // Print Employee else // Print Manager } 11/23/2020 Copyright 2005, The Ohio State University 11
Virtual Functions (3) • Type Fields is smart method but it suffers from following: – Cumbersome – Difficult to manage – Difficult to extend • Elegant way of doing it is by using Virtual Functions • If a function is defined in base and derived classes and if it is virtual, then given a pointer we can unambiguously (at run time) determine which function needs to be invoked • Since ambiguity is resolved at run time, it is known as late Binding or Dynamic Binding 11/23/2020 Copyright 2005, The Ohio State University 12
Virtual Functions (4) class Foo { public: void f() { cout << "Foo: : f()"; } virtual void g() { cout << "Foo: : g()"; } } class Bar : public foo { public: void f() { cout << “Bar: : f()"; } virtual void g() { cout << “Bar: : g()"; } } 11/23/2020 Foo foo; Bar bar; Foo *baz = &bar ; Bar *quux = &bar ; foo. f(); // "Foo: : f()" foo. g(); // "Foo: : g()" bar. f(); // "Bar: : f()" bar. g(); // "Bar: : g()" // So far everything we would expect. . . baz->f(); // "Foo: : f()" baz->g(); // "Bar: : g()" quux->f(); // "Bar: : f()" quux->g(); // "Bar: : g()" Copyright 2005, The Ohio State University 13
Abstract Classes (1) • Not all classes are concrete • They might represent some abstract concept Class shape can define abstract methods like draw and rotate which should be overridden in derived classes Shape Rectangle Triangle class Shape { // intelligent but inelegant public: virtual void draw () { cout << “error: draw() not defined here. ”; } virtual void rotate (int degrees) { cout << “error: rotate() not defined here. ”; } } 11/23/2020 Copyright 2005, The Ohio State University 14
Abstract Classes (2) class Shape { // elegant public: virtual void draw () = 0; // pure virtual function virtual void rotate (int degrees) = 0; // pure virtual function } • Class with one or more pure virtual functions is abstract • Abstract class can only be used as an interface or base class – i. e. , can not instantiate an object of abstract class – Shape s; // silly: shapeless shape • We can instantiate only a concrete class 11/23/2020 Copyright 2005, The Ohio State University 15
11/23/2020 Copyright 2005, The Ohio State University 16