INHERITANCE - Part 1 Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors CSC 330 OO Software Design 1
OOP Major Capabilities encapsulation inheritance polymorphism which can improve the design, structure and reusability of code. CSC 330 OO Software Design 2
Using inheritance to promote software reusability 3
Main Benefit - code reuse: No need to replicate class methods (the subclass can inherit those of the superclass). No need to replicate functions with parameters of the superclass type; they will work fine when passed actual parameters of the subclass type (because every object of the subclass type IS-A object of the superclass type, too!) CSC 330 OO Software Design 4
When using Inheritance? When you have a class C, and you want another class D, that does everything that C does and more: use inheritance! CSC 330 OO Software Design 5
CSC 330 OO Software Design 6
CSC 330 OO Software Design 7
Example class Point { private : // attributes int x, y ; // methods void setx ( int newx ) ; int getx ( ) ; void sety ( int newy ) ; int gety ( ) ; class Circle { private : // attributes int x, y, radius ; // methods void setx ( int newx ) ; int getx ( ) ; void sety ( int newy ) ; int gety ( ) ; void setrad ( int newrad ) ; int getrad ( ) ; CSC 330 OO Software Design 8
Discussions Comparing both classes definitions, we can observe the following: Both classes have two data elements x and y. In the class Point these elements describe the position of the point, in the class Circle, they describe the circle center. Thus, x and y have the same meaning in both classes: They describe the position of their associated object by defining a point. CSC 330 OO Software Design 9
Discussions cont d Both classes offer the same set methods to get and set the value of two data elements, x and y. Class Circle "adds" a new data element radius and corresponding access methods. We can describe a circle as a point plus a radius. Thus a circle is "a-kind-of" point. However, a circle is somewhat more "specialized". CSC 330 OO Software Design 10
Terminology Base Class (sometimes called superclass class) Derived Class (sub( subclass) Single inheritance Multiple inheritance 3 kinds of inheritance: Public Protected Private CSC 330 OO Software Design 11
Inheritance examples student shape Graduate student Undergraduate student circle triangle ECE256 student Right-angled triangle CSC 330 OO Software Design 12
Q&A What can be inherited from a base class? Can a derived class be a base class? Can a derived class add more attributes of its own? Is a derived class more specific than its base class? Is a derived class represents a smaller group of objects? CSC 330 OO Software Design 13
How does inheritance work? Access (members) Rules Access control within a class Access control among classes Public inheritance Protected inheritance Private inheritance CSC 330 OO Software Design 14
Public inheritance Base class Public members Protected members Private members Derived class Public members Protected members Private members CSC 330 OO Software Design 15
Basics The 3 most important capabilities of object-oriented oriented programming: CSC 330 OO Software Design 16
Relationships Is a Has a Use a : public inheritance : composition of other classes : non-private member functions : pointers to other objects Knows a : pointers to other objects CSC 330 OO Software Design 17
Discussions cont d In this and the following figures, classes are drawn using rectangles. The arrow line indicates the direction of the relation.it is to be read as "Circle is a-kind-of Point". CSC 330 OO Software Design 18
Inheritance (same as saying Derivation) With inheritance, we are able to make use of the a-kind-of (is-a ) relationship. In our point and circle example, we can define a circle which inherits from point. CSC 330 OO Software Design 19
Definition (Inheritance): Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say ``A inherits from B''. Objects of class A thus have access to attributes and methods of class B without the need to redefine them. Definition (Superclass/Subclass) If class A inherits from class B, then B is called superclass of A. A is called subclass of B. CSC 330 OO Software Design 20
Inheritance Basic Definitions Superclasses are also called parent classes or base classes. Subclasses may also be called child classes or just derived classes. CSC 330 OO Software Design 21
Basic Concepts and Syntax To derive class DC from class BC, we write: // BC is the base class; DC is the derived class class DC : public BC { //... CSC 330 OO Software Design 22
Example 4.2.1 class Pen { enum ink ( off, on ); void set-status( ink ); void set-location( int, int ); private: int x; int y; int status; }; CSC 330 OO Software Design 23
Example 4.2.1 cont d class CPen : public Pen { void set_color ( int ) ; private: int color ; CSC 330 OO Software Design 24
private Members in Inheritance Each private member in a base class is visible only in the base class. In particular, a private member of a base class is not visible in a derived class. A private member of a base class is inherited by the derived class, but it is not visible in the derived class. CSC 330 OO Software Design 25
Fig. 4.2.1. Deriving one class from another CSC 330 OO Software Design 26
private Members in Inheritance Example 4.2.2. class Point { void set_x( int x1 ) { x = xl; } void set_y( int yl ) { y = yl; } int get_x( ) const { return x; } int get_y( ) const { return y; } private: int x; int y; }; class Intense_point : public Point { void set_intensity ( int I ) {intensity = I ; } int get_intensity ( ) const { return intensity ; } private: int intensity ; CSC 330 OO Software Design 27
Discussions In Example 4.2.1, data members x, y, and status are inherited by CPen from Pen even though they are not visible in CPen. Whenever an object of type CPen is created, storage for x, y, and status is allocated. Although a private member of a base class cannot be directly accessed in a derived class, it might be indirectly accessed through a derived method. CSC 330 OO Software Design 28
Members of the Derived Class Intense_point Member x y set_x set_y get_x get_y intensity set_intensity get_intensity Access status in Intense_point Not accessible Not accessible public public public public private public public How Obtained From class Point From class Point From class Point From class Point From class Point From class Point Added by class Intense_point Added by class Intense_point Added by class Intense_point CSC 330 OO Software Design 29
Adjusting Access Example 4.2.3. class BC { // base class void set_x( float a) { x = a; } private: float x; class DC : public BC { // derived class void set_y( float b) { y = b; } private: float y; CSC 330 OO Software Design 30
Method set_x is made private in class DC class BC { // base class void set_x( float a) { x = a; } private: float x; class DC : public BC { // derived class void set_y( float b) { y = b; } private: float y; using BC::set_x; int main ( ) { DC d ; d.set_y (4.31) ; // OK d.set_x (-8.03) ; // *** ERROR: set_x is private in DC // } CSC 330 OO Software Design 31
Name Hiding Example 4.2.4. class BC { // base class void h (float ) ; // BC : : h class DC : public BC { // derived class void h( char [ ] ) ; // *** DANGER: hides BC::h int main( ) { DC dl; dl.h( "Boffo! ) ; // DC::h, not BC::h dl.h( 707.7) ; // **** ERROR: DC::h hides BC::h dl.bc::h( 707.7) ; // OK: invokes BC::h //... CSC 330 OO Software Design 32
Indirect Inheritance: Example 4.2.5. // direct base class for Cat, // indirect base class for HouseCat class Animal { string species; float lifeexpdctancy; bool warmblooded_p; // direct derived class from Animal, //direct base class for HouseCat class Cat : public Animal { string range[ 100 ] ; float favoriteprey[ 100 ] [ 100 ]; // indirect derived class from Animal, // direct derived class from Cat class HouseCat : public Cat { string toys[ 10000 ]; string catpsychiatrist; string catdentist; string catdoctor; string apparentowner; CSC 330 OO Software Design 33
protected Members: Example 4.4.1 class BC { // base class void set_x( int a) { x = a; } protected: int get_x( ) const { return x; } private: int x; class DC : public BC { void add2( ) { int c = get_x( ); set_x( c + 2 ) ; } CSC 330 OO Software Design 34
Accessibility Table: Example 4.4.1 Member Access Status in DC How Obtained set_x public From class BC get_x protected From class BC x Not accessible From class BC add2 public Added by class DC CSC 330 OO Software Design 35
int main ( ) { DC d; d.set_x( 3) ; // Example 4.4.1. cont d OK se_x is public in DC // ***** ERROR: get_x is protected in DC cout << d.get_x( ) << \n ; d.x = 77; // ***** ERROR: x is private in BC d.add2( ); // OK -- add2 is public in DC //... } class DC : public BC { //... // ***** ERROR: x is accessible only within BC void add3( ) { x += 3; } //... CSC 330 OO Software Design 36
Example 4.4.2. class BC { // base class protected: int get_w ( ) const; //... class DC : public BC { // derived class // get_w belongs to DC because it is inherited from BC int get_val( ) const { return get_w( ); } // ***** ERROR: b.get_w not visible in DC since b.get_w is // a member of BC, not DC void base_w( const BC& b ) const { cout << b.get_w( ) << \n ;} CSC 330 OO Software Design 37
Example 4.4.3. class BC { void init_x( ) { x = 0; } protected: int get_x ( ) const { return x; } private: int x; class DC : public BC { void g( ) { init_x( ); cout << get_x( ) << \n ; } CSC 330 OO Software Design 38