Upload
hadziq-fabroyir
View
1.245
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
10/04/2023Hadziq Fabroyir - Informatics ITS 1
C++ OOP :: Inheritance
10/04/2023Hadziq Fabroyir - Informatics ITS 2
Classes Characteristic
Classes are used to accomplish:Modularity ► Scope for global (static) methodsBlueprints for generating objects or instances
Classes support Data encapsulation - private data and implementation.
Inheritance - code reuse
10/04/2023Hadziq Fabroyir - Informatics ITS 3
InheritanceInheritance allows a software developer
to derive a new class from an existing one.
10/04/2023Hadziq Fabroyir - Informatics ITS 4
Inheritance
The existing class is called the
parent, super, or base class.
The derived class is called a child or
subclass.
10/04/2023Hadziq Fabroyir - Informatics ITS 5
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.
10/04/2023Hadziq Fabroyir - Informatics ITS 6
InheritanceInheritance
relationships are often shown graphically in a class diagram with the arrow pointing to the
parent class.
Inheritance should
create an is-a relationship, meaning
the child is a more specific version of the
parent.
Animal
Bird
10/04/2023Hadziq Fabroyir - Informatics ITS 7
Examples: Base Classes and Derived Classes
Base class Derived classesStudent GraduateStudent
UndergraduateStudent
Shape CircleTriangleRectangle
Loan CarLoanHomeImprovementLoan MortgageLoan
Employee FacultyMember StaffMember
Account CheckingAccountSavingsAccount
10/04/2023Hadziq Fabroyir - Informatics ITS 8
Single / Multiple Inheritance ?
Some languages, e.g., C++, allow Multiple 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.
10/04/2023Hadziq Fabroyir - Informatics ITS 9
Class HierarchiesA child class of one parent can be the parent of
another child, forming a class hierarchy
10/04/2023Hadziq Fabroyir - Informatics ITS 10
Classify It !
Triangle Circle 2D Shape
Cube Sphere 3D Shape
Square Cylinder Shape
10/04/2023Hadziq Fabroyir - Informatics ITS 11
Class Hierarchies
10/04/2023Hadziq Fabroyir - Informatics ITS 12
Classify It !
Employee
Instructor
Graduate
Staff Professor
Alumnus
Under Graduate
Community Member Faculty
Student
10/04/2023Hadziq Fabroyir - Informatics ITS 13
Class Hierarchies
10/04/2023Hadziq Fabroyir - Informatics ITS 14
Class HierarchiesAn 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.
10/04/2023Hadziq Fabroyir - Informatics ITS 15
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();
10/04/2023Hadziq Fabroyir - Informatics ITS 16
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.
Winter QuarterLect 28
P. 17
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.
Winter QuarterLect 28
P. 18
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) ;
}
Winter QuarterLect 28
P. 19
Function Overloading
void 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; }
10/04/2023Hadziq Fabroyir - Informatics ITS 20
ComparisonOverloading
Overloading deals with multiple methods in the same class with the same name but different signatures
Overloading lets you define a similar operation in different ways for different data
Example:int foo(string[] bar);int foo(int bar1, float a);
OverridingOverriding deals with two methods, one in a parent class and one in a child class, that have the same signature
Overriding lets you define a similar operation in different ways for different object types
Example:class Base { public virtual int foo() {} }class Derived { public override int foo() {}}
10/04/2023Hadziq Fabroyir - Informatics ITS 21
Declaring a Derived Class
Define a new class DerivedClass which extends BaseClass
class BaseClass { // class contents
} class DerivedClass : BaseClass { // class contents
}
10/04/2023Hadziq Fabroyir - Informatics ITS 22
Declaring a Derived Class
class A : base class access specifier B{
member access specifier(s):...
member data and member function(s);...
}
Valid access specifiers include public, protected, and private
10/04/2023Hadziq Fabroyir - Informatics ITS 23
Public Inheritanceclass A : public B{ // Class A now inherits the members of Class B
// with no change in the “access specifier” for
} // the inherited members
10/04/2023Hadziq Fabroyir - Informatics ITS 24
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
10/04/2023Hadziq Fabroyir - Informatics ITS 25
Private Inheritance
class A : private B{ // Class A now inherits the members of Class B
// with public and protected members
} // “promoted” to private
10/04/2023Hadziq Fabroyir - Informatics ITS 26
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)
10/04/2023Hadziq Fabroyir - Informatics ITS
☺~ Next: Virtual & Polymorphism ~☺
[ 27 ]