Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
#OOP_D_ITS - 6th - C++ Oop Inheritance
1. C++ OOP :: Inheritance 14/10/2009 1 Hadziq Fabroyir - Informatics ITS
2. Classes Characteristic Classes are used to accomplish: Modularity► Scope for global (static) methods Blueprints for generating objects or instances Classes support Data encapsulation - private data and implementation. Inheritance-code reuse 14/10/2009 Hadziq Fabroyir - Informatics ITS 2
3. Inheritance Inheritance allows a software developer to derive a new class from an existing one. 14/10/2009 Hadziq Fabroyir - Informatics ITS 3
4. Inheritance The existing class is called the parent, super, or baseclass. The derived class is called a child or subclass. 14/10/2009 Hadziq Fabroyir - Informatics ITS 4
5.
6. The child has special rights to the parents methods and data.
9. The child has its own unique behaviors and data.14/10/2009 Hadziq Fabroyir - Informatics ITS 5
10. Inheritance Inheritance 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/2009 Hadziq Fabroyir - Informatics ITS 6 Animal Bird
11. Examples: Base Classes and Derived Classes 14/10/2009 Hadziq Fabroyir - Informatics ITS 7
12. 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/2009 Hadziq Fabroyir - Informatics ITS 8
13. Class Hierarchies A child class of one parent can be the parent of another child, forming a class hierarchy 14/10/2009 Hadziq Fabroyir - Informatics ITS 9
14. Classify It ! 14/10/2009 Hadziq Fabroyir - Informatics ITS 10
18. Class Hierarchies An inherited member is continually passed down the line Inheritance is transitive Good class design puts all common features as high in the hierarchy as is reasonable. Avoids redundant code. 14/10/2009 Hadziq Fabroyir - Informatics ITS 14
19. References and Inheritance An 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/2009 Hadziq Fabroyir - Informatics ITS 15
20. Overriding Methods A child class can override the definition of an inherited method in favor of its own That is, a child can redefine a method that it inherits from its parent The new method must have the same signature as the parent's method, but can have a different implementation. 14/10/2009 Hadziq Fabroyir - Informatics ITS 16
21. Lect 28 P. 17 Winter Quarter Function Overloading C++ supports writing more than one function with the same name but different argument lists. This could include: different data types different number of arguments 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.
22. Lect 28 P. 18 Winter Quarter Function Overloading void 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); }
24. Comparison Overloading Overloadingdeals with multiple methods in the same class with the same name but different signatures Overloadinglets you define a similar operation in different ways for different data Example: int foo(string[] bar); intfoo(intbar1, floata); Overriding Overridingdeals with two methods, one in a parent class and one in a child class, that have the same signature Overridinglets you define a similar operation in different ways for different object types Example: class Base { publicvirtualintfoo() {} } class Derived { publicoverrideintfoo() {}} 14/10/2009 Hadziq Fabroyir - Informatics ITS 20
25. Declaring a Derived Class Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class contents } 14/10/2009 Hadziq Fabroyir - Informatics ITS 21
26. Declaring a Derived Class class A : base classaccess specifier B { member access specifier(s): ... member data and member function(s); ... } Valid access specifiers include public, protected, and private 14/10/2009 Hadziq Fabroyir - Informatics ITS 22
27. Public Inheritance class A : public B { // Class A now inherits the members of Class B // with no change in the “access specifier” for } // the inherited members 14/10/2009 Hadziq Fabroyir - Informatics ITS 23
28. Protected Inheritance class A : protected B { // Class A now inherits the members of Class B // with public members “promoted” to protected } // but no other changes to the inherited members 14/10/2009 Hadziq Fabroyir - Informatics ITS 24
29. Private Inheritance class A : private B { // Class A now inherits the members of Class B // with public and protected members } // “promoted” to private 14/10/2009 Hadziq Fabroyir - Informatics ITS 25
30. For your practice … Exercise Declare Classes of ... Shape (ex: public getColor(), protected color) 2D Shape, 3D Shape (ex: public getArea(), protected edgeLength) Triangle, Circle, Square; Cube, Cylinder, Sphere Lab Session for lower order (Senin, 15.00-17.00) Lab Session for upper order (Senin, 19.00-21.00) Please provide: the softcopy(send it by email – deadline Sunday 23:59) the hardcopy(bring it when attending the lab session) 14/10/2009 Hadziq Fabroyir - Informatics ITS 26
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.