06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation...

14
06E-1 oop C++ Addendum: Inheritance C++ Addendum: Inheritance and Encapsulation and Encapsulation Protected members Inheritance Type Public Inheritance Private Inheritance Protected Inheritance

Transcript of 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation...

Page 1: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-1

oop

C++ Addendum: Inheritance and C++ Addendum: Inheritance and EncapsulationEncapsulation

Protected members Inheritance Type Public Inheritance Private Inheritance Protected Inheritance

Page 2: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-2

oop

Private vs. ProtectedPrivate vs. Protected Private: private members can be accessed only by member

functions and friends. Members cannot be accessed by derived classes. Main Rational: otherwise, inheritance could be used to break

encapsulation.By inheriting from a class, a user may gain access to its

implementation details. Counter Argument: language enforced encapsulation is used to protect

against accidents, not against malice! In many cases it would be impossible to implement new functionality

in a derived class without access to encapsulated members. Protected: protected members can be accessed by member

functions, friends and derived classes. Benefit: true extendibility

A class cannot be extended if it discloses vital details from its extender.

private members allow the designer to reserve the right to modify a class at the cost of putting restrictions on extendibility.

Page 3: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-3

oop

Visibility Levels in C++Visibility Levels in C++

private protected public

visibility membersfriends

membersfriendsderived classes

any

encapsulation maximal moderate none

extendibility restrictedsubclass cannot even dothe same as superclass

maximalsubclass can do whateverthe superclass can

N/A

modifiability maximalchanges hidden fromsubclass

restrictedchanges affect subclass

minimalchanges affectall clients

Page 4: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-4

oop

Inheritance TypeInheritance Type For many purposes: subobject is just like a data member. What is the visibility of a subobject?

Public inheritance: Most commonly usedSpecified by a public keyword:

class Derived: public Base {...}; Inheritance/existence of a sub-object: visible outside the class

Private inheritance:Rarely usedSpecified by the private keyword:

class Derived: private Base {...}; Inheritance/existence of a sub-object: visible only inside the class

Defaults: struct: Public inheritance

class: Private inheritance Many compilers (Cfront, BC, ...) warn if the default is used

Page 5: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-5

oop

Example of Private InheritanceExample of Private Inheritance Default for class inheritance (unfortunate?)

The fact that Car inherits from Engine is private: The existence of an Engine subobject in Car is private Only from inside Car

convert Car to Engineconvert Car* to Engine* convert Car& to Engine&call public or protected function members of Engineaccess public or protected data members of Engine

class Car: private Engine { //...

};

Page 6: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-6

oop

Rules of Inheritance TypeRules of Inheritance Type

Base class is just like a member; it can be declared: public (default if subclass is struct)

protected private (default if subclass is class)

The inheritance type controls rights to type cast to super-class,

access to public members of superclass,

access to protected members of superclass, and

access to private members of superclass.

Page 7: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-7

oop

Outside View of Outside View of Inheritance TypesInheritance Types

class Super {} *p;

class PublicInherit: public Super {} *p1;class ProtectedInherit: protected Super {} *p2;class PrivateInherit: private Super {} *p3;

void OutsiderFunc(void){

p = p1; //OK

p = p2; //Error! protected fact “type of *p2 // is a subtype of *p”

p = p3; //Error! private fact “type of *p3 // is a subtype of *p”

}

Page 8: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-8

oop

Private Inheritance: Inside ViewPrivate Inheritance: Inside View

class Super {} *p;

class PublicInherit: public Super {} *p1;class PrivateInherit: private Super {} *p3;

void PrivateInherit::f(void){

p = p1; // OK

p = p3; // OK// private fact: “type of *p3// is a subtype of *p”

}

Page 9: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-9

oop

Inheritance Types: Inheritance Types: Members’ VisibilityMembers’ Visibility

class Base {public: int pub;

} base;

class PublicInherit: public Base {} pubHeir;class ProtectedInherit: protected Base {} protHeir;class PrivateInherit: private Base {} privHeir;

void OutsiderFunc(void){

int i = pubHeir.pub; // OK

i = protHeir.pub; // Error! // protHeir.pub is protected

i = privHeir.pub; // Error! privHeir.pub is private}

Page 10: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-10

oop

More on Members’ Visibility More on Members’ Visibility

class Base {public: int pub;protected: int prot;

};

class XBase: protected Base {};class X: public XBase {};

class YBase: private Base {};class Y: public YBase {};

void X::f(void) {int i = pub; // OKi = prot; // OK

}void Y::f(void){ int i = pub; // Error! pub is private i = prot; // Error! prot is private}

Page 11: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-11

oop

Adjusting Access Control #1/2Adjusting Access Control #1/2class Base {

public: int pub1;int pub2;

protected: int prot;

private: int priv;

};

class Derived: private Base {public:

Base::pub1;protected:

Base::pub2;protected:

Base::prot;};

Relies on a deprecated feature of the language.

Works in most current compilers.

Page 12: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-12

oop

Adjusting Access Control #2/2Adjusting Access Control #2/2class Base {

public: int pub1;int pub2;

protected: int prot;

private: int priv;

};

class Derived: private Base {public:

using Base::pub1;protected:

using Base::pub2;protected:

using Base::prot;};

Technique relies on the using keyword.

Page 13: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-13

oop

Restrictions on AdjustingRestrictions on Adjusting

The base class must be private, or

protected Can only:

Restore permission to be the same as in the base class:public member in private base protected member in private base public member in protected base

Increase permission:public member in private base

• Increase to protected

Page 14: 06E-1 oop C++ Addendum: Inheritance and Encapsulation C++ Addendum: Inheritance and Encapsulation uProtected members uInheritance Type uPublic Inheritance.

06E-14

oop

Summary: Visibility in Inheritance TypesSummary: Visibility in Inheritance TypesPublic

Base ClassProtected

Base ClassPrivate

Base Class

Public members of derived class

Protected members of derived class

Not accessible in derived

class

Protected1 members of derived class

Protected members of derived class

Not accessible in derived

class

Private2 members of derived class

Private3

members of derived class

Not accessible in derived

class

Public Members of Base Class

ProtectedMembers of Base Class

Private Members of Base Class

1 can adjust to public2 can adjust to protected or public 3 can adjust to protected