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

Size: px
Start display at page:

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

Transcription

1 Ch 14. Inheritance May 14, 2014 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 { }; // Derived class class Derived : Base { }; Inheritance Class Diagram Base Class 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 -inta -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( ); }; 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 Project 14-4 As driver program that tests all the methods, use following procedures in your main() function. //******************** //Testing Application int main() { Vehicle v1("ford", 4, "James Carter") ; Vehicle v2; Vehicle v3(v1); v2 = v1; cout << " ncar v1 (constructed) Data: n"; cout << v1.getmfgrname() << endl; cout << v1.getnumbercyl() << endl; cout << v1.getowner() << endl; cout << " ncar v2 (assigned) Data: n"; cout << v2.getmfgrname() << endl; cout << v2.getnumbercyl() << endl; cout << v2.getowner() << endl; cout << " ncar v3 (copy constructed) Data: n"; cout << v3.getmfgrname() << endl; cout << v3.getnumbercyl() << endl; cout << v3.getowner() << endl; ch 14-48

49 14.1 Programming Project 14-4 (cont.) Truck t1(80.0, 20000, "Mac", 8, "John Q. Driver"); // 80 tons gross vehicle weight, 20,000 lbs tow capacity Truck t3(t1), t2; t2 = t1; cout << " ntruck T1 (constructed) data: n"; cout << t1.getmfgrname() << endl; cout << t1.getnumbercyl() << endl; cout << t1.getowner() << endl; cout << t1.getloadcapacity() << endl; cout << t1.gettowingcapacity() << endl; cout << " ntruck T2 (assigned) data: n"; cout << t2.getmfgrname() << endl; cout << t2.getnumbercyl() << endl; cout << t2.getowner() << endl; cout << t2.getloadcapacity() << endl; cout << t2.gettowingcapacity() << endl; cout << " ntruck T3 (copy constructed) data: n"; cout << t3.getmfgrname() << endl; cout << t3.getnumbercyl() << endl; cout << t3.getowner() << endl; cout << t3.getloadcapacity() << endl; cout << t3.gettowingcapacity() << endl; return 0; } // end testing application ch 14-49

50 14.2 C++ Programming with Inheritance of Point, Rectangle, Hexahedron class Shape contains protected position data members of double x, double y, and it initializes the data members x = 0.0, y = 0.0, in default constructor. class Rectangle inherits from class Shape, and contains protected data members of double width and double depth, and it initializes the data members to width = 1.0, depth = 1.0 at default constructor. class Hexahedron inherits from class Rectangle, and contains protected data member double height, and initializes the data member to height = 1.0 at default constructor. C++ class Rectangle and Hexahedron contain methods double area() const; and double volume() const; to calculate surface area (area) and volume, and also contain operator overloading for output operator << (the format of output may be designed appropriately). Write class and member functions for Shape, Rectangle and Hexahedron. The main() function must contain following procedure to use class Rectangle and class Hexahedron. The result must be output to file output.dat. main() { ofstream outf( output.dat, ios::out); Rectangle rect1(0, 0, 2, 3), rect2(1, 2, 3, 4); Hexahedron hx1(1, 2, 3, 4, 5), hx2(5, 6, 7, 8, 9); } outf << rect1 << Area of rectangle 1 is : << rect1.area() << endl; outf << rect2 << Area of rectangle 2 is : << rect2.area() << endl; outf << hx1 << Volume of hexahedron 1 is : << hx1.volume() << endl; outf << hx2 << Volume of hexahedron 2 is : << hx2.volume() << endl;.... ch 14-50

51 14.3 C++ programming with Inheritance for class point, square, cube class Shape contains protected data members double x and double y, and initializes the data members to x = 0.0, y = 0.0 at default constructor. class Square inherits from class Shape, contains protected data member double width, and initializes the data member to width = 1.0 at default constructor. class Cube inherits from class Square, contains protected data member double height, and initializes the data member to height = 1.0 at default constructor. Class Square and Cube contain member functions double area() const; and double volume() const; to calculate the surface area and volume, respectively. It also contains an output operator overloading for operator << (the format of output may be designed appropriately). Write class and member functions of Point, Square and Cube. The main() function must contain following procedure to use class Square and Cube, The result be output to file output.dat. main() { ofstream outf( output.dat, ios::out); Square sq1(0,0,2), sq2(1,2,3); Cube cb1(1,2,3,4), cb2(5,6,7,8); } outf << sq1 << Area of square 1 is : << sq1.area() << endl; outf << sq2 << Area of square 2 is : << sq2.area() << endl; outf << cb1 << Volume of cube 1 is : << cb1.volume() << endl; outf << cb2 << Volume of cube 2 is : << cb2.volume() << endl;.... ch 14-51

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

Ch 14. Inheritance. September 10, Prof. Young-Tak Kim 2013-2 Ch 14. Inheritance September 10, 2013 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 (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. 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. 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

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

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

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

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

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

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

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

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

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

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

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

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

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

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

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

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

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

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

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

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

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

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

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

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called 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. Class : BCA 3rd Semester Course Code: BCA-S3-03 Course Title: Object Oriented Programming Concepts in C++ Unit III Inheritance The mechanism that allows us to extend the definition of a class without making

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

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

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

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

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

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

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

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

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

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

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

More information

Computer Programming Inheritance 10 th Lecture

Computer Programming Inheritance 10 th Lecture 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

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

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

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

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

COEN244: Polymorphism

COEN244: Polymorphism COEN244: Polymorphism Aishy Amer Electrical & Computer Engineering Polymorphism means variable behavior / ability to appear in many forms Outline Casting between objects Using pointers to access objects

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

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

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

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

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

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

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

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

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

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

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

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

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

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

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

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

Friend Functions, Inheritance

Friend Functions, Inheritance Friend Functions, Inheritance Friend Function Private data member of a class can not be accessed by an object of another class Similarly protected data member function of a class can not be accessed by

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

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

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

Ch 6-1. Structures. March 30, Prof. Young-Tak Kim

Ch 6-1. Structures. March 30, Prof. Young-Tak Kim 2014-1 Ch 6-1. Structures March 30, 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

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

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Topics 7.1 Abstract Data Types 7.2 Object-Oriented Programming

More information

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Move semantics Classes Operator overloading Making your class copyable Making your class movable Rule of all or nothing Inheritance

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

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Polymorphism 1 / 19 Introduction to Object-Oriented Programming Today we ll learn how to combine all the elements of object-oriented programming in the design of a program that handles a company payroll.

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

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

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

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

Function Overloading

Function Overloading Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures

More information

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

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

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

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

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 17 Inheritance Overview Problem: Can we create bigger classes from smaller ones without having to repeat information? Subclasses: a class inherits

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

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

Lecture 14: more class, C++ streams

Lecture 14: more class, C++ streams CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 14:

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

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC Topic 7: Inheritance Reading: JBD Sections 7.1-7.6 1 A Quick Review of Objects and Classes! An object is an abstraction that models some thing or process! Examples of objects:! Students, Teachers, Classes,

More information

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University C++ Review CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Purpose of Review Review some basic C++ Familiarize us with

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

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

Programming in the large

Programming in the large Inheritance 1 / 28 Programming in the large Software is complex. Three ways we deal with complexity: Abstraction - boiling a concept down to its essential elements, ignoring irrelevant details Decomposition

More information

Object Oriented Programming CS250

Object Oriented Programming CS250 Object Oriented Programming CS250 Abas Computer Science Dept, Faculty of Computers & Informatics, Zagazig University arabas@zu.edu.eg http://www.arsaliem.faculty.zu.edu.eg Polymorphism Chapter 8 8.1 Static

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

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