Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Object Oriented Programming with Java
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance and Polymorphism.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
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,
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance and Polymorphism
One class is an extension of another.
CS212: Object Oriented Analysis and Design
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
C++ Classes C++ Interlude 1.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Java Programming, Second Edition
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Chapter 8: Class Relationships
Inheritance and Polymorphism
CIS 199 Final Review.
Presentation transcript:

Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B

Overview Assignment Operator Inheritance – Descendants and Ancestors – Instance variables and methods – Protected – Constructors – Calling ancestor functions – Polymorphism

Overloading Assignment Operator Used when assigning an object to another object: – Tuple a_tuple, b_tuple(5); – a_tuple = b_tuple; void operator =(const Tuple &other); Tuple& operator =(const Tuple &other);

Inheritance Classes can extend existing classes Member variables and member functions are inherited by the extending class An inheriting class is called a child class, derived class, or subclass The inherited calss is called a parent class, base class, or superclass.

How to inherit class NamedTuple: public Tuple { … };

Ancestors and Descendants Any class that inherits directly or indirectly from some class is said to be a descendant of that class – C inherits B which inherits A Both C and B are descendants of A Any class which has descendants, is said to be an ancestor of those classes – C inherits B which inherits A A is an ancestor to both B and C

Constructors are not inherited Constructors need to be redefined (you probably want to anyway as you’ll likely add instance variables to a new class) Exception: If no constructors are defined in the child-class, then it will inherit the default constructor

Instance Methods and Variables Child classes inherit both instance methods and variables Private instance methods are unusable as they cannot be invoked Private instance variables still store data, but they can only be used via accessors and mutators

Protected Keyword Declaring variables as protected in the parent class, allows descendant classes to access them directly

Redefining Instance Methods Instance methods can be redefined in child classes The method just needs to be declared and defined in the context of the child class Note: Function names only have to be declared if new or being redefined

Destructor The parent class destructors are automatically invoked after the child class destructors

virtual keyword The virtual keyword allows for dynamic dispatch Dynamic dispatch means to lookup the appropriate function to call at run-time Useful when working only with ancestor class types, but you want to call the specific descendant class function

More on Virtual The virtual property is inherited, thus it isn’t required to have virtual in the function declaration when overriding a method

Polymorphism The use of the virtual keyword is polymorphism The behavior of an instance changes depending on its actual implementation