Computer Programming Inheritance 10 th Lecture

Similar documents
Cpt S 122 Data Structures. Inheritance

C++ Polymorphism. Systems Programming

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Object Oriented Programming with C++ (24)

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

BBM 102 Introduction to Programming II Spring Inheritance

Inheritance Introduction. 9.1 Introduction 361

IS 0020 Program Design and Software Tools

WEEK 13 EXAMPLES: POLYMORPHISM

Object-Oriented Programming: Polymorphism

Inheritance (Deitel chapter 9)

final Methods and Classes

Object- Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism Pearson Education, Inc. All rights reserved.

Object Oriented Design

CS111: PROGRAMMING LANGUAGE II

Inheritance and Polymorphism

CS313D: ADVANCED PROGRAMMING LANGUAGE

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

CS313D: ADVANCED PROGRAMMING LANGUAGE

Solutions for H7. Lecture: Xu Ying Liu

Object-Oriented Programming: Polymorphism

CS111: PROGRAMMING LANGUAGE II

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

Polymorphism (Deitel chapter 10) (Old versions: chapter 9)

Lecture 5: Inheritance

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

G52CPP C++ Programming Lecture 13

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

Chapter 9 - Object-Oriented Programming: Inheritance

Friend Functions, Inheritance

Polymorphism Part 1 1

Polymorphism. Chapter 4. CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science. Dr. S.

INHERITANCE: CONSTRUCTORS,

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Programming in C# Inheritance and Polymorphism

ECE 3574: Dynamic Polymorphism using Inheritance

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

CS 162, Lecture 25: Exam II Review. 30 May 2018

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

Inheritance, and Polymorphism.

Inheritance and Interfaces

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Object-Oriented Programming. Lecture 4 Dr Piotr Cybula

VIRTUAL FUNCTIONS Chapter 10

Computer Programming C++ Classes and Objects 6 th Lecture

Object Oriented Programming

Midterm Exam 5 April 20, 2015

Inheritance. OOP: Inheritance 1

Object-Oriented Programming, Iouliia Skliarova

COEN244: Polymorphism

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2015 EOM, HYEONSANG ALL RIGHTS RESERVED

Object Oriented Programming. Java-Lecture 11 Polymorphism

INHERITANCE - Part 1. CSC 330 OO Software Design 1

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

OOP. Unit:3.3 Inheritance

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

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

A A B U n i v e r s i t y

Derived Classes in C++

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

Object Oriented Design

G52CPP C++ Programming Lecture 9

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

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

Banaras Hindu University

Classes: A Deeper Look

CHAPTER 9 INHERITANCE. 9.1 Introduction

Computer Programming Class Members 9 th Lecture

AN OVERVIEW OF C++ 1

Introduction to Programming session 24

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Extending Classes (contd.) (Chapter 15) Questions:

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Make Classes Useful Again

Inheritance and Interfaces

Programming, numerics and optimization

CSE 303 Lecture 23. Inheritance in C++ slides created by Marty Stepp

CS11 Introduction to C++ Fall Lecture 7

C++ Important Questions with Answers

Introduction to Classes

CS304 Object Oriented Programming Final Term

C++ Inheritance and Encapsulation

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

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

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Computer Programming with C++ (21)

Object Oriented Design

ECE 122. Engineering Problem Solving with Java

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Java Object Oriented Design. CSC207 Fall 2014

Short Notes of CS201

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

Chapter 19 - C++ Inheritance

Lecture 18 CSE11 Fall 2013 Inheritance

2 ADT Programming User-defined abstract data types

Transcription:

Computer Programming Inheritance 10 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2015 Eom, Hyeonsang All Rights Reserved

순서 Inheritance Public Protected Private Constructors under Inheritance Destructors under Inheritance Q&A

Inheritance Software reusability Create new class from existing class instead of building it entirely from the scratch Existing class s data and behaviors Adding new capabilities Derived class inherits from base class

Class hierarchy Direct base class Inherited explicitly (one level up hierarchy) Indirect base class Inherited two or more levels up hierarchy Single inheritance Inherits from one base class Multiple inheritance Inherits from multiple base classes Base classes possibly unrelated

Three Types of Inheritance public Every object of derived class is also an object of base class Base-class objects are not objects of derived classes All cars are vehicles, but not all vehicles are cars Can access non-private members of base class To access private base-class members Derived class must use inherited non-private member functions private (later) Alternative to composition protected (later) Rarely used

is-a vs has-a is-a Inheritance Derived class object can be treated as base class object Car is a vehicle has-a Vehicle properties/behaviors also apply to a car Composition Object contains one or more objects of other classes as members Car has a steering wheel

Base Classes and Derived Classes Object of one class is an object of another class Rectangle is quadrilateral Class Rectangle inherits from class Quadrilateral base class: quadrilateral derived class: rectangle Base class typically represents larger set of objects than derived classes Base class: Vehicle Derived class: Car

Base Classes and Derived Classes Cont d GraduatedStudent class is derived from Student class GraduatedStudent class is inherited from Student class Student class is super class of GraduatedStudent Graduated class is child class or subclass of Student class

Public Inheritance class TwoDimensionalShape : public Shape Class TwoDimensionalShape inherits from class Shape Base class private members Not accessible directly, but still inherited Accessed through inherited public member functions Base class public and protected members Inherited with original member access friend functions Not inherited class BaseClass //... class DerivedClass : public BaseClass //...

Public Inheritance Cont d class BaseClass void public_method(); protected: void protected_method(); void private_method(); class DerivedClass : public BaseClass void public_method(); protected: void protected_method();

Protected Access Specifier Intermediate level of protection between public and private Protected methods/data cannot be accessible by other classes except for subclasses Other classes consider protected members as normal private members Subclasses consider protected members as normal public members

Protected Access Specifier Cont d Protected members are accessible to Base class members Base class friends Derived class members Derived class friends

Protected Access Specifier Cont d Public members in base class is public in derived class Protected members in base class are protected in derived class Derived-class members Refer to public and protected members of base class Simply use member names Redefined base class members can be accessed by using base-class name and binary scope resolution operator (::)

Protected Inheritance public and protected members in base class become protected in derived class class BaseClass void public_method(); protected: void protected_method(); void private_method(); class DerivedClass : protected BaseClass protected: void public_method(); protected: void protected_method();

Private Inheritance class BaseClass void public_method(); protected: void protected_method(); void private_method(); class DerivedClass : private BaseClass void public_method(); void protected_method();

Protected Data Members Advantages Derived class can modify values directly No set/get method call overhead Disadvantages No validity checking Derived class can assign invalid value Implementation dependent Derived class more likely dependent on base class implementation Base class implementation may result in derived class s modification fragile software

Class CommissionEmployee #ifndef COMMISSION_H #define COMMISSION_H #include <string> using std::string; class CommissionEmployee CommissionEmployee( const string &, const string &, const string &, double = 0.0, double = 0.0 ); void setfirstname( const string & ); string getfirstname() const; void setlastname( const string & ); string getlastname() const; void setsocialsecuritynumber( const string & ); string getsocialsecuritynumber() const; void setgrosssales( double ); double getgrosssales() const; void setcommissionrate( double ); double getcommissionrate() const; double earnings() const; void print() const; protected: string firstname; string lastname; string socialsecuritynumber; double grosssales; double commissionrate; #endif double CommissionEmployee::earnings() const return commissionrate * grosssales; }

Class BasePlusCommissionEmployee #ifndef BASEPLUS_H void setbasesalary( double ); #define BASEPLUS_H double getbasesalary() const; #include <string> // C++ standard string class using std::string; #include "CommissionEmployee.h" class BasePlusCommissionEmployee : public CommissionEmployee BasePlusCommissionEmployee( c onst string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0 ); double earnings() const; void print() const; double basesalary; #endif double BasePlusCommissionEmployee::e arnings() const return basesalary + ( commissionrate * grosssales ); }

The Best Software Engineering Practice Declare data members as private Enables programmers to change the base-class implementation without having to change derived-class implementations Use the protected access specifier when a base class should provide a service (i.e., a member function) only to its derived classes (and friends), not to other clients Provide public get and set functions Use get method to obtain values of data members

The Best Software Engineering Practice Cont d Set/get method slightly slower than direct access But today s optimizing compiler inlines set/get methods Or you can explicitly specify inline keyword class BaseClass inline int getx()const return x; } inline void setx( int v ) } if( v > 100 ) error(); else x = v; int x; }

Selection of public/protected/private Methods According to the service range for all other classes : public for itself and subclasses : protected only for itself : private

Constructors under Inheritance Constructor in base class Does not construct derived class specific parts Constructor in derived class Initialize its own data members Invokes the constructor of the base class Implicitly or explicitly

Constructors under Inheritance Cont d Base of inheritance hierarchy Last constructor called in chain First constructor body to finish executing CommissionEmployee/BasePlusCommissionEmployee hierarchy CommissionEmployee constructor called last CommissionEmployee constructor body finishes execution first Initializing data members Each base-class constructor initializes its data members that are inherited by derived class

Constructors under Inheritance Cont d class BaseClass BaseClass() x = 1; } int x; class DerivedClass : public BaseClass DerivedClass() y = 2; } int y; class BaseClass BaseClass() x = 1; } BaseClass( int a ) x = a; } int x; class DerivedClass : public BaseClass DerivedClass() y = 2; } DerivedClass( int x ) : BaseClass( x ) y = 2; } int y;

Constructors under Inheritance Cont d class BaseClass // BaseClass() x = 1; } BaseClass( int a ) x = a; } int x; class DerivedClass : public BaseClass DerivedClass() y = 2; } // error int y; class BaseClass BaseClass() } cout << base ; cout << endl; class DerivedClass : public BaseClass DerivedClass() } cout << derived ; cout << endl; ============================ base derived

Destructors under Inheritance Destroying derived-class object Chain of destructor calls Reverse order of constructor chain Destructor of derived-class called first Destructor of next base class up hierarchy next Continue up hierarchy until final base reached After final base-class destructor, object removed from memory

Destructors under Inheritance Cont d class BaseClass BaseClass() x = new int[100]; } ~BaseClass() delete[] x; } int* x; class DerivedClass : public BaseClass DerivedClass() y = new int[10]; } ~DerivedClass() delete[] y; } int* y; class BaseClass BaseClass() } cout << base con ; cout << endl; ~BaseClass() cout << base des ; cout << endl; } class DerivedClass : public BaseClass DerivedClass() } cout << derived con ; cout << endl; ~DerivedClass() } cout << derived des ; cout << endl;

Base-class Member Accessibility in a Derived Class

Customizing and Reusing Existing Software Derived class can re-implement the behaviors of the base class Known as method overriding add new behaviors or data We can use not only new behaviors but also the existing behaviors Factor out common attributes and behaviors and place these in a base class Use inheritance to form derived classes, endowing them with capabilities beyond those inherited from the base class The creation of a derived class does not affect its base class s source code