Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 10 1.

Slides:



Advertisements
Similar presentations
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
Starting Out with C++, 3 rd Edition 1 Chapter 15 – Inheritance, Polymorphism, and Virtual Functions.
Inheritance, Polymorphism, and Virtual Functions
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
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.
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.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6 1.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
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.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
CMSC 202 Lesson 18 Polymorphism 1. Warmup What errors are present in the following hierarchy? Assume GetCurrentTime() and RingBell() are defined elsewhere.
Chapter -6 Polymorphism
Overview of C++ Polymorphism
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.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC 202 Polymorphism.
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Polymorphism Lec
Andy Wang Object Oriented Programming in C++ COP 3330
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Polymorphism 1 CMSC 202.
Programming II Polymorphism A.AlOsaimi.
Polymorphism CMSC 202, Version 4/02.
Overview of C++ Polymorphism
Polymorphism 2 CMSC 202.
CMSC 202 Lesson 19 Polymorphism 2.
Andy Wang Object Oriented Programming in C++ COP 3330
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.
Presentation transcript:

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10 1

Base Class Pointers 2 Pointers to a base class object may be assigned the address of derived class objects The base class pointer will ignore any overridden functions in the derived class

Polymorphism 3 class Base { public: void Show() { cout << "This is from the Base class.\n"; } }; class Derived : public Base { public: void Show() { cout << "This is from the Derived class.\n"; } }; int main() { Base *Bptr; Derived Dobject; Bptr = &Dobject; Bptr->Show(); // which Show function executes? return 0; }

Polymorphism 4 class Base { public: virtual void Show() { cout << "This is from the Base class.\n"; } }; class Derived : public Base { public: virtual void Show() { cout << "This is from the Derived class.\n"; } }; int main() { Base *Bptr; Derived Dobject; Bptr = &Dobject; Bptr->Show(); // which Show function executes now? return 0; }

Overriding vs. Redefining 5 Redefining When a derived class member function has the same name/signature as a base-class member function Overriding When a class redefines a virtual function, it is said that the class overrides the virtual function In C++ Redefined member functions are statically bound Overridden member functions are dynamically bound

Virtual Destructors 6 Remember to always have virtual destructors in inheritance hierarchy Needed so a base-class pointer to derived-class object knows which destructor to call (or to start with)

Abstract Base Classes and Pure Virtual Functions 7 Abstract base class cannot be instantiated other classes are derived from it (and they can be instantiated) contains at least one pure virtual function Can still have pointers of abstract-base-class type Pure Virtual Function a member function that must be overridden in any derived class. member function prototype ends with = 0; in the base class virtual void talk() = 0; Have no body or definition in the base class syntax error to try and instantiate an object of an abstract base class

Student 8 A student must have a major of CS, CpE, or EE. Student CS Student CpE Student EE Student Can’t be a student without having a major. So student cannot be instantiated – it should be abstract

Back to the Farm 9 CAnimal CCat CCow Must be a particular type of animal. Animals don’t talk, but all sub-classes do talk

Back To the Farm 10 class CAnimal { public: virtual void talk ()=0; }; class CAnimal { public: virtual void talk ()=0; }; class CCow : public CAnimal { public: virtual void talk () { cout << "Moooooo." << endl; CAnimal::talk(); } }; class CCat : public CAnimal { public: virtual void talk () { cout << "Meowwww" << endl; } }; class CCow : public CAnimal { public: virtual void talk () { cout << "Moooooo." << endl; CAnimal::talk(); } }; class CCat : public CAnimal { public: virtual void talk () { cout << "Meowwww" << endl; } }; int main () { CAnimal* arr[3]; arr[0] = new CAnimal(); arr[1] = new CCow(); arr[2] = new CCat(); arr[0]->talk(); arr[1]->talk(); arr[2]->talk(); return 0; } int main () { CAnimal* arr[3]; arr[0] = new CAnimal(); arr[1] = new CCow(); arr[2] = new CCat(); arr[0]->talk(); arr[1]->talk(); arr[2]->talk(); return 0; } Syntax Error

Classes Derived from Derived Classes 11 A base class can also be derived from another class A C B

Multiple Inheritance 12 When one class inherits from more than one base class Class C Class B Class A

Multiple Inheritance 13 class Date { protected: int month, day, year; public: Date(int m, int d, int y) { month = m; day = d; year = y; } int getDay() {return day;} int getYear() {return year;} int getMonth() {return month;} };

Multiple Inheritance 14 class Time { protected: int hour, minute; public: Time(int h, int m) { hour = h; minute = m; } int getHour() {return hour;} int getMinute() {return minute;} };

Multiple Inheritance 15 class DateTime : public Date, public Time { protected: char DTString[20]; public: DateTime(int, int, int, int, int, int); void GetDateTime(char *Str) { strcpy(Str, DTString); } };

Multiple Inheritance 16 DateTime::DateTime(int Dy, int Mon, int Yr, int Hr, int Mt, int Sc) : Date(Dy, Mon, Yr), Time(Hr, Mt, Sc) { char Temp[10]; // Temporary work area for itoa() strcpy(DTString, itoa(GetMonth(), Temp, 10)); strcat(DTString, “/”); strcat(DTString, itoa(GetDay(), Temp, 10)); strcat(DTString, “/”); strcat(DTString, itoa(GetYear(), Temp, 10)); strcat(DTString, “ “); strcpy(DTString, itoa(GetHour(), Temp, 10)); strcat(DTString, “:”); strcat(DTString, itoa(GetMin(), Temp, 10)); strcat(DTString, “:”); strcat(DTString, itoa(GetSec(), Temp, 10)); }

Multiple Inheritance 17 #include #include “datetime.h” int main() { char Formatted[20]; DateTime PastDay(4, 2, 60, 5, 32, 27); PastDay.GetDateTime(Formatted); cout << Formatted << endl; return 0; }