Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

Similar documents
INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1

C++ Programming: Inheritance

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Inheritance, and Polymorphism.

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Programming, numerics and optimization

CMSC 132: Object-Oriented Programming II

Inheritance (Deitel chapter 9)

! "# $ $ % # & ' ( ) * +,! ) -

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Konzepte von Programmiersprachen

ITI Introduction to Computing II

CS250 Final Review Questions

Java Object Oriented Design. CSC207 Fall 2014

ITI Introduction to Computing II

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

CLASSES AND OBJECTS IN JAVA

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

CMSC 132: Object-Oriented Programming II

C++ (classes) Hwansoo Han

Inheritance and Polymorphism

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

JAVA MOCK TEST JAVA MOCK TEST II

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

More C++ : Vectors, Classes, Inheritance, Templates

CS250 Final Review Questions

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Lecture 10: Introduction to Inheritance

CS105 C++ Lecture 7. More on Classes, Inheritance

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CS1004: Intro to CS in Java, Spring 2005

What are the characteristics of Object Oriented programming language?

Chapter 6 Introduction to Defining Classes

Reusing Classes. Hendrik Speleers

COURSE 2 DESIGN PATTERNS

CS-202 Introduction to Object Oriented Programming

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

PIC 10A. Lecture 15: User Defined Classes

INHERITANCE: EXTENDING CLASSES

Data Structures and Other Objects Using C++

Chapter 6. Object- Oriented Programming Part II

Abstract Data Types (ADT) and C++ Classes

Programming Languages and Techniques (CIS120)

Computer Programming Inheritance 10 th Lecture

Chapter 10 Classes Continued. Fundamentals of Java

CSE 307: Principles of Programming Languages

Chapter 9 - Object-Oriented Programming: Inheritance

Data Structures (INE2011)

Programming II (CS300)

Object-Oriented Programming Concepts


Outline. Outline. 1 Chapter 2: Data Abstraction

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

Object Oriented Programming

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Classes. Christian Schumacher, Info1 D-MAVT 2013

OBJECT ORİENTATİON ENCAPSULATİON

Objects and Classes: Working with the State and Behavior of Objects

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

ECE 3574: Dynamic Polymorphism using Inheritance

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

G Programming Languages - Fall 2012

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT OBJECT ORIENTED PROGRAMMING

index.pdf January 21,

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Object Oriented Programming. Solved MCQs - Part 2

Introduction to Programming

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Chapter 3: Inheritance and Polymorphism

Inheritance Motivation

ITI Introduction to Computing II

CS11 Introduction to C++ Fall Lecture 7

Ch 14. Inheritance. May 14, Prof. Young-Tak Kim

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

ITI Introduction to Computing II

Data Abstraction. Hwansoo Han

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Programming Languages and Techniques (CIS120)

Programming in C# Inheritance and Polymorphism

2015 Academic Challenge

Today s lecture. CS 314 fall 01 C++ 1, page 1

Polymorphism Part 1 1

Chapter 19 - C++ Inheritance

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Information System Design (IT60105)

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

CS250 Final Review Questions

PROGRAMMING IN C++ COURSE CONTENT

Transcription:

INHERITANCE - Part 1 OOP Major Capabilities Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors encapsulation inheritance polymorphism which can improve the design, structure and reusability of code. CSC 330 OO Software Design 1 CSC 330 OO Software Design 2 Main Benefit - code reuse: Objectives Using inheritance to promote software reusability No need to replicate class methods (the subclass can inherit those of the superclass). No need to replicate functions with parameters of the superclass type; they will work fine when passed actual parameters of the subclass type (because every object of the subclass type IS-A object of the superclass type, too!) 3 CSC 330 OO Software Design 4 When using Inheritance? When you have a class C, and you want another class D, that does everything that C does and more: use inheritance! CSC 330 OO Software Design 5 CSC 330 OO Software Design 6 1

Example class Point { private : // attributes int x, y ; // methods void setx ( int newx ) ; int getx ( ) ; void sety ( int newy ) ; int gety ( ) ; class Circle { private : // attributes int x, y, radius ; // methods void setx ( int newx ) ; int getx ( ) ; void sety ( int newy ) ; int gety ( ) ; void setrad ( int newrad ) ; int getrad ( ) ; CSC 330 OO Software Design 7 CSC 330 OO Software Design 8 Discussions Discussions cont d Comparing both classes definitions, we can observe the following: Both classes have two data elements x and y. In the class Point these elements describe the position of the point, in the class Circle, they describe the circle center. Thus, x and y have the same meaning in both classes: They describe the position of their associated object by defining a point. Both classes offer the same set methods to get and set the value of two data elements, x and y. Class Circle "adds" a new data element radius and corresponding access methods. We can describe a circle as a point plus a radius. Thus a circle is "a-kind-of" point. However, a circle is somewhat more "specialized". CSC 330 OO Software Design 9 CSC 330 OO Software Design 10 Terminology Inheritance examples Base Class (sometimes called superclass class) Derived Class (sub( subclass) Single inheritance Multiple inheritance 3 kinds of inheritance: Public Protected Private Graduate ECE256 Undergraduate circle shape triangle Right-angled triangle CSC 330 OO Software Design 11 CSC 330 OO Software Design 12 2

Q&A How does inheritance work? What can be inherited from a base class? Can a derived class be a base class? Can a derived class add more attributes of its own? Is a derived class more specific than its base class? Is a derived class represents a smaller group of objects? CSC 330 OO Software Design 13 Access () Rules Access control within a class Access control among classes Public inheritance Protected inheritance Private inheritance CSC 330 OO Software Design 14 Public inheritance Basics Base class Public Protected Private The 3 most important capabilities of object-oriented oriented programming: Derived class Public Protected Private CSC 330 OO Software Design 15 CSC 330 OO Software Design 16 Relationships Discussions cont d Is a Has a Use a : inheritance : composition of other classes : non-private member functions Knows a : pointers to other objects In this and the following figures, classes are drawn using rectangles. The arrow line indicates the direction of the relation.it is to be read as "Circle is a- kind- of Point". CSC 330 OO Software Design 17 CSC 330 OO Software Design 18 3

Inheritance (same as saying Derivation) Definition (Inheritance): With inheritance, we are able to make use of the a-kind-of (is-a ) relationship. In our point and circle example, we can define a circle which inherits from point. Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say ``A inherits from B''. Objects of class A thus have access to attributes and methods of class B without the need to redefine them. Definition (Superclass/Subclass) If class A inherits from class B, then B is called superclass of A. A is called subclass of B. CSC 330 OO Software Design 19 CSC 330 OO Software Design 20 Inheritance Basic Definitions Superclasses are also called parent classes or base classes. Subclasses may also be called child classes or just derived classes. Basic Concepts and Syntax To derive class DC from class BC, we write: // BC is the base class; DC is the derived class class DC : BC { CSC 330 OO Software Design 21 CSC 330 OO Software Design 22 Example 4.2.1 class Pen { enum ink ( off, on ); void set-status( ink ); void set-location( int, int ); int y; int status; }; Example 4.2.1 cont d class CPen : Pen { void set_color ( int ) ; int color ; CSC 330 OO Software Design 23 CSC 330 OO Software Design 24 4

private Members in Inheritance Fig. 4.2.1. Deriving one class from another Each private member in a base class is visible only in the base class. In particular, a private member of a base class is not visible in a derived class. A private member of a base class is inherited by the derived class, but it is not visible in the derived class. CSC 330 OO Software Design 25 CSC 330 OO Software Design 26 private Members in Inheritance Example 4.2.2. class Point { void set_x( int x1 ) { x = xl; } void set_y( int yl ) { y = yl; } int get_x( ) const { return x; } int get_y( ) const { return y; } int y; }; class Intense_point : Point { void set_intensity ( int I ) {intensity = I ; } int get_intensity ( ) const { return intensity ; } int intensity ; Discussions In Example 4.2.1, data x, y, and status are inherited by CPen from Pen even though they are not visible in CPen. Whenever an object of type CPen is created, storage for x, y, and status is allocated. Although a private member of a base class cannot be directly accessed in a derived class, it might be indirectly accessed through a derived method. CSC 330 OO Software Design 27 CSC 330 OO Software Design 28 Members of the Derived Class Intense_point Adjusting Access Example 4.2.3. Member x y set_x set_y get_x get_y intensity set_intensity get_intensity Access status in Intense_point Not accessible Not accessible private How Obtained Added by class Intense_point Added by class Intense_point Added by class Intense_point void set_x( float a) { x = a; } float x; class DC : BC { // derived class void set_y( float b) { y = b; } float y; CSC 330 OO Software Design 29 CSC 330 OO Software Design 30 5

Method set_x is made private in class DC Name Hiding Example 4.2.4. void set_x( float a) { x = a; } float x; class DC : BC { // derived class void set_y( float b) { y = b; } float y; using BC::set_x; int main ( ) { DC d ; d.set_y (4.31) ; // OK d.set_x (-8.03) ; // *** ERROR: set_x is private in DC // } void h (float ) ; // BC : : h class DC : BC { // derived class void h( char [ ] ) ; // *** DANGER: hides BC::h int main( ) { DC dl; dl.h( "Boffo! ) ; // DC::h, not BC::h dl.h( 707.7) ; // **** ERROR: DC::h hides BC::h dl.bc::h( 707.7) ; // OK: invokes BC::h CSC 330 OO Software Design 31 CSC 330 OO Software Design 32 Indirect Inheritance: Example 4.2.5. protected Members: Example 4.4.1 // direct base class for Cat, // indirect base class for HouseCat class Animal { string species; float lifeexpdctancy; bool warmblooded_p; // direct derived class from Animal, //direct base class for HouseCat class Cat : Animal { string range[ 100 ] ; float favoriteprey[ 100 ] [ 100 ]; // indirect derived class from Animal, // direct derived class from Cat class HouseCat : Cat { string toys[ 10000 ]; string catpsychiatrist; string catdentist; string catdoctor; string apparentowner; CSC 330 OO Software Design 33 void set_x( int a) { x = a; } protected: int get_x( ) const { return x; } class DC : BC { void add2( ) { int c = get_x( ); set_x( c + 2 ) ; } CSC 330 OO Software Design 34 Accessibility Table: Example 4.4.1 Member Access Status in DC How Obtained Example 4.4.1. cont d int main ( ) { DC d; d.set_x( 3) ; // OK se_x is in DC set_x get_x protected From class BC From class BC // ***** ERROR: get_x is protected in DC cout << d.get_x( ) << \n ; d.x = 77; // ***** ERROR: x is private in BC d.add2( ); // OK -- add2 is in DC } x add2 Not accessible From class BC Added by class DC class DC : BC { // ***** ERROR: x is accessible only within BC void add3( ) { x += 3; } //... CSC 330 OO Software Design 35 CSC 330 OO Software Design 36 6

Example 4.4.2. protected: int get_w ( ) const; //... class DC : BC { // derived class // get_w belongs to DC because it is inherited from BC int get_val( ) const { return get_w( ); } // ***** ERROR: b.get_w not visible in DC since b.get_w is // a member of BC, not DC void base_w( const BC& b ) const { cout << b.get_w( ) << \n ;} Example 4.4.3. class BC { void init_x( ) { x = 0; } protected: int get_x ( ) const { return x; } class DC : BC { void g( ) { init_x( ); cout << get_x( ) << \n ; } CSC 330 OO Software Design 37 CSC 330 OO Software Design 38 7