#oop_d_its - 6th - c++ oop inheritance

27
C++ OOP :: Inheritance 07/06/2022 1 Hadziq Fabroyir - Informatics ITS

Upload: hadziq-fabroyir

Post on 18-Jan-2015

1.239 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: #OOP_D_ITS - 6th - C++ Oop Inheritance

10/04/2023Hadziq Fabroyir - Informatics ITS 1

C++ OOP :: Inheritance

Page 2: #OOP_D_ITS - 6th - 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

Page 3: #OOP_D_ITS - 6th - C++ Oop Inheritance

10/04/2023Hadziq Fabroyir - Informatics ITS 3

InheritanceInheritance allows a software developer

to derive a new class from an existing one.

Page 4: #OOP_D_ITS - 6th - C++ Oop Inheritance

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.

Page 5: #OOP_D_ITS - 6th - C++ Oop Inheritance

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.

Page 6: #OOP_D_ITS - 6th - C++ Oop Inheritance

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

Page 7: #OOP_D_ITS - 6th - C++ Oop Inheritance

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

Page 8: #OOP_D_ITS - 6th - C++ Oop Inheritance

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.

Page 9: #OOP_D_ITS - 6th - C++ Oop Inheritance

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

Page 10: #OOP_D_ITS - 6th - C++ Oop Inheritance

10/04/2023Hadziq Fabroyir - Informatics ITS 10

Classify It !

Triangle Circle 2D Shape

Cube Sphere 3D Shape

Square Cylinder Shape

Page 11: #OOP_D_ITS - 6th - C++ Oop Inheritance

10/04/2023Hadziq Fabroyir - Informatics ITS 11

Class Hierarchies

Page 12: #OOP_D_ITS - 6th - C++ Oop Inheritance

10/04/2023Hadziq Fabroyir - Informatics ITS 12

Classify It !

Employee

Instructor

Graduate

Staff Professor

Alumnus

Under Graduate

Community Member Faculty

Student

Page 13: #OOP_D_ITS - 6th - C++ Oop Inheritance

10/04/2023Hadziq Fabroyir - Informatics ITS 13

Class Hierarchies

Page 14: #OOP_D_ITS - 6th - C++ Oop Inheritance

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.

Page 15: #OOP_D_ITS - 6th - C++ Oop Inheritance

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();

Page 16: #OOP_D_ITS - 6th - C++ Oop Inheritance

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.

Page 17: #OOP_D_ITS - 6th - C++ Oop Inheritance

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.

Page 18: #OOP_D_ITS - 6th - C++ Oop Inheritance

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) ;

}

Page 19: #OOP_D_ITS - 6th - C++ Oop Inheritance

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; }

Page 20: #OOP_D_ITS - 6th - C++ Oop Inheritance

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() {}}

Page 21: #OOP_D_ITS - 6th - C++ Oop Inheritance

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

}

Page 22: #OOP_D_ITS - 6th - C++ Oop Inheritance

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

Page 23: #OOP_D_ITS - 6th - C++ Oop Inheritance

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

Page 24: #OOP_D_ITS - 6th - C++ Oop Inheritance

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

Page 25: #OOP_D_ITS - 6th - C++ Oop Inheritance

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

Page 26: #OOP_D_ITS - 6th - C++ Oop Inheritance

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)

Page 27: #OOP_D_ITS - 6th - C++ Oop Inheritance

10/04/2023Hadziq Fabroyir - Informatics ITS

☺~ Next: Virtual & Polymorphism ~☺

[ 27 ]