OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Pointer Data Type and Pointer Variables
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Programming in Java CSCI-2220 Object Oriented Programming.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Inheritance and Access Control CS 162 (Summer 2009)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
ISBN Object-Oriented Programming Chapter Chapter
Overview of C++ Polymorphism
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7. Inheritance and Polymorphism
Inheritance What is inheritance?
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Table of Contents Class Objects.
Week 4 Object-Oriented Programming (1): Inheritance
Types of Programming Languages
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance and Polymorphism:
Advanced Programming Behnam Hatami Fall 2017.
More Object-Oriented Programming
Polymorphism Polymorphism
Fundaments of Game Design
Object-Oriented Programming
Overview of C++ Polymorphism
Java Programming Language
Lecture 10 Concepts of Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Presentation transcript:

OOP, Inheritance and Polymorphism Lecture 6

Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation ‘comprises’, ‘consists’, ‘ is made of’ etc. e.g. Our gun has a chamber, Our player has an inventory, Our map consists of locations  Association is related to by some action e.g. musician plays the piano, cowboy fires a gun

Composition  Clock comprises of Case Clock face Minute hand Hour hand Works  It is not a clock without these objects!

Inheritance  Inheritance allows code reuse by enabling the creation of sub-classes based on base classes  Syntax of inheritance: class subclass: access_level baseclass1,access_level baseclass2{};  Class Dog : public Mammal, public Canine  Avoid multiple inheritance

Inheritance  When a sub class is instantiated the base class constructor is called by default  The access level describes the maximum access level that members of the sub class will have  The default access level is private for classes and public for structs

Sub classes  Do not inherit Constructors Destructors Copy constructors Friends

Protected  Private members are only accessible in the base class  Protected members are accessible in the child class  Non-static protected variables are accessible to: Friends Methods of a child class

Base class  Virtual functions Anything that is allowed to be overridden Destructor Tells the compiler to check the virtual function table  Pure virtual function

Derived class  Virtual functions As someone might want to use your class  Initializer lists  MyClass : ParentClass(), x(a), y(b)

Advantages  We can store an array of Parent class objects  We don’t have to rewrite code

Access levels  Public Inherit all parent’s public attributes and methods that are public AS public  Private Inherit all parent’s public attributes and methods that are public AS private  Protected Inherit all parent’s public attributes and methods that are public AS protected

Pure virtual methods  virtual void show () = 0;  No implementation of function in the.cpp  Allows us to create an interface for a class  The interface can not be instantiated

 Aircraft is an interface for types of Aircraft

 We still use parent class pointers even if the class is abstract i.e. Aircraft* plane = NULL;  These statements are permitted plane = new Fighter(); plane = new Bomber();  Compiler error plane = new Aircraft();

Polymorphism  A base class pointer can reference a derived class  However it can only access members the derived class inherits  Bar is a Foo  Foo f has been instantiated (Bar*) f; dynamic_cast (f); f->NextLine(); //Calls Bar’s function!

Let’s dive into some code  Time to see some epic typos!

Inheritance architecture  Clock is a base object  Clock has a Hand class  Is Hand also a base object?

Plane is a BaseObject

The issue with inheritance

Component architecture

BaseObject* clock = new BaseObject(); clock->addComponent(BigHand); clock->addComponent(SmallHand); clock->addComponent(Face); clock->addComponent(Engine);

Component abstract class