Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs....

16
Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control Inheritance Types Virtual Functions Abstract Classes

Transcript of Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs....

Page 1: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 1

Inheritance

• What is Inheritance?• Overriding vs. Overloading• Access Control

– Inheritance Types

• Virtual Functions• Abstract Classes

Page 2: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 2

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

Page 3: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 3

Derived Classes

• Also known as Sub classes• Derived class inherits attributes and

behavior from Base class => Inheritance• Manager is-a Employee

class Employee { string name; Date hiring_date; int emp_no; …}

class Manager : public Employee { // name, hiring_date, emp_no are inherited int level; Employee *group; …}

Employee

Manager

Base class

Derived classInheritance Type

Page 4: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 4

Examples

Rectangle Triangle

Polygon Point

Circle 3D-Point

SphereMammal Pet

Cat

Page 5: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 5

• 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 = &ee; // 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 !!

Page 6: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 6

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

class Savings_Acc : public Bank_Account { private: float interest_rate, interest_earned; Date balance_date; public: void print () const {

Bank_Account::print ( ); cout << interest_rate … } …}

Overriding

Page 7: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 7

Overriding vs. Overloading

• Overriding operates across scopes• Overloading operates within a scope

class Point { int x, y; public: void update (int xx, int yy); void update (int xx); void update (int yy); …}

class Circle : public Point { int radius; public: update ( int xx, int yy, int rr ); …}

Overriding

Overloading

Page 8: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 8

Access Control on members

• Access control of inherited members in derived classes are governed by:1. Access control of members2. 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

Page 9: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 9

Inheritance Types

private protected public

private - - -

protected

private protected protected

public private protected public

Type of Inheritance

• 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

A

B_priv

Access control in AAccess control in B

B_prot

B_public

Page 10: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 10

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?

Page 11: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 11

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}

Page 12: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 12

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

Page 13: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 13

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()"; }}

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()"

Page 14: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 14

Abstract Classes (1)• Not all classes are concrete• They might represent some abstract concept

Rectangle Triangle

ShapeClass shape can define abstract

methods like draw and rotate which should be overridden in derived classes

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

Page 15: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 15

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

Page 16: Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Copyright 2005, The Ohio State University 16