#OOP_D_ITS - 6th - C++ Oop Inheritance

1,758 views

Published on

Published in: Technology
3 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total views
1,758
On SlideShare
0
From Embeds
0
Number of Embeds
139
Actions
Shares
0
Downloads
144
Comments
3
Likes
2
Embeds 0
No embeds

No notes for slide
  • 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.
  • #OOP_D_ITS - 6th - C++ Oop Inheritance

    1. 1. C++ OOP :: Inheritance<br />14/10/2009<br />1<br />Hadziq Fabroyir - Informatics ITS<br />
    2. 2. Classes Characteristic<br />Classes are used to accomplish:<br />Modularity► Scope for global (static) methods<br />Blueprints for generating objects or instances<br />Classes support <br />Data encapsulation - private data and implementation.<br />Inheritance-code reuse<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />2<br />
    3. 3. Inheritance<br />Inheritance allows a software developer to derive a new class from an existing one.<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />3<br />
    4. 4. Inheritance<br />The existing class is called the parent, super, or baseclass.<br />The derived class is called a child or subclass.<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />4<br />
    5. 5. Inheritance<br /><ul><li> The child inherits characteristics of the parent.
    6. 6. The child has special rights to the parents methods and data.
    7. 7. Public access like any one else
    8. 8. Protected access available only to child classes (and their descendants).
    9. 9. The child has its own unique behaviors and data.</li></ul>14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />5<br />
    10. 10. Inheritance<br />Inheritance relationships are often shown graphically in a classdiagram with the arrow pointing to the parent class.<br />Inheritance should create an is-arelationship, meaning the child is a more specific version of the parent.<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />6<br />Animal<br />Bird<br />
    11. 11. Examples: Base Classes and Derived Classes<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />7<br />
    12. 12. Single / Multiple Inheritance ?<br />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.<br />C# and Java support only Single inheritance, meaning that a derived class can have only one parent class.<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />8<br />
    13. 13. Class Hierarchies<br />A child class of one parent can be the parent of another child, forming a class hierarchy<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />9<br />
    14. 14. Classify It !<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />10<br />
    15. 15. Class Hierarchies<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />11<br />
    16. 16. Classify It !<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />12<br />
    17. 17. Class Hierarchies<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />13<br />
    18. 18. Class Hierarchies<br />An inherited member is continually passed down the line<br />Inheritance is transitive<br />Good class design puts all common features as high in the hierarchy as is reasonable. Avoids redundant code.<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />14<br />
    19. 19. References and Inheritance<br />An object reference can refer to an object of its class, or to an object of any class derived from it by inheritance.<br />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.<br />2DShape myShape;<br />myShape = new 2DShape();<br />…<br />myShape = new Triangle();<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />15<br />
    20. 20. Overriding Methods<br />A child class can override the definition of an inherited method in favor of its own<br />That is, a child can redefine a method that it inherits from its parent<br />The new method must have the same signature as the parent&apos;s method, but can have a different implementation.<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />16<br />
    21. 21. Lect 28 P. 17<br />Winter Quarter<br />Function Overloading<br />C++ supports writing more than one function with the same name but different argument lists. This could include:<br />different data types<br />different number of arguments<br />The advantage is that the same apparent function can be called to perform similar but different tasks. The following will show an example of this.<br />
    22. 22. Lect 28 P. 18<br />Winter Quarter<br />Function Overloading<br />void swap (int *a, int *b);<br />void swap (float *c, float *d);<br />void swap (char *p, char *q);<br />int main ( ) <br />{<br />int a = 4, b = 6 ;<br />float c = 16.7, d = -7.89 ;<br />char p = &apos;M&apos; , q = &apos;n&apos; ;<br /> swap (&a, &b) ;<br /> swap (&c, &d);<br /> swap (&p, &q);<br /> }<br />
    23. 23. Lect 28 P. 19<br />Winter Quarter<br />Function Overloading<br />void swap (int *a, int *b)<br />{ int temp; temp = *a; *a = *b; *b = temp; }<br />void swap (float *c, float *d)<br />{float temp; temp = *c; *c = *d; *d = temp;}<br />void swap (char *p, char *q)<br />{char temp; temp = *p; *p = *q; *q = temp;}<br />
    24. 24. Comparison<br />Overloading<br />Overloadingdeals with multiple methods in the same class with the same name but different signatures<br />Overloadinglets you define a similar operation in different ways for different data<br />Example:<br />int foo(string[] bar);<br />intfoo(intbar1, floata);<br />Overriding<br />Overridingdeals with two methods, one in a parent class and one in a child class, that have the same signature<br />Overridinglets you define a similar operation in different ways for different object types<br />Example:<br />class Base {<br />publicvirtualintfoo() {} }<br />class Derived {<br />publicoverrideintfoo() {}}<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />20<br />
    25. 25. Declaring a Derived Class<br />Define a new class DerivedClass which extends BaseClass<br />class BaseClass<br /> {<br />// class contents<br /> }<br /> class DerivedClass : BaseClass<br /> {<br />// class contents<br /> }<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />21<br />
    26. 26. Declaring a Derived Class<br />class A : base classaccess specifier B<br />{ <br />member access specifier(s):<br /> ...<br /> member data and member function(s);<br /> ...<br />}<br />Valid access specifiers include public, protected, and private<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />22<br />
    27. 27. Public Inheritance<br />class A : public B<br />{ // Class A now inherits the members of Class B<br /> // with no change in the “access specifier” for<br />} // the inherited members<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />23<br />
    28. 28. Protected Inheritance<br />class A : protected B<br />{ // Class A now inherits the members of Class B<br /> // with public members “promoted” to protected<br />} // but no other changes to the inherited members<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />24<br />
    29. 29. Private Inheritance<br />class A : private B<br />{ // Class A now inherits the members of Class B<br /> // with public and protected members<br />} // “promoted” to private<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />25<br />
    30. 30. For your practice …<br />Exercise<br />Declare Classes of ...<br />Shape (ex: public getColor(), protected color)<br />2D Shape, 3D Shape (ex: public getArea(), protected edgeLength)<br />Triangle, Circle, Square; Cube, Cylinder, Sphere<br />Lab Session for lower order (Senin, 15.00-17.00)<br />Lab Session for upper order (Senin, 19.00-21.00)<br />Please provide:<br /> the softcopy(send it by email – deadline Sunday 23:59)<br /> the hardcopy(bring it when attending the lab session)<br />14/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />26<br />
    31. 31. ☺~ Next: Virtual & Polymorphism ~☺<br />[ 27 ]<br />Hadziq Fabroyir - Informatics ITS<br />14/10/2009<br />

    ×