C++ OOP :: Inheritance14/10/20091Hadziq Fabroyir - Informatics ITS
Classes CharacteristicClasses are used to accomplish:Modularity► Scope for global (static) methodsBlueprints for generating objects or instancesClasses support Data encapsulation - private data and implementation.Inheritance-code reuse14/10/2009Hadziq Fabroyir - Informatics ITS2
InheritanceInheritance allows a software developer to derive a new class from an existing one.14/10/2009Hadziq Fabroyir - Informatics ITS3
InheritanceThe existing class is called the parent, super, or baseclass.The derived class is called a child or subclass.14/10/2009Hadziq Fabroyir - Informatics ITS4
Inheritance The child inherits characteristics of the parent.
 The child has special rights to the parents methods and data.
Public access like any one else
Protected access available only to child classes (and their descendants).
The child has its own unique behaviors and data.14/10/2009Hadziq Fabroyir - Informatics ITS5
InheritanceInheritance relationships are often shown graphically in a classdiagram with the arrow pointing to the parent class.Inheritance should create an is-arelationship, meaning the child is a more specific version of the parent.14/10/2009Hadziq Fabroyir - Informatics ITS6AnimalBird
Examples: Base Classes and Derived Classes14/10/2009Hadziq Fabroyir - Informatics ITS7
Single / Multiple Inheritance ?Some languages, e.g., C++, allowMultiple inheritance, which allows a class to be derived from two or more classes, inheriting the members of all parents.C# and Java support only Single inheritance, meaning that a derived class can have only one parent class.14/10/2009Hadziq Fabroyir - Informatics ITS8
Class HierarchiesA child class of one parent can be the parent of another child, forming a class hierarchy14/10/2009Hadziq Fabroyir - Informatics ITS9
Classify It !14/10/2009Hadziq Fabroyir - Informatics ITS10
Class Hierarchies14/10/2009Hadziq Fabroyir - Informatics ITS11
Classify It !14/10/2009Hadziq Fabroyir - Informatics ITS12
Class Hierarchies14/10/2009Hadziq Fabroyir - Informatics ITS13
Class HierarchiesAn inherited member is continually passed down the lineInheritance is transitiveGood class design puts all common features as high in the hierarchy as is reasonable. Avoids redundant code.14/10/2009Hadziq Fabroyir - Informatics ITS14
References and InheritanceAn object reference can refer to an object of its class, or to an object of any class derived from it by inheritance.For example, if the 2DShape class is used to derive a child class called Triangle, then a 2DShape reference can be used to point to a Triangle object.2DShape myShape;myShape = new 2DShape();…myShape = new Triangle();14/10/2009Hadziq Fabroyir - Informatics ITS15
Overriding MethodsA child class can override the definition of an inherited method in favor of its ownThat is, a child can redefine a method that it inherits from its parentThe new method must have the same signature as the parent's method, but can have a different implementation.14/10/2009Hadziq Fabroyir - Informatics ITS16
Lect 28	P. 17Winter QuarterFunction OverloadingC++ supports writing more than one function with the same name but different argument lists.  This could include:different data typesdifferent number of argumentsThe advantage is that the same apparent function can be called to perform similar but different tasks.  The following will show an example of this.
Lect 28	P. 18Winter QuarterFunction Overloadingvoid swap (int *a, int *b);void swap (float *c, float *d);void swap (char *p, char *q);int main ( ) {int a = 4, b = 6 ;float c = 16.7, d = -7.89 ;char p = 'M' , q = 'n' ;	swap (&a, &b) ;	swap (&c, &d);	swap (&p, &q); }
Lect 28	P. 19Winter QuarterFunction Overloadingvoid swap (int *a, int *b){ int temp;  temp = *a;  *a = *b;  *b = temp; }void swap (float *c, float *d){float temp;  temp = *c;  *c = *d;  *d = temp;}void swap (char *p, char *q){char temp;  temp = *p;  *p = *q;  *q = temp;}
ComparisonOverloadingOverloadingdeals with multiple methods in the same class with the same name but different signaturesOverloadinglets you define a similar operation in different ways for different dataExample:int foo(string[] bar);intfoo(intbar1, floata);OverridingOverridingdeals with two methods, one in a parent class and one in a child class, that have the same signatureOverridinglets you define a similar operation in different ways for different object typesExample:class Base {publicvirtualintfoo() {} }class Derived {publicoverrideintfoo() {}}14/10/2009Hadziq Fabroyir - Informatics ITS20
Declaring a Derived ClassDefine a new class  DerivedClass which extends BaseClassclass BaseClass 	{// class contents	}  class DerivedClass : BaseClass  {// class contents	}14/10/2009Hadziq Fabroyir - Informatics ITS21
Declaring a Derived Classclass A : base classaccess specifier B{		member access specifier(s):	...		member data and member function(s);	...}Valid access specifiers include public, protected, and private14/10/2009Hadziq Fabroyir - Informatics ITS22
Public Inheritanceclass A : public B{		// Class A now inherits the members of Class B		// with no change in the “access specifier” for}		// the inherited members14/10/2009Hadziq Fabroyir - Informatics ITS23

#OOP_D_ITS - 6th - C++ Oop Inheritance

  • 1.
    C++ OOP ::Inheritance14/10/20091Hadziq Fabroyir - Informatics ITS
  • 2.
    Classes CharacteristicClasses areused to accomplish:Modularity► Scope for global (static) methodsBlueprints for generating objects or instancesClasses support Data encapsulation - private data and implementation.Inheritance-code reuse14/10/2009Hadziq Fabroyir - Informatics ITS2
  • 3.
    InheritanceInheritance allows asoftware developer to derive a new class from an existing one.14/10/2009Hadziq Fabroyir - Informatics ITS3
  • 4.
    InheritanceThe existing classis called the parent, super, or baseclass.The derived class is called a child or subclass.14/10/2009Hadziq Fabroyir - Informatics ITS4
  • 5.
    Inheritance The childinherits characteristics of the parent.
  • 6.
    The childhas special rights to the parents methods and data.
  • 7.
    Public access likeany one else
  • 8.
    Protected access availableonly to child classes (and their descendants).
  • 9.
    The child hasits own unique behaviors and data.14/10/2009Hadziq Fabroyir - Informatics ITS5
  • 10.
    InheritanceInheritance relationships areoften shown graphically in a classdiagram with the arrow pointing to the parent class.Inheritance should create an is-arelationship, meaning the child is a more specific version of the parent.14/10/2009Hadziq Fabroyir - Informatics ITS6AnimalBird
  • 11.
    Examples: Base Classesand Derived Classes14/10/2009Hadziq Fabroyir - Informatics ITS7
  • 12.
    Single / MultipleInheritance ?Some languages, e.g., C++, allowMultiple inheritance, which allows a class to be derived from two or more classes, inheriting the members of all parents.C# and Java support only Single inheritance, meaning that a derived class can have only one parent class.14/10/2009Hadziq Fabroyir - Informatics ITS8
  • 13.
    Class HierarchiesA childclass of one parent can be the parent of another child, forming a class hierarchy14/10/2009Hadziq Fabroyir - Informatics ITS9
  • 14.
    Classify It !14/10/2009HadziqFabroyir - Informatics ITS10
  • 15.
  • 16.
    Classify It !14/10/2009HadziqFabroyir - Informatics ITS12
  • 17.
  • 18.
    Class HierarchiesAn inheritedmember is continually passed down the lineInheritance is transitiveGood class design puts all common features as high in the hierarchy as is reasonable. Avoids redundant code.14/10/2009Hadziq Fabroyir - Informatics ITS14
  • 19.
    References and InheritanceAnobject reference can refer to an object of its class, or to an object of any class derived from it by inheritance.For example, if the 2DShape class is used to derive a child class called Triangle, then a 2DShape reference can be used to point to a Triangle object.2DShape myShape;myShape = new 2DShape();…myShape = new Triangle();14/10/2009Hadziq Fabroyir - Informatics ITS15
  • 20.
    Overriding MethodsA childclass can override the definition of an inherited method in favor of its ownThat is, a child can redefine a method that it inherits from its parentThe new method must have the same signature as the parent's method, but can have a different implementation.14/10/2009Hadziq Fabroyir - Informatics ITS16
  • 21.
    Lect 28 P. 17WinterQuarterFunction OverloadingC++ supports writing more than one function with the same name but different argument lists. This could include:different data typesdifferent number of argumentsThe advantage is that the same apparent function can be called to perform similar but different tasks. The following will show an example of this.
  • 22.
    Lect 28 P. 18WinterQuarterFunction Overloadingvoid swap (int *a, int *b);void swap (float *c, float *d);void swap (char *p, char *q);int main ( ) {int a = 4, b = 6 ;float c = 16.7, d = -7.89 ;char p = 'M' , q = 'n' ; swap (&a, &b) ; swap (&c, &d); swap (&p, &q); }
  • 23.
    Lect 28 P. 19WinterQuarterFunction Overloadingvoid swap (int *a, int *b){ int temp; temp = *a; *a = *b; *b = temp; }void swap (float *c, float *d){float temp; temp = *c; *c = *d; *d = temp;}void swap (char *p, char *q){char temp; temp = *p; *p = *q; *q = temp;}
  • 24.
    ComparisonOverloadingOverloadingdeals with multiplemethods in the same class with the same name but different signaturesOverloadinglets you define a similar operation in different ways for different dataExample:int foo(string[] bar);intfoo(intbar1, floata);OverridingOverridingdeals with two methods, one in a parent class and one in a child class, that have the same signatureOverridinglets you define a similar operation in different ways for different object typesExample:class Base {publicvirtualintfoo() {} }class Derived {publicoverrideintfoo() {}}14/10/2009Hadziq Fabroyir - Informatics ITS20
  • 25.
    Declaring a DerivedClassDefine a new class DerivedClass which extends BaseClassclass BaseClass {// class contents } class DerivedClass : BaseClass {// class contents }14/10/2009Hadziq Fabroyir - Informatics ITS21
  • 26.
    Declaring a DerivedClassclass A : base classaccess specifier B{ member access specifier(s): ... member data and member function(s); ...}Valid access specifiers include public, protected, and private14/10/2009Hadziq Fabroyir - Informatics ITS22
  • 27.
    Public Inheritanceclass A: public B{ // Class A now inherits the members of Class B // with no change in the “access specifier” for} // the inherited members14/10/2009Hadziq Fabroyir - Informatics ITS23

Editor's Notes

  • #6 The child inherits characteristics of the parent.Methods and data defined for the parent class.The child has special rights to the parents methods and data.Public access like any one elseProtected access available only to child classes (and their descendants).The child has its own unique behaviors and data.