Today’s Objectives 5-Jul-2006 Announcements

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 13: Object-Oriented Programming
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 12: Adding Functionality to Your Classes.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Chapter 8 More Object Concepts
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Today’s Objectives  Announcements Homework #2 is due next Monday, 26-Jun, at the beginning of class Midterm Exam is Monday, 26-Jun – over material in.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Peyman Dodangeh Sharif University of Technology Fall 2014.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Coming up: Inheritance
1 Today’s Objectives  Announcements Homework #5 is due today. Since this date is so close to the end of the semester, no late assignments will be accepted.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
7. Inheritance and Polymorphism
03/10/14 Inheritance-2.
COMP 2710 Software Construction Inheritance
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
group work #hifiTeam
Object Oriented Analysis and Design
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Learning Objectives Inheritance Virtual Function.
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
By Muhammad Waris Zargar
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Overriding Methods & Class Hierarchies
Chapter 11: Inheritance and Composition
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Object-Oriented Programming
CIS 199 Final Review.
Chapter 8 Inheritance Part 2.
Final and Abstract Classes
Today’s Objectives 28-Jun-2006 Announcements
Presentation transcript:

Today’s Objectives 5-Jul-2006 Announcements Turn in HW #3 early for 10 bonus points on this HW Homework #4 is posted, it is due on July 17 Quiz #3 will be on Monday, July 10 – Class string, string stream processing, operator overloading, and inheritance Object-Oriented Programming: Inheritance (Ch. 12) Reusability Base classes and derived classes “is-a” relationship Using protected Constructors and destructors Other types of inheritance Using typedef Review of Homework #4 Library Demo (Continued)

Object-Oriented Programming: Inheritance Chapter 12

Three Principle Features of Object-Oriented Programming Object-Oriented Programming: Inheritance Three Principle Features of Object-Oriented Programming Encapsulation Inheritance Polymorphism

Reusability An important design goal of software engineering Object-Oriented Programming: Inheritance (Deitel, 634–680, Goodrich) Reusability An important design goal of software engineering Advantages Saves time Better to use code that is already tested

Object-Oriented Programming: Inheritance (Deitel, 634–680) Allows reuse of code that is common between related classes Create a base class that has the common code (also called a “parent” class) Create a derived class that inherits from the base class (also called a “child” class)

UML Notation Base class Inheritance relationship Derived classes Object-Oriented Programming: Inheritance (Deitel, 634–680) UML Notation Base class Inheritance relationship Derived classes

“is-a” Relationship Represented by inheritance Object-Oriented Programming: Inheritance (Deitel, 634–680) “is-a” Relationship Represented by inheritance We say that an object of the derived class “is-a” object of the base class Example: “A Student is-a Person” is always true However, an object of the base class is not an object of the derived class Example – We cannot say, “A Person is-a Student,” because that is not always true

Examples Base class Derived class Person Student Account Object-Oriented Programming: Inheritance Examples Base class General Code used by all derived classes Derived class Specific Specialized code Person Student Account SavingsAccount GamePlayer Defender Shape Rectangle RentalItem Movie

Object-Oriented Programming: Inheritance (Deitel, 634–680) Public Inheritance One of three types of inheritance – the others are “private” and “protected” Creates the “is-a” relationship Most commonly used type of inheritance class Student : public Person { //... }; Derived class Base class

Objects of the Derived Class Object-Oriented Programming: Inheritance Objects of the Derived Class The derived class object automatically has all the data members and member functions that are in the base class //Derived class objects can use base class functions Student alice("Alice"); cout << alice.getName(); A derived-class object can be assigned to a base-class object Student bob("Bob"); Person personBob = bob; A derived-class object can be used as an argument when the function parameter is declared as a base-class object void notify( Person& p ){ /*...*/ } int main(){ notify(alice); } Instantiate an object of the derived class Use a base class member function Assigning an object of the derived class to a base class object A function with a base class parameter It can be called with a derived class object

Protected Members “protected” is a member-access specifier Object-Oriented Programming: Inheritance (Deitel, 634–680) Protected Members “protected” is a member-access specifier Protected members can be accessed by members and friends of both the base class and the derived classes class Person{ protected: string name;//Accessible in derived classes public: //... };

Single and Multiple Inheritance Object-Oriented Programming: Inheritance (Deitel, 634, 1213) Single and Multiple Inheritance Single inheritance When the derived class inherits from only one base class Multiple inheritance When the derived class inherits from more than one base class Derived class has characteristics of both bases Example: class TA : public Student, Faculty{ //... };

Derived Class Initializer Object-Oriented Programming: Inheritance (Deitel, 634–680) Derived Class Initializer Should explicitly call the base class constructor Pass arguments to the base class constructor when appropriate Example: class Student : public Person{ public: Student(string nm=""):Person(nm){} //... }; Initializer list calls the constructor of the base class, and passes the argument to it Constructor of the derived class

Overriding a Base Class Function Object-Oriented Programming: Inheritance (Deitel, 634–680) Overriding a Base Class Function A derived class can redefine a function that it inherited from the base class Example: class Game : public RentalItem { private: string platform; public: string toString(){ return title + "Platform: " + platform; } //... }; Redefinition of a member function of the base class

Calling a Base Class Function in the Derived Class Object-Oriented Programming: Inheritance (Deitel, 634–680) Calling a Base Class Function in the Derived Class A base class function can be explicitly called from the derived class Use the binary scope operator class Game : public RentalItem { private: string platform; public: string toString(){ return RentalItem::toString()+"Platform: "+platform; } //... }; Explicit call to a member function of the base class

Constructors and Destructors Object-Oriented Programming: Inheritance (Deitel, 634–680) Constructors and Destructors Sequence of calls when an object of a derived class is instantiated Base class constructor Derived class constructor Sequence of calls when an object of a derived class is destroyed Derived class destructor Base class destructor

Using typedef

Typedef Associates a name with a type Using typedef (Deitel, 773) Typedef Associates a name with a type Can be used to create a shorter or more meaningful name Use the name like an alias typedef Book* BookPtr; BookPtr book1; //same as Book* book1 The alias

Library Demo (Continued)

Library Demo Object-Oriented Programming: Inheritance Since one of the requirements states that the library shall have both books and journals, we can create a base class called LibraryItem and two derived classes, Book and Journal class Book : public LibraryItem { //... };

Library Demo Object-Oriented Programming: Inheritance A derived class can redefine a function that it inherited from the base class class Journal : public LibraryItem { public: string toString(){ return LibraryItem::toString() + ", " + year; } //... Base class function Redefines the base class function

Library Demo Object-Oriented Programming: Inheritance When a derived class object is assigned to a base class object, any members in the derived class that are not in the base class are lost Journal journal("Dr. Dobbs","2004"); cout << journal.toString(); //prints "Dr. Dobbs, 2004" LibraryItem item = journal; cout << item.toString(); //prints "Dr. Dobbs" prints "Dr. Dobbs" prints "Dr. Dobbs, 2004"

References Deitel, H. M., and P. J. Deitel, C++ How to Program, Fourth Edition. Upper Saddle River, NJ: Prentice Hall, 2003. Goodrich, M. T., R. Tamassia, and D. Mount, Data Structures and Algorithms in C++. Hoboken, NJ: John Wiley & Sons, Inc., 2004.