CSC241 Object-Oriented Programming (OOP) Lecture No. 18

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

1 CSC241: Object Oriented Programming Lecture No 21.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Inheritance Juan Marquez 12_inheritance.ppt
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
Polymorphism and Virtual Functions. class Ball : public Sphere.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
C++ facts From What all is inherited from parent class in C++? Following are the things which a derived class inherits from its.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 1 #include // for file streams for input/output #include.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
CSCE 2013L: Lab 1 Overview  Java Basics The JVM Anatomy of a Java Program  Object-Oriented Programming Overview  Example: Payroll.java JDK Tools and.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Polymorphism Dr. Leon Jololian. Dr.Jololian2 class Person { private: string name; int age; public: Person(string na, int ag); Person(string na); string.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
CS100A, Fall Lecture 4 1 CS100A, Fall 1997 Lecture, Thursday, 12 September. This lecture continues the discussion of classes. In addition, the following.
Chapter 10 Object-Oriented Programming: Polymorphism.
CSC241 Object-Oriented Programming (OOP) Lecture No. 16.
Class Inheritance Dr. Leon Jololian. Dr.Jololian2 class Person { private: string name; int age; public: Person(string na, int ag); Person(string na);
COSC 1P02 Intro. to Computer Science 8.1 Cosc 1P02 Only those who attempt the absurd can achieve the impossible.
Example: O-O Payroll Program (§11.4) Object-Oriented Design Behavior. Our program should read a sequence of employees from an input file, ( managers, secretaries,
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
Functions with Input/Ouput. Assignments Due – Lab 2 Reading – Chapter 4 –
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
Dynamic Binding Implementation Object-Oriented Programming Spring
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Topic 10ARRAYS 1 TOPIC 10 continued l Arrays of objects Arrays.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
Exception Handling How to handle the runtime errors.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Exercises on Polymorphism and Operator Overloading TCP1201: 8.
Programming Abstractions Cynthia Lee CS106B. Inheritance Topics Inheritance  The basics › Example: Stanford GObject class  Polymorphism.
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.
Introduction to Programming
Chapter 12 OOP: Polymorphism, Interfaces and Operator Overloading
CMSC 202 Polymorphism.
7. Inheritance and Polymorphism
CSC241: Object Oriented Programming
Programming Fundamental
Polymorphism and Virtual Functions
CS250 Introduction to Computer Science II
Object-Oriented Programming (OOP) Lecture No. 28
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Sampath Kumar S Assistant Professor, SECE
Polymorphism CMSC 202, Version 4/02.
Inheritance -I.
Programming Abstractions in C++, Chapter 19
Presentation transcript:

CSC241 Object-Oriented Programming (OOP) Lecture No. 18

Polymorphism – Case Study A Simple Payroll Application

Problem Statement Develop a simple payroll application. There are three kinds of employees in the system: salaried employee, hourly employee, and commissioned employee. The system takes as input an array containing employee objects, calculates salary polymorphically, and generates report.

OO Model String Employee pStr name taxRate String operator = getName calcSalary SalariedEmp HourlyEmp CommEmp salary hours hourlyRate sales commRate calcSalary calcSalary calcSalary

Class Employee class Employee { private: String name; double taxRate; public: Employee(String&, double); String getName(); virtual double calcSalary() = 0; };

… Class Employee Employee::Employee(String& n, double tr):name(n) { taxRate = tr; } String Employee::getName(){ return name;

Class SalariedEmp class SalariedEmp : public Employee { private: double salary; public: SalariedEmp(String&, double, double); virtual double calcSalary(); };

… Class SalariedEmp SalariedEmp::SalariedEmp(String& n, double tr, double sal) : Employee(n, tr) { salary = sal; } double SalariedEmp::calcSalary() { double tax = salary * taxRate; return salary – tax;

Class HourlyEmp class HourlyEmp : public Employee { private: int hours; double hourlyRate; public: HourlyEmp(string&, double, int, double); virtual double calcSalary(); };

… Class HourlyEmp HourlyEmp::HourlyEmp(String& n, double tr, int h, double hr): Employee(n, tr) { hours = h; hourlyRate = hr; }

… Class HourlyEmp double HourlyEmp::calcSalary() { double grossPay, tax; grossPay = hours * hourlyRate; tax = grossPay * taxRate; return grossPay – tax; }

Class CommEmp class CommEmp : public Employee { private: double sales; double commRate; public: CommEmp(String&, double, double, double); virtual double calcSalary(); };

… Class CommEmp CommEmp::CommEmp(String& n,double tr, double s, double cr) : Employee(n, tr) { sales = s; commRate = cr; }

… Class CommEmp double CommEmp::calcSalary() { double grossPay = sales * commRate; double tax = grossPay * taxRate; return grossPay – tax; }

A Sample Payroll int main() { Employee* emp[10]; emp[0] = new SalariedEmp("Aamir", 0.05, 15000); emp[1] = new HourlyEmp("Faakhir", 0.06, 160, 50); emp[2] = new CommEmp("Fuaad", 0.04, 150000, 10); … generatePayroll(emp, 10); return 0; }

…A Sample Payroll void generatePayroll(Employee* emp[], int size) { cout << "Name\tNet Salary\n\n"; for (int i = 0; i < size; i++) { cout << emp[i]->getName() << '\t' << emp[i]->calcSalary() << '\n'; }

Sample Output Name Net Salary Aamir 14250 Fakhir 7520 Fuaad 14400 …

Never Treat Arrays Polymorphically

Shape Hierarchy Revisited draw calcArea Line Circle Triangle draw calcArea draw calcArea draw calcArea

Shape Hierarchy class Shape { … public: Shape(); virtual void draw(){ cout << "Shape\n"; } virtual int calcArea() { return 0; } };

… Shape Hierarchy class Line : public Shape { ... public: Line(Point p1, Point p2); void draw(){ cout << "Line\n"; } };

drawShapes() void drawShapes(Shape _shape[], int size) { for (int i = 0; i < size; i++) _shape[i].draw(); }

Polymorphism & Arrays int main() { Shape _shape[10]; _shape[0] = Shape(); _shape[1] = Shape(); … drawShapes(_shape, 10); return 0; }

Sample Output Shape …

…Polymorphism & Arrays int main() { Point p1(10, 10), p2(20, 20), … Line _line[10]; _line[0] = Line(p1, p2); _line[1] = Line(p3, p4); … drawShapes(_line, 10); return 0; }

Sample Output Shape // Run-time error

Because Shape Array Line Array 0000 0000 0010 0015 0020 0030 0030 0045 _shape[i].draw(); *(_shape + (i * sizeof(Shape))).draw();

Original drawShapes() void drawShapes(Shape* _shape[], int size) { for (int i = 0; i < size; i++) _shape[i]->draw(); }

Sample Output Line …

Because … _line1 0000 _line2 0004 0008 0012 _line3 Shape* _shape[] _shape[i]->draw(); (_shape + (i * sizeof(Shape*)))->draw();