CMSC 202 Lesson 17 Inheritance II.

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
Inheritance, Polymorphism, and Virtual Functions
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Inheritance using Java
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
CMSC 202 Lesson 16 Inheritance. Warmup Identify which constructor each of the following use (default, non-default, copy) MyClass a; MyClass b(a); MyClass.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Inheritance One of the most powerful features of C++
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
CMSC 202 Lesson 18 Polymorphism 1. Warmup What errors are present in the following hierarchy? Assume GetCurrentTime() and RingBell() are defined elsewhere.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
Overview of C++ Polymorphism
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 2 Objects and Classes
Yan Shi CS/SE 2630 Lecture Notes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Lecture 12 Inheritance.
Inheritance CMSC 202, Version 4/02.
Operator Overloading CMSC 202.
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Inheritance & Polymorphism
Inheritance II CMSC 202.
Lesson 14 Copy and Assignment
Week 8 Lecture -3 Inheritance and Polymorphism
Chapter 2 Objects and Classes
group work #hifiTeam
Inheritance I CMSC 202.
CMSC 202 Lesson 16 Inheritance.
Module 4: Implementing Object-Oriented Programming Techniques in C#
Learning Objectives Inheritance Virtual Function.
Inheritance, Polymorphism, and Virtual Functions
Polymorphism 1 CMSC 202.
Polymorphism CMSC 202, Version 4/02.
Object-Oriented Programming (OOP) Lecture No. 22
Overview of C++ Polymorphism
Polymorphism 2 CMSC 202.
CMSC 202 Lesson 6 Functions II.
Lesson 25 Miscellaneous Topics
CMSC 202 Lesson 19 Polymorphism 2.
Chapter 11 Classes.
Inheritance in C++ Inheritance Protected Section
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

CMSC 202 Lesson 17 Inheritance II

Warmup Define a class called Giraffe that inherits publicly from a class called Mammal

Inheritance Review Base class Derived class class BaseClass { }; More general class Derived class More specific class Uses, adds, extends, or replaces base-class functionality class BaseClass { }; class DerivedClass : public BaseClass

Inherited Functionality Derived class Has access to all public methods of base class “Owns” these public methods Can be used on derived class objects! BaseClass b; b.BaseClassMethod(); b.DerivedClassMethod(); DerivedClass d; d.BaseClassMethod(); d.DerivedClassMethod();

Protection Mechanism Public Private Protected Anything can access these methods/data Private Only this class can access these methods/data Protected Only derived classes (and this class) can access these methods/data

Trip to the Zoo Animal Lion Hi, my name is Fred class Animal { public: void Print() { cout << “Hi, my name is” << m_name; } protected: string m_name; }; class Lion : public Animal Lion(string name) { m_name = name; } void main() Lion lion(“Fred”); lion.Print(); } Hi, my name is Fred

Constructors and Destructors Not inherited Base class constructor is called before Derived class constructor Use initializer-list to call non-default base-class constructor Similar for copy constructor Destructors Derived class destructor is called before Base class We’ll look more carefully at these next week

Constructor and Destructor class Animal { public: Animal() { cout << “Base constructor” << endl; } ~Animal() { cout << “Base destructor” << endl; } }; class Lion : public Animal Lion() { cout << “Derived constructor” << endl; } ~Lion() { cout << “Derived destructor” << endl; } int main() Lion lion; return 0; } Will print: Base constructor Derived constructor Derived destructor Base destructor

Non-default Constructor class Animal { public: Animal(string name) { m_name = name; } protected: string m_name; }; class Lion : public Animal Lion(string name) : Animal(name) { } What’s going on here?

operator= operator= Not inherited Need to override this! Can do: Well, at least not exactly Need to override this! Can do: Base base1 = base2; Base base1 = derived1; Cannot do: Derived derived1 = base1; Why won’t this work??

Operator= int main() { Lion lion(“Fred”); Animal animal1(“John”); class Animal { public: Animal(string name) { m_name = name; } Animal& operator=(Animal& a) { m_name = a.m_name; } protected: string m_name; }; class Lion : public Animal Lion(string name) : Animal(name) { } int main() { Lion lion(“Fred”); Animal animal1(“John”); Animal animal2(“Sue”); animal1 = animal2; animal2 = lion; lion = animal1; // Uh Oh!!! return 0; } Compiler looks for an operator= that takes a Lion on the left-hand side – doesn’t find one!!!

Method Overriding Overriding Use exact same signature Derived class method can Modify, add to, or replace base class method Derived method will be called for derived objects Base method will be called for base objects Pointers are special cases More on this next week!

Method Overriding I eat meat I eat stuff class Animal { public: void Eat() { cout << “I eat stuff” << endl; } }; class Lion : public Animal void Eat() { cout << “I eat meat” << endl; } void main() Lion lion; lion.Eat(); Animal animal; animal.Eat(); } I eat meat I eat stuff

Method Overloading Overloading Use different signatures Derived class has access to both… Not usually thought of as an inheritance topic Pointers are tricky More on this next week!

Method Overloading I ate a(n) steak I eat stuff class Animal { public: void Eat() { cout << “I eat stuff” << endl; } }; class Lion : public Animal void Eat(string food) { cout << “I ate a(n) ” << food << endl; } void main() Lion lion; lion.Eat(“steak”); lion.Eat(); } I ate a(n) steak I eat stuff

Challenge Complete the Giraffe and Mammal classes Implement at least one overloaded method Implement at least one protected data member Implement a constructor Implement a destructor Implement a non-default constructor