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

Inheritance, and Polymorphism.

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

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

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

Inheritance (Deitel chapter 9)

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

Programming, numerics and optimization

CMSC 132: Object-Oriented Programming II

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

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

Chapter 6 Introduction to Defining Classes

C++ (classes) Hwansoo Han

Inheritance and Polymorphism

What are the characteristics of Object Oriented programming language?

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

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

JAVA MOCK TEST JAVA MOCK TEST II

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

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.

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

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

CS250 Final Review Questions

Computer Programming Inheritance 10 th Lecture

Abstract Data Types (ADT) and C++ Classes

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

Data Structures (INE2011)

Classes. Christian Schumacher, Info1 D-MAVT 2013

Lecture 10: Introduction to Inheritance

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

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

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

Chapter 9 - Object-Oriented Programming: Inheritance

CS1004: Intro to CS in Java, Spring 2005

Object Oriented Programming

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

Reusing Classes. Hendrik Speleers

COURSE 2 DESIGN PATTERNS

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

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

PIC 10A. Lecture 15: User Defined Classes

INHERITANCE: EXTENDING CLASSES

Data Structures and Other Objects Using C++

Data Abstraction. Hwansoo Han

Chapter 6. Object- Oriented Programming Part II

Inheritance Motivation

Programming Languages and Techniques (CIS120)

CSE 307: Principles of Programming Languages

Chapter 10 Classes Continued. Fundamentals of Java

Programming in C# Inheritance and Polymorphism

Programming II (CS300)


Object-Oriented Programming Concepts

Outline. Outline. 1 Chapter 2: Data Abstraction

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

CS1150 Principles of Computer Science Objects and Classes

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario


Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Make Classes Useful Again

OBJECT ORİENTATİON ENCAPSULATİON

ECE 3574: Dynamic Polymorphism using Inheritance

G Programming Languages - Fall 2012

PROGRAMMING IN C++ COURSE CONTENT

Chapter 19 - C++ Inheritance

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

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

Object Oriented Programming. Solved MCQs - Part 2

index.pdf January 21,

CSC1322 Object-Oriented Programming Concepts

Introduction to Programming

CS 251 Intermediate Programming Inheritance

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

Chapter 3: Inheritance and Polymorphism

ITI Introduction to Computing II

CS11 Introduction to C++ Fall Lecture 7

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

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

ITI Introduction to Computing II

Inheritance, Polymorphism and the Object Memory Model

Programming Languages and Techniques (CIS120)

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

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

Using inheritance to promote software reusability 3

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

CSC 330 OO Software Design 7

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 8

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 9

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 10

Terminology Base Class (sometimes called superclass class) Derived Class (sub( subclass) Single inheritance Multiple inheritance 3 kinds of inheritance: Public Protected Private CSC 330 OO Software Design 11

Inheritance examples student shape Graduate student Undergraduate student circle triangle ECE256 student Right-angled triangle CSC 330 OO Software Design 12

Q&A 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

How does inheritance work? Access (members) Rules Access control within a class Access control among classes Public inheritance Protected inheritance Private inheritance CSC 330 OO Software Design 14

Public inheritance Base class Public members Protected members Private members Derived class Public members Protected members Private members CSC 330 OO Software Design 15

Basics The 3 most important capabilities of object-oriented oriented programming: CSC 330 OO Software Design 16

Relationships Is a Has a Use a : public inheritance : composition of other classes : non-private member functions : pointers to other objects Knows a : pointers to other objects CSC 330 OO Software Design 17

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 18

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 19

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 20

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 21

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 22

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 23

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

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 25

Fig. 4.2.1. Deriving one class from another 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; } 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 27

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 28

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 29

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 30

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 31

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 32

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 33

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 34

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 35

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 36

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 37

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 38