Ch 14. Inheritance. September 10, Prof. Young-Tak Kim

Size: px
Start display at page:

Download "Ch 14. Inheritance. September 10, Prof. Young-Tak Kim"

Transcription

1 Ch 14. Inheritance September 10, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : ; Fax : ytkim@yu.ac.kr)

2 Outline Inheritance Basics Derived classes, with constructors protected: qualifier Redefining member functions Non-inherited functions Programming with Inheritance Assignment operators and copy constructors Destructors in derived classes Multiple inheritance ch 14-2

3 Introduction to Inheritance Object-oriented programming Powerful programming technique Provides abstraction dimension called inheritance General form of class is defined Specialized versions then inherit properties of general class And add to it/modify it s functionality for it s appropriate use ch 14-3

4 Inheritance Inheritance is a way of creating a new class by starting with an existing class and adding new members The new class can replace or extend the functionality of the existing class Inheritance models the is a relationship between classes; not has a relationship ch 14-4

5 Inheritance - Terminology The existing class is called the base class Alternates: parent class, superclass The new class is called the derived class Alternates: child class, subclass // Existing class class Base { }; Inheritance Class Diagram Base Class // Derived class class Derived : Base { }; Derived Class ch 14-5

6 Inheritance (1) a mechanism that lets the programmer create new objects based on old ones (which are proven in performance and reliability) the child object inherits the qualities of a parent object provides ease of maintenance: when some changes are required for all the similar objects that have been inherited from the same parent object, only the parent objects needs to change Diagram name 1 * Figure color centerposition penthickness pentype move() select() rotate() display() ZeroDimension OneDimension orientation scale() TwoDimension orientation filltype scale() fill() Point display() Line endpoints display() Arc radius startangle arcangle display() Spline controlpts display() Polygon numofsides vertices display() Circle diameter display() rotate() ch 14-6

7 Inheritance (2) the derived classes contains all attributes of the parent class the parent class is providing more generic attributes the derived (children) classes specify more specific features class student - name - student_id - department - gpa class under_student - internships - class: freshman, sophomore, junior, senior class grad_student - course: ms or Phd - advisor of thesis - publications ch 14-7

8 Inheritance Basics New class inherited from another class Base class "General" class from which others derive Derived class New class Automatically has base class s: Member variables Member functions Can then add additional member functions and variables ch 14-8

9 Derived Classes Consider example: Class of "Employees" Composed of: Salaried employees Hourly employees Each is "subset" of employees Another might be those paid fixed wage each month or week ch 14-9

10 Derived Classes Don t "need" type of generic "employee" Since no one s just an "employee" General concept of employee helpful! All have names All have social security numbers Associated functions for these "basics" are same among all employees So "general" class can contain all these "things" about employees ch 14-10

11 Inheritance of Members class Parent { int a; void bf(); }; class Child : Parent { int c; void df(); }; Objects of Parent have members int a; void bf(); Objects of Child have members int a; void bf(); int c; void df(); Child Parent - int a - void bf() -int c -void df() ch 14-11

12 Type Compatibility in Inheritance Hierarchies Classes in a program may be part of an inheritance hierarchy Classes lower in the hierarchy are special cases of those up higher Wild_Animal Animal Farm_Animal Lion Tiger Pig Sheep ch 14-12

13 Employee Class Many members of "employee" class apply to all types of employees Accessor functions: getssn(); Mutator functions: setssn(); Most data items: SSN //Social Security Number Name Pay We won t have "objects" of this class, however ch 14-13

14 Employee Class Consider printcheck() function: Will always be "redefined" in derived classes So different employee types can have different checks Makes no sense really for "undifferentiated employee So function printcheck() in Employee class says just that Error message stating "printcheck called for undifferentiated employee!! Aborting " ch 14-14

15 Deriving from Employee Class Derived classes from Employee class: Automatically have all member variables Automatically have all member functions Derived class said to "inherit" members from Base class Can then redefine existing members and/or add new members ch 14-15

16 Interface for the Derived Class HourlyEmployee (1) /** employee.h */ #include <string> using std::string; class Employee { public: Employee( ); Employee(string thename, string thessn); string getname( ); string getssn( ); double getnetpay( ); void setname(string newname); void setssn(string newssn); void setnetpay(double newnetpay); void printcheck( ); protected: string name; string ssn; double netpay; }; ch 14-16

17 Interface for the Derived Class HourlyEmployee (2) ch 14-17

18 Interface for the Derived Class HourlyEmployee (3) ch 14-18

19 HourlyEmployee Class Interface Note definition begins same as any other #ifndef structure Includes required libraries Also includes employee.h! And, the heading: class HourlyEmployee : public Employee { } Specifies "publicly inherited" from Employee class ch 14-19

20 HourlyEmployee Class Additions Derived class interface only lists new or "to be redefined" members Since all others inherited are already defined i.e.: "all" employees have ssn, name, etc. HourlyEmployee adds: Constructors wagerate, hours member variables setrate(), getrate(), sethours(), gethours() member functions ch 14-20

21 HourlyEmployee Class Redefinitions HourlyEmployee redefines: printcheck() member function This "overrides" the printcheck() function implementation from Employee class Its definition must be in HourlyEmployee class s implementation As do other member functions declared in HourlyEmployee s interface New and "to be redefined" ch 14-21

22 Inheritance Terminology Common to simulate family relationships Parent class Refers to base class Child class Refers to derived class Ancestor class Class that is a parent of a parent Descendant class Opposite of ancestor ch 14-22

23 Constructors in Derived Classes Base class constructors are NOT inherited in derived classes! But they can be invoked within derived class constructor Which is all we need! Base class constructor must initialize all base class member variables Those inherited by derived class So derived class constructor simply calls it "First" thing derived class constructor does ch 14-23

24 Derived Class Constructor Example Consider syntax for HourlyEmployee constructor: HourlyEmployee::HourlyEmployee(string thename, string thessn, double thewagerate, double thehours) : Employee(theName, thessn), wagerate(thewagerate), hours(thehours) { //Deliberately empty } Portion after : is "initialization section" Includes invocation of Employee constructor ch 14-24

25 Another HourlyEmployee Constructor A second constructor: HourlyEmployee::HourlyEmployee() : Employee(), wagerate(0), hours(0) { //Deliberately empty } Default version of base class constructor is called (no arguments) Should always invoke one of the base class s constructors ch 14-25

26 Constructor: No Base Class Call Derived class constructor should always invoke one of the base class s constructors If you do not: Default base class constructor automatically called Equivalent constructor definition: HourlyEmployee::HourlyEmployee() : wagerate(0), hours(0) {.... } ch 14-26

27 Pitfall: Base Class Private Data Derived class "inherits" private member variables But still cannot directly access them Not even through derived class member functions! Private member variables can ONLY be accessed "by name" in member functions of the class they re defined in ch 14-27

28 Pitfall: Base Class Private Member Functions Same holds for base class member functions Cannot be accessed outside interface and implementation of base class Not even in derived class member function definitions ch 14-28

29 Pitfall: Base Class Private Member Functions Impact Larger impact here vs. member variables Member variables can be accessed indirectly via accessor or mutator member functions Member functions simply not available This is "reasonable" Private member functions should be simply "helper" functions Should be used only in class they re defined ch 14-29

30 The protected: Qualifier New classification of class members Allows access "by name" in derived class But nowhere else Still no access "by name" in other classes In class it s defined acts like private Considered "protected" in derived class To allow future derivations Many feel this "violates" information hiding ch 14-30

31 Private vs. Protected Class Base { private: int x; public: int getx1( ); }; 2 1 Class Base { protected: int x; public: int getx1( ); }; Direct Access by Name! Class Derived : Base { private: int y; public: int gety() int getx2( ); } Class Derived : Base { private: int y; protected: int getx2( ); public: int gety() } ch 14-31

32 Inheritance Modes in public inheritance, the classifiers in base class are not changed in protected inheritance, the public classifier in base class is changed to protected classifier in derived class in private inheritance, the public and protected classifier in base class are changed to private classifier in derived class base class members classifier Public Inheritance Protected Inheritance Private Inheritance Public Public Protected Private Protected Protected Protected Private Private Private Private Private ch 14-32

33 Redefinition of Member Functions Recall interface of derived class: Contains declarations for new member functions Also contains declarations for inherited member functions to be changed Inherited member functions NOT declared: Automatically inherited unchanged Implementation of derived class will: Define new member functions Redefine inherited functions as declared ch 14-33

34 Redefining vs. Overloading Very different! Redefining in derived class: SAME parameter list Essentially "re-writes" same function Overloading: Different parameter list Defined "new" function that takes different parameters Overloaded functions must have different signatures ch 14-34

35 A Function s Signature Recall definition of a "signature": Function s name Sequence of types in parameter list Including order, number, types Signature does NOT include: Return type const keyword & (call-by-reference) ch 14-35

36 Accessing Redefined Base Function When redefined in derived class, base class s definition not "lost" Can specify its use: Employee JaneE; HourlyEmployee SallyH; JaneE.printCheck(); calls Employee s printcheck function SallyH.printCheck(); calls HourlyEmployee printcheck function SallyH.Employee::printCheck(); Calls Employee s printcheck function! Not typical here, but useful sometimes ch 14-36

37 Functions Not Inherited All "normal" functions in base class are inherited in derived class Exceptions: Constructors (we ve seen) Destructors Copy constructor But if not defined, generates "default" one Recall need to define one for pointers! Assignment operator ( = ) If not defined default ch 14-37

38 Assignment Operators and Copy Constructors Recall: overloaded assignment operators and copy constructors NOT inherited But can be used in derived class definitions Typically MUST be used! Similar to how derived class constructor invokes base class constructor ch 14-38

39 Assignment Operator Example Given "Derived" is derived from "Base": Derived& Derived::operator =(const Derived & rightside) { Base::operator =(rightside); } Notice code line Calls assignment operator from base class This takes care of all inherited member variables Would then set new variables from derived class ch 14-39

40 Copy Constructor Example Consider: Derived::Derived(const Derived& Object) : Base(Object), { } After : is invocation of base copy constructor Sets inherited member variables of derived class object being created Note Object is of type Derived; but it s also of type Base, so argument is valid ch 14-40

41 Destructors in Derived Classes If base class destructor functions correctly Easy to write derived class destructor When derived class destructor is invoked: Automatically calls base class destructor! So no need for explicit call So derived class destructors need only be concerned with derived class variables And any data they "point" to Base class destructor handles inherited data automatically ch 14-41

42 Destructor Calling Order Consider: class B derives from class A class C derives from class B A B C When object of class C goes out of scope: Class C destructor called 1 st Then class B destructor called Finally class A destructor is called Opposite of how constructors are called ch 14-42

43 "Is a" vs. "Has a" Relationships Inheritance Considered an "Is a" class relationship e.g., An HourlyEmployee "is a" Employee A Convertible "is a" Automobile A class contains objects of another class as it s member data Considered a "Has a" class relationship e.g., One class "has a" object of another class as its data Example: HourlyEmployee is an Employee HourlyEmployee has a Date member variable (birthdate) ch 14-43

44 Protected and Private Inheritance New inheritance "forms" Both are rarely used Protected inheritance: class SalariedEmployee : protected Employee { } Public members in base class become protected in derived class Private inheritance: class SalariedEmployee : private Employee { } All members in base class become private in derived class ch 14-44

45 Multiple Inheritance Derived class can have more than one base class! Syntax just includes all base classes separated by commas: class derivedmulti : public base1, base2 { } Possibilities for ambiguity are endless! Dangerous undertaking! Some believe should never be used Certainly should only be used be experienced programmers! ch 14-45

46 Summary 14-1 Inheritance provides code reuse Allows one class to "derive" from another, adding features Derived class objects inherit members of base class And may add members Private member variables in base class cannot be accessed "by name" in derived Private member functions are not inherited ch 14-46

47 Summary 14-2 Can redefine inherited member functions To perform differently in derived class Protected members in base class: Can be accessed "by name" in derived class member functions Overloaded assignment operator not inherited But can be invoked from derived class Constructors are not inherited Are invoked from derived class s constructor ch 14-47

48 Homework Programming projects (Class Vehicle, Truck, Person) 14.2 Programming projects (Class Pet, Dog, Rock) ch 14-48

49 14.3 Object-oriented programming with inheritance (1) class Figure private data members: int pos_x, int pos_y, int angle, and string name public member functions: Figure(int x, int y, int a, string name); // constructor virtual double getarea();// returns 0.0 virtual double getvolume(); // returns 0.0 virtual void draw(); // prints out Draw_Figure at pos(x, y), name, virtual void print(); // prints out attributes of Figure void setpos_x(int x); void setpos_y(int y); int getpos_x(); // implement as inline function int getpos_y(); // implement as inline function int getangle();// implement as inline function string getname();// implement as inline function (2) class Ellipse class Ellipse (public) inherits from class Figure private data members: int radius1, radius2; // two sides (radiuses) of ellipse public member functions: Ellipse(int x, int y, int a, string name, int r1, int r2); // constructor virtual double getarea(); // calculates the area of ellipse (ellipse_area = PI*r1*r2) double getcircumference(); // calculates the circumference of ellipse // approximation of ellipse_circum = PI*{5*(r1+r2)/4 (r1*r2)/(r1+r2)} virtual double getvolume(); // returns 0.0 virtual void draw(); // prints out Draw_Ellipse at pos(x, y), name, virtual void print(); // prints out attributes of Ellipse int getradius1(); int getradius2(); void setradius1(int r1); // implement as inline function void setradius2(int r2); // implement as inline function ch 14-49

50 14.3 Object-oriented programming with inheritance (cont.) (3) class Cylinder class Cylinder (public) inherits from class Ellipse. private data member: int height; // height of cylinder public member functions: Cylinder(int x, int y, int a, string name, int r1, int r2, int h); // constructor virtual double getarea(); // calculates the surface area of cylinder virtual double getvolume(); // calculate the volume of cylinder virtual void draw(); // prints out Draw_Cylinder at pos(x, y), name, virtual void print(); // prints out attributes of Cylinder int getheight(); void setheight(int h); // implement as inline function ch 14-50

51 14.3 Object-oriented programming with inheritance (cont.) (4) main() function #include <iostream>.... void main() { Figure *figarray[5]; Figure f1(1, 2, 0, "Fig1"); f1.print(); cout << endl; Ellipse e1(3, 3, 0, "Ellipse1", 5, 5), e2(4, 4, 0, "Ellipse2", 6, 6); e1.print(); cout << endl; Cylinder c1(6, 6, 0, "Cyl1", 7, 7, 9), c2(7, 7, 0, "Cyl2", 8, 8, 9); c1.print(); cout << endl; figarray[0] = &f1; figarray[1] = &e1; figarray[2] = &e2; figarray[3] = &c1; figarray[4] = &c2; cout <<"Draw figures using polymorphism:" << endl; for (int i=0; i<5; i++) { figarray[i]->draw(); } cout << endl; } // end of main() ch 14-51

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

Ch 14. Inheritance. May 14, Prof. Young-Tak Kim 2014-1 Ch 14. Inheritance May 14, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742

More information

Chapter 14. Inheritance. Slide 1

Chapter 14. Inheritance. Slide 1 Chapter 14 Inheritance Slide 1 Learning Objectives Inheritance Basics Derived classes, with constructors protected: qualifier Redefining member functions Non-inherited functions Programming with Inheritance

More information

Chapter 14 Inheritance. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 14 Inheritance. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo Chapter 14 Inheritance 1 Learning Objectives Inheritance Basics Derived classes, with constructors Protected: qualifier Redefining member functions Non-inherited functions Programming with Inheritance

More information

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

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy CISC181 Introduction to Computer Science Dr. McCoy Lecture 27 December 8, 2009 Object Oriented Programming Classes categorize entities that occur in applications. Class teacher captures commonalities of

More information

Inheritance. Overview. Chapter 15 & additional topics. Inheritance Introduction. Three different kinds of inheritance

Inheritance. Overview. Chapter 15 & additional topics. Inheritance Introduction. Three different kinds of inheritance Inheritance Chapter 15 & additional topics Overview Inheritance Introduction Three different kinds of inheritance Changing an inherited member function More Inheritance Details Polymorphism Motivating

More information

Inheritance (with C++)

Inheritance (with C++) Inheritance (with C++) Starting to cover Savitch Chap. 15 More OS topics in later weeks (memory concepts, libraries) Inheritance Basics A new class is inherited from an existing class Existing class is

More information

Inheritance Basics. Inheritance (with C++) Base class example: Employee. Inheritance begets hierarchies. Writing derived classes

Inheritance Basics. Inheritance (with C++) Base class example: Employee. Inheritance begets hierarchies. Writing derived classes Inheritance (with C++) Starting to cover Savitch Chap. 15 More OS topics in later weeks (memory concepts, libraries) Inheritance Basics A new class is inherited from an existing class Existing class is

More information

Inheritance. Chapter 15 & additional topics

Inheritance. Chapter 15 & additional topics Inheritance Chapter 15 & additional topics Overview Inheritance Introduction Three different kinds of inheritance Changing an inherited member function More Inheritance Details Polymorphism Inheritance

More information

Inheritance. Chapter 15 & additional topics

Inheritance. Chapter 15 & additional topics Inheritance Chapter 15 & additional topics Overview Inheritance Introduction Three different kinds of inheritance Changing an inherited member function More Inheritance Details Polymorphism Inheritance

More information

Chapter 7. Inheritance

Chapter 7. Inheritance Chapter 7 Inheritance Introduction to Inheritance Inheritance is one of the main techniques of objectoriented programming (OOP) Using this technique, a very general form of a class is first defined and

More information

Inheritance (Deitel chapter 9)

Inheritance (Deitel chapter 9) Inheritance (Deitel chapter 9) 1 2 Plan Introduction Superclasses and Subclasses protected Members Constructors and Finalizers in Subclasses Software Engineering with Inheritance 3 Introduction Inheritance

More information

Comp 249 Programming Methodology

Comp 249 Programming Methodology Comp 249 Programming Methodology Chapter 7 - Inheritance Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted,

More information

9/10/2018 Programming Data Structures Inheritance

9/10/2018 Programming Data Structures Inheritance 9/10/2018 Programming Data Structures Inheritance 1 Email me if the office door is closed 2 Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same

More information

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

CS105 C++ Lecture 7. More on Classes, Inheritance CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.

More information

Inheritance Motivation

Inheritance Motivation Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 8(a): Abstract Classes Lecture Contents 2 Abstract base classes Concrete classes Dr. Amal Khalifa, 2014 Abstract Classes and Methods

More information

Ch 8. Operator Overloading, Friends, and References

Ch 8. Operator Overloading, Friends, and References 2014-1 Ch 8. Operator Overloading, Friends, and References May 28, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Inheritance Assignment 5 Many types of classes that we create can have similarities. Useful to take advantage of the objectoriented programming technique known

More information

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1 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

More information

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017 Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017 Three Properties of Object-Oriented Languages: Encapsulation Inheritance Dynamic method binding (polymorphism)

More information

Object Oriented Programming

Object Oriented Programming OOP Object Oriented Programming Object 2 Object 1 Object 3 For : COP 3330. Object oriented Programming (Using C++) Object 4 http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Objects: State (fields),

More information

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

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance? INHERITANCE - Part 1 OOP Major Capabilities Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors encapsulation

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Polymorphism Lecture 8 September 28/29, 2004 Introduction 2 Polymorphism Program in the general Derived-class object can be treated as base-class object is -a

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

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

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 1. The following C++ program compiles without any problems. When run, it even prints out the hello called for in line (B) of main. But subsequently

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

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

CS 162, Lecture 25: Exam II Review. 30 May 2018 CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you

More information

Data Structures (INE2011)

Data Structures (INE2011) Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam ( hnam@hanyang.ac.kr ) Lecture 1 1 Data Structures Data? Songs in a smartphone Photos in a camera Files

More information

Inheritance -- Introduction

Inheritance -- Introduction Inheritance -- Introduction Another fundamental object-oriented technique is called inheritance, which, when used correctly, supports reuse and enhances software designs Chapter 8 focuses on: the concept

More information

Chapter 9 - Object-Oriented Programming: Inheritance

Chapter 9 - Object-Oriented Programming: Inheritance Chapter 9 - Object-Oriented Programming: Inheritance 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship between Superclasses and Subclasses 9.5 Case Study: Three-Level

More information

Chapter 9 - Object-Oriented Programming: Polymorphism

Chapter 9 - Object-Oriented Programming: Polymorphism Chapter 9 - Object-Oriented Programming: Polymorphism Polymorphism Program in the general Introduction Treat objects in same class hierarchy as if all superclass Abstract class Common functionality Makes

More information

Inheritance Examples

Inheritance Examples Example #1: Employee Class Hierarchy #ifndef EMPLOYEE_H_INCLUDED #define EMPLOYEE_H_INCLUDED #include Inheritance Examples class employee public: // constructor employee(const std::string& name,

More information

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I. y UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I Lab Module 7 CLASSES, INHERITANCE AND POLYMORPHISM Department of Media Interactive

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Objects and Classes (contd.) Course Lecture Slides 19 May 2010 Ganesh Viswanathan Objects and Classes Credits: Adapted from CIS3023 lecture

More information

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

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017 OOP components For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Data Abstraction Information Hiding, ADTs Encapsulation Type Extensibility Operator Overloading

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 12 continue 12.6 Case Study: Payroll System Using Polymorphism This section reexamines the CommissionEmployee- BasePlusCommissionEmployee hierarchy that we explored throughout

More information

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. All Rights Reserved. 1 Inheritance is a form of software reuse in which you create a class that absorbs an existing class s data and behaviors

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

Ch 7. Constructors and Other Tools

Ch 7. Constructors and Other Tools 2013-2 Ch 7. Constructors and Other Tools September 3, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure that you review all previous exams and make sure you fully understand

More information

Object Oriented Programming with C++ (24)

Object Oriented Programming with C++ (24) Object Oriented Programming with C++ (24) Zhang, Xinyu Department of Computer Science and Engineering, Ewha Womans University, Seoul, Korea zhangxy@ewha.ac.kr Polymorphism (II) Chapter 13 Outline Review

More information

CMSC 132: Object-Oriented Programming II. Inheritance

CMSC 132: Object-Oriented Programming II. Inheritance CMSC 132: Object-Oriented Programming II Inheritance 1 Mustang vs Model T Ford Mustang Ford Model T 2 Interior: Mustang vs Model T 3 Frame: Mustang vs Model T Mustang Model T 4 Compaq: old and new Price:

More information

Data Structures and Other Objects Using C++

Data Structures and Other Objects Using C++ Inheritance Chapter 14 discuss Derived classes, Inheritance, and Polymorphism Inheritance Basics Inheritance Details Data Structures and Other Objects Using C++ Polymorphism Virtual Functions Inheritance

More information

ENCAPSULATION AND POLYMORPHISM

ENCAPSULATION AND POLYMORPHISM MODULE 3 ENCAPSULATION AND POLYMORPHISM Objectives > After completing this lesson, you should be able to do the following: Use encapsulation in Java class design Model business problems using Java classes

More information

Lecture 5: Inheritance

Lecture 5: Inheritance McGill University Computer Science Department COMP 322 : Introduction to C++ Winter 2009 Lecture 5: Inheritance Sami Zhioua March 11 th, 2009 1 Inheritance Inheritance is a form of software reusability

More information

Chapter 19 - C++ Inheritance

Chapter 19 - C++ Inheritance Chapter 19 - C++ Inheritance 19.1 Introduction 19.2 Inheritance: Base Classes and Derived Classes 19.3 Protected Members 19.4 Casting Base-Class Pointers to Derived-Class Pointers 19.5 Using Member Functions

More information

Inheritance, Polymorphism, and Interfaces

Inheritance, Polymorphism, and Interfaces Inheritance, Polymorphism, and Interfaces Chapter 8 Inheritance Basics (ch.8 idea) Inheritance allows programmer to define a general superclass with certain properties (methods, fields/member variables)

More information

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: has a CSE143 Sp Student. CSE 143 Java Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches

More information

C++ Important Questions with Answers

C++ Important Questions with Answers 1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children

More information

Module 10 Inheritance, Virtual Functions, and Polymorphism

Module 10 Inheritance, Virtual Functions, and Polymorphism Module 10 Inheritance, Virtual Functions, and Polymorphism Table of Contents CRITICAL SKILL 10.1: Inheritance Fundamentals... 2 CRITICAL SKILL 10.2: Base Class Access Control... 7 CRITICAL SKILL 10.3:

More information

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++ Session 6 - Inheritance in C++ The most important slide of the lecture Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Why use inheritance?

More information

Ch 6. Structures and Classes

Ch 6. Structures and Classes 2013-2 Ch 6. Structures and Classes September 1, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Inheritance Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation

More information

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4 CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();

More information

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

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

More information

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

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck C++ Crash Kurs Polymorphism Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck http://www.itm.uni-luebeck.de/people/pfisterer C++ Polymorphism Major abstractions of C++ Data abstraction

More information

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

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor CSE 143 Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog

More information

CSE 143. "Class Relationships" Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization

CSE 143. Class Relationships Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization Class Relationships and Inheritance [Chapter 8, pp.343-354] "Class Relationships" is the title of Chapter 8 One class may include another as a member variable Date" used inside "Performance" Called a "has-a"

More information

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1 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

More information

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University Polymorphism Miri Ben-Nissan (Kopel) 1 Shape Triangle Rectangle Circle int main( ) Shape* p = GetShape( ); p->draw( ); Shape* GetShape( ) choose randomly which shape to send back For example: Shape* p

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand

More information

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

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

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

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

Programming in C# Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data

More information

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

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance CS257 Computer Science I Kevin Sahr, PhD Lecture 10: Inheritance 1 Object Oriented Features For a programming language to be called object oriented it should support the following features: 1. objects:

More information

C++ Programming: Inheritance

C++ Programming: Inheritance C++ Programming: Inheritance 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Basics on inheritance C++ syntax for inheritance protected members Constructors and destructors

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 4&5: Inheritance Lecture Contents What is Inheritance? Super-class & sub class The object class Using extends keyword @override keyword

More information

Ch 4. Parameters and Function Overloading

Ch 4. Parameters and Function Overloading 2014-1 Ch 4. Parameters and Function Overloading March 19, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

More information

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

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism. Outline Inheritance Class Extension Overriding Methods Inheritance and Constructors Polymorphism Abstract Classes Interfaces 1 OOP Principles Encapsulation Methods and data are combined in classes Not

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Abstract Data Types (ADT) and C++ Classes

Abstract Data Types (ADT) and C++ Classes Abstract Data Types (ADT) and C++ Classes 1-15-2013 Abstract Data Types (ADT) & UML C++ Class definition & implementation constructors, accessors & modifiers overloading operators friend functions HW#1

More information

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: has a CSC Employee. Supervisor CSC 143 Object & Class Relationships Inheritance Reading: Ch. 10, 11 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog

More information

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

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor Programming C++ Lecture 3 Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Inheritance S Software reuse inherit a class s data and behaviors and enhance with new capabilities.

More information

WEEK 13 EXAMPLES: POLYMORPHISM

WEEK 13 EXAMPLES: POLYMORPHISM WEEK 13 EXAMPLES: POLYMORPHISM CASE STUDY: PAYROLL SYSTEM USING POLYMORPHISM Use the principles of inheritance, abstract class, abstract method, and polymorphism to design a payroll project for a car lot.

More information

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August Polymorphism, Dynamic Binding and Interface 2 4 pm Thursday 7/31/2008 @JD2211 1 Announcement Next week is off The class will continue on Tuesday, 12 th August 2 Agenda Review Inheritance Abstract Array

More information

CS11 Introduction to C++ Fall Lecture 7

CS11 Introduction to C++ Fall Lecture 7 CS11 Introduction to C++ Fall 2012-2013 Lecture 7 Computer Strategy Game n Want to write a turn-based strategy game for the computer n Need different kinds of units for the game Different capabilities,

More information

Introduction to Programming

Introduction to Programming Introduction to Programming SS 2010 Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-3966, Office: H-B 8406 Stand: June 21, 2010 Betriebssysteme / verteilte Systeme Introduction

More information

Making New instances of Classes

Making New instances of Classes Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance? CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses

More information

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

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes

More information

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

More C++ : Vectors, Classes, Inheritance, Templates Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit

More information

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN CSSE 220 Day 15 Inheritance Check out DiscountSubclasses from SVN Discount Subclasses Work in pairs First look at my solution and understand how it works Then draw a UML diagram of it DiscountSubclasses

More information

Design Project - Object-oriented Programming for Tetris Game

Design Project - Object-oriented Programming for Tetris Game 2014-1 Design Project - Object-oriented Programming for Tetris Game June 1, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, College of Engineering, Yeungnam University,

More information

Lecture #1. Introduction to Classes and Objects

Lecture #1. Introduction to Classes and Objects Lecture #1 Introduction to Classes and Objects Topics 1. Abstract Data Types 2. Object-Oriented Programming 3. Introduction to Classes 4. Introduction to Objects 5. Defining Member Functions 6. Constructors

More information

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

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III Distributed Real-Time Control Systems Lecture 14 Intro to C++ Part III 1 Class Hierarchies The human brain is very efficient in finding common properties to different entities and classify them according

More information

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 11: Inheritance and Polymorphism Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad

More information

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Chapter 15: Inheritance, Polymorphism, and Virtual Functions Chapter 15: Inheritance, Polymorphism, and Virtual Functions 15.1 What Is Inheritance? What Is Inheritance? Provides a way to create a new class from an existing class The new class is a specialized version

More information

index.pdf January 21,

index.pdf January 21, index.pdf January 21, 2013 1 ITI 1121. Introduction to Computing II Circle Let s complete the implementation of the class Circle. Marcel Turcotte School of Electrical Engineering and Computer Science Version

More information

VALLIAMMAI ENGINEERING COLLEGE

VALLIAMMAI ENGINEERING COLLEGE VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur 603 203 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK B.E. - Electrical and Electronics Engineering IV SEMESTER CS6456 - OBJECT ORIENTED

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses

More information