CS250 Introduction to Computer Science II

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Lecture Note 9_Polymorphism Outline Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class object Virtual.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
C++ Lecture 7 Thursday, 21 July Chapter 9 Inheritance l Creating new classes from the existing classes l The notions of base classes and derived.
Object-Oriented Programming: Polymorphism Zhen Jiang West Chester University
Object-Oriented Programming Polymorphism Session 07 Mata kuliah: M0874 – Programming II Tahun: 2010.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
 2003 Prentice Hall, Inc. All rights reserved Virtual Functions Polymorphism –Same message, “print”, given to many objects All through a base.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Object-Oriented Programming: Polymorphism Chapter 10.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
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.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
1 More About Derived Classes and Inheritance Chapter 9.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
Inheritance ITI1121 Nour El Kadri.
Advanced Program Design with C++
Interfaces.
Polymorphism.
Chapter 5 Hierarchies IS-A associations superclasses subclasses
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
Learning Objectives Inheritance Virtual Function.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 20- Virtual Functions and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Inheritance: Polymorphism and Virtual Functions
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Inheritance and Polymorphism
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Chapter 20 - C++ Virtual Functions and Polymorphism
Object-Oriented Programming: Polymorphism
Inheritance: Polymorphism and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Virtual Functions Example
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Inheritance: Polymorphism and Virtual Functions
Lecture 6: Polymorphism
Computer Science II for Majors
Presentation transcript:

CS250 Introduction to Computer Science II Abstract Classes 4/8/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II So Far We have covered polymorphism What is it? And virtual functions What are those? Today we will learn about Abstract class Pure virtual functions 4/8/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Abstract Classes Consider a base class called GameObject that contains a draw function Avatar, Monster, and Castle are classes that are derived from GameObject, and each one has a unique draw function If some kind of array of GameObjects is maintained, a simple draw command can be sent to each object invoking the specific draw method for each object type This is where we are heading 4/8/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Abstract Classes An abstract class is a class where the programmer never intends to instantiate an object of the abstract class type These classes are typically base classes and are used in an inheritance hierarchy to build more generic derived classes Parts of the abstract class are not implemented in the base class; therefore, this logic must be implemented in the derived class 4/8/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Concrete Classes A concrete class is any class that can be instantiated An object of that class can be created Consider an abstract class called Shape2D with concrete classes Circle, Square, and Triangle derived from Shape2D Shape2D has a draw method that is not implemented while Circle, Square, and Triangle must have implemented draw methods 4/8/05 CS250 Introduction to Computer Science II

Pure Virtual Functions A class is made abstract by having one or more pure virtual functions associated with the class as follows: virtual void functionName () const = 0; Each derived class must provide its own draw function that overrides the draw function of the abstract class How is this different from virtual functions? 4/8/05 CS250 Introduction to Computer Science II

Pure Virtual Functions A virtual function Allows the derived class the ability to override the function and Must have an implementation A pure virtual function Requires the derived class to override the function Cannot have an implementation 4/8/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Example Let us add an abstract class to the point, circle, cylinder hierarchy The abstract class will contain two pure virtual functions print: to print the data for the shape getName: returns a string containing the name of the shape (I.e. point, circle, or cylinder) 4/8/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Example The abstract class will also contain two virtual functions: getArea: returns the area of the shape getVolume: returns the volume of the shape Why would these be defined as virtual functions and not pure virtual functions? 4/8/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Shape Header #ifndef SHAPE_H #define SHAPE_H #include <string> using std::string; class Shape { public: virtual double getArea() const; virtual double getVolume() const; virtual string getName() const = 0; virtual void print() const = 0; }; #endif 4/8/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Shape Definition #include <iostream> using std::cout; #include "shape.h" double Shape::getArea() const { return 0.0; } double Shape::getVolume() const 4/8/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Summary We covered virtual functions We covered: Pages 672 - 679 4/8/05 CS250 Introduction to Computer Science II