INHERITANCE - Part 1. CSC 330 OO Software Design 1

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

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

Programming, numerics and optimization

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

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (Deitel chapter 9)

Konzepte von Programmiersprachen

ITI Introduction to Computing II

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

ITI Introduction to Computing II

JAVA MOCK TEST JAVA MOCK TEST II

CMSC 132: Object-Oriented Programming II

CS250 Final Review Questions

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

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

CLASSES AND OBJECTS IN JAVA

Object-Oriented Programming Concepts

Lecture 10: Introduction to Inheritance

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

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Abstract Data Types (ADT) and C++ Classes

PIC 10A. Lecture 15: User Defined Classes

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

Inheritance and Polymorphism

Data Structures and Other Objects Using C++

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

INHERITANCE: EXTENDING CLASSES

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

CS250 Final Review Questions

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

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

Programming II (CS300)

What are the characteristics of Object Oriented programming language?

CMSC 132: Object-Oriented Programming II

Classes. Christian Schumacher, Info1 D-MAVT 2013

G Programming Languages - Fall 2012

Programming Languages and Techniques (CIS120)

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

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

CS1004: Intro to CS in Java, Spring 2005

Chapter 6 Introduction to Defining Classes

CS-202 Introduction to Object Oriented Programming

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

Object Oriented Programming. Solved MCQs - Part 2

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

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

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

C++ Important Questions with Answers

2015 Academic Challenge

Data Structures (INE2011)

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

Chapter 9 - Object-Oriented Programming: Inheritance

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

C++ (classes) Hwansoo Han

Introduction to Programming

Chapter 6. Object- Oriented Programming Part II

Arrays Classes & Methods, Inheritance

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

COP 3330 Final Exam Review

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

Inheritance Motivation

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance -- Introduction

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

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

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

Object-Oriented Programming (Java)

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

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

CS422 - Programming Language Design

Programming Languages and Techniques (CIS120)

CS11 Introduction to C++ Fall Lecture 7

Chapter 3: Inheritance and Polymorphism

Data Abstraction. Hwansoo Han

CS 251 Intermediate Programming Inheritance

Computer Science II (20073) Week 1: Review and Inheritance

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

Programming II (CS300)

Programming Exercise 14: Inheritance and Polymorphism

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

An Introduction to C++

Classes, Objects, and OOP in Java. June 16, 2017

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

OBJECT ORİENTATİON ENCAPSULATİON

Data Structures using OOP C++ Lecture 6


Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

CSE 307: Principles of Programming Languages

Programming in C++: Assignment Week 3

Chapter 9 - Object-Oriented Programming: Polymorphism

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

Transcription:

INHERITANCE - Part 1 Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors CSC 330 OO Software Design 1

OOP Major Capabilities encapsulation inheritance polymorphism which can improve the design, structure and reusability of code. CSC 330 OO Software Design 2

Main Benefit - code reuse: 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!) CSC 330 OO Software Design 3

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 4

CSC 330 OO Software Design 5

CSC 330 OO Software Design 6

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

Discussions 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. CSC 330 OO Software Design 8

Discussions cont d 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

Discussions cont d 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 10

Inheritance (same as saying Derivation) 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. CSC 330 OO Software Design 11

Definition (Inheritance): 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 12

Inheritance Basic Definitions Superclasses are also called parent classes or base classes. Subclasses may also be called child classes or just derived classes. CSC 330 OO Software Design 13

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 : public BC { //... CSC 330 OO Software Design 14

Example 4.2.1 class Pen { enum ink ( off, on ); void set-status( ink ); void set-location( int, int ); private: int x; int y; int status; }; CSC 330 OO Software Design 15

Example 4.2.1 cont d class CPen : public Pen { void set_color ( int ) ; private: int color ; CSC 330 OO Software Design 16

private Members in Inheritance 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 17

Fig. 4.2.1. Deriving one class from another CSC 330 OO Software Design 18

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; } private: int x; int y; }; class Intense_point : public Point { void set_intensity ( int I ) {intensity = I ; } int get_intensity ( ) const { return intensity ; } private: int intensity ; CSC 330 OO Software Design 19

Discussions In Example 4.2.1, data members 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 20

Members of the Derived Class Intense_point 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 public public public public private public public How Obtained From class Point From class Point From class Point From class Point From class Point From class Point Added by class Intense_point Added by class Intense_point Added by class Intense_point CSC 330 OO Software Design 21

Adjusting Access Example 4.2.3. class BC { // base class void set_x( float a) { x = a; } private: float x; class DC : public BC { // derived class void set_y( float b) { y = b; } private: float y; CSC 330 OO Software Design 22

Method set_x is made private in class DC class BC { // base class void set_x( float a) { x = a; } private: float x; class DC : public BC { // derived class void set_y( float b) { y = b; } private: 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 // } CSC 330 OO Software Design 23

Name Hiding Example 4.2.4. class BC { // base class void h (float ) ; // BC : : h class DC : public 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 24

Indirect Inheritance: Example 4.2.5. // 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 : public Animal { string range[ 100 ] ; float favoriteprey[ 100 ] [ 100 ]; // indirect derived class from Animal, // direct derived class from Cat class HouseCat : public Cat { string toys[ 10000 ]; string catpsychiatrist; string catdentist; string catdoctor; string apparentowner; CSC 330 OO Software Design 25

protected Members: Example 4.4.1 class BC { // base class void set_x( int a) { x = a; } protected: int get_x( ) const { return x; } private: int x; class DC : public BC { void add2( ) { int c = get_x( ); set_x( c + 2 ) ; } CSC 330 OO Software Design 26

Accessibility Table: Example 4.4.1 Member Access Status in DC How Obtained set_x public From class BC get_x protected From class BC x Not accessible From class BC add2 public Added by class DC CSC 330 OO Software Design 27

int main ( ) { DC d; d.set_x( 3) ; // Example 4.4.1. cont d OK se_x is public in DC // ***** 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 public in DC //... } class DC : public BC { //... // ***** ERROR: x is accessible only within BC void add3( ) { x += 3; } //... CSC 330 OO Software Design 28

Example 4.4.2. class BC { // base class protected: int get_w ( ) const; //... class DC : public 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 ;} CSC 330 OO Software Design 29

Example 4.4.3. class BC { void init_x( ) { x = 0; } protected: int get_x ( ) const { return x; } private: int x; class DC : public BC { void g( ) { init_x( ); cout << get_x( ) << \n ; } CSC 330 OO Software Design 30