CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Slides:



Advertisements
Similar presentations
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
Object Oriented Programming Inheritance and Polymorphism Dr. Mike Spann
Object-Oriented Programming in C++ Lecture 6 Inheritance.
More about classes and objects Classes in Visual Basic.NET.
1 CSE 303 Lecture 23 Inheritance in C++ slides created by Marty Stepp
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Programming Languages and Paradigms Object-Oriented Programming.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
C++ C++ Overview (I) What is Object Orientated Programming? Approach: Break problem into subgroups of related parts that take into account code and data;
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Object-Oriented Programming in C++
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
Inheritance One of the most powerful features of C++
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
CSC241 Object-Oriented Programming (OOP) Lecture No. 16.
LECTURE LECTURE 15 Inheritance Text book p
Chapter 10 Inheritance and Polymorphism
11 Introduction to Object Oriented Programming (Continued) Cats.
Object-Oriented Programming in C++ More examples of Association.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Chapter 4 Introduction to Classes, Objects, Methods and strings
John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Introduction to Object Oriented Programming Chapter 10.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Week 5 Extending classes in Java. Extending classes u Using an existing classes as the basis for a new one u Defining only the differences between the.
A First Book of C++ Chapter 12 Extending Your Classes.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
1 More About Derived Classes and Inheritance Chapter 9.
By Muhammad Waris Zargar
Polymorphism &Virtual Functions
Polymorphism.
Chapter 5 Classes.
Computing with C# and the .NET Framework
Presented By: Nazia Hossain Lecturer, Stamford University
group work #hifiTeam
Object-Oriented Programming (OOP) Lecture No. 28
Inheritance: Polymorphism and Virtual Functions
CS2011 Introduction to Programming I Objects and Classes
Inheritance: Polymorphism and Virtual Functions
Introduction to Classes and Objects
By Rajanikanth B OOP Concepts By Rajanikanth B
C++ Programming CLASS This pointer Static Class Friend Class
Inheritance: Polymorphism and Virtual Functions
Lecture 6: Polymorphism
Presentation transcript:

CE Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism

Introduction Basic inheritance Polymorphism Constructors and destructors Extending inheritance

Bank accounts “Normal” accountAccount  Already considered  Owner details, balance, methods etc. TransferCheque  Technique to transfer money to other account  Charge for handling Savings Savings  Deposit, withdrawal & interest InvestmentsInvestment  Only interest can be withdrawn

Accounts relationships Account Cheque Savings Investment

Declarations - abridged Within header file class Account { protected: double dBalance; Public: Account(double dBal = 0) { dBalance = dBal; } double GetBalance(void) { return dBalance; } };

Default values - revisited Use can be esoteric  Especially when removing implementation from class definition See DefaultValues.cpp & DefaultValues.h

Protected New keyword Allows derived classes / objects to access.  “Family” Not available to “outsiders” Private members  Only within  Or by friends

Savings Account class Savings:public Account { protected: double dRate; public: Savings(double = 0.0, double = 0.5); double Compound(void); double Withdraw(double); };

Savings Account See Savings.cpp & Savings.h Acc2 is Savings  Inherited from base Account Attributes Methods  Adds new as required for speciality AttributesInterest Rate – dRate MethodsOwn constructor(s) Own method(s) – Compound()

More Inheritance - Investment class Investment:public Savings { protected: double dFundsAvailable; public: Investment(double = 0.0, double = 1.5, int = -99); double Compound();// redefinition double Withdraw(double);// redefinition double GetAvailable(); };

Investment Account Inherited from Savings  NOT directly from Account See Investment.cpp & Investment.h Note  Investment Withdraw method Complete redefinition  Investment Partial, adds to Savings

Polymorphism Meaning  The ability to appear in many forms Programming  Objects react differently to same “instruction” Depending upon data types or class Practically  Ability to redefine methods for derived classes

Polymorphism Shape CircleSquareTriangle

Polymorphism Shape – base type  Anchor position, “name” field, colour etc. Circle – inherits from Shape  Radius, method to draw circle. Square – inherits from Shape  Height, width, methods to draw square Triangle – inherits from Shape  Height, base width, methods to draw triagle

Polymorphism Let the object look after itself  Carry what it needs to know  Remove functionality to the object  Do not carry items relating to others

Shape base class class Shape { private: // Attributes int nXPosition; int nYposition; char strName[50]; public: // Methods Shape(int nX, int nY, char *ptrName) { nXPosition = nX; nYposition = nY; strcpy(strName, ptrName); } void Draw(void) { cout << "I am a " << strName << ", I am at " << nXPosition << " and " << nYposition << endl; } };

Square class class Square:public Shape { private: // Attributes for Square int nLength; int nWidth; public: Square(int nX, int nY, int nL, int nW, char *ptrName):Shape(nX,nY,ptrName) { nLength = nL; nWidth = nW; cout << "Square object created" << endl; } void Draw(void) { int nArea; nArea = nLength * nWidth; Shape::Draw(); cout << "My area is " << nArea << " square units" << endl; } };

Circle class class Circle:public Shape { private: // Attributes for Circle int nRadius; public: Circle(int nX, int nY, int nR, char *ptrName):Shape(nX, nY, ptrName) { nRadius = nR; cout << "Circle object created" << endl; } ~Circle(void) { cout << "Circle object being deleted (Circle destructor)" << endl; } void Draw(void) { float nArea; nArea = (float)(PI * nRadius * nRadius); // Note casting to avoid warning Shape::Draw(); cout << "My area is " << nArea << " square units" << endl; } };

Main // ShapesMain.cpp #include #define PI using namespace std; #include "ShapesClasses.h" int main(void) { ShapeMyBase(10, 20, "Base Shape"); SquareMySquare(20, 50, 162, 75, "Square"); CircleMyCircle(70, 26, 24, "Circle"); cout << endl << " " << endl; MyBase.Draw(); cout << endl << " " << endl; MySquare.Draw(); cout << endl << " " << endl; MyCircle.Draw(); cout << endl << " " << endl; return 0; }

Running the code Note output from constructors

Constructor Order Starts with least specialised Works sequentially towards most specialised. ShapeMyBase(10, 20, "Base Shape"); SquareMySquare(20, 50, 162, 75, "Square"); CircleMyCircle(70, 26, 24, "Circle");

Destructors ~Shape(void) { cout << strName << " being deleted (Shape destructor)" << endl; } ~Square(void) { cout << "Square object being deleted (Square destructor)" << endl; } ~Circle(void) { cout << "Circle object being deleted (Circle destructor)" << endl; }

Destructor Order Starts with most specialised Works sequentially towards least specialised. Opposite of constructors

Destructor call Review  As before – when “delete” used Not included within Main – see slide 19 Only difference within class  Addition of Destructors When application terminates  All associated objects deleted  Destructors call automatically

Example code See ShapesMain.cpp & ShapesClasses.h  Within ShapeTypes.doc

Tutorial Work Develop Cheque using inheritance  As per diagram within slide 4  Able to transfer money from one account to another Extend Shapes to include Triangle Extend Shapes with Triangle  Different types of triangle  See next slide

Triangles Shape CircleSquareTriangle IsoscelesEquilateralScalene

Summary Basic inheritance Polymorphism Constructors and inheritance Destructors and inheritance Extending the inheritance