Object-Oriented Programming
(OOP)
Lecture No. 30
Polymorphism – Case Study
A Simple Payroll Application
Problem Statement
• Develop a simple payroll application.
There are three kinds of employees in the
system: salaried emplo...
OO Model
Employee
SalariedEmp HourlyEmp CommEmp
getName
calcSalary
sales
commRate
hours
hourlyRate
salary
calcSalarycalcSa...
Class Employee
class Employee {
private:
String name;
double taxRate;
public:
Employee( String&, double );
String getName(...
… Class Employee
Employee::Employee( String& n,
double tr ): name(n){
taxRate = tr;
}
String Employee::getName() {
return ...
Class SalariedEmp
class SalariedEmp : public Employee
{
private:
double salary;
public:
SalariedEmp(String&,double,double)...
… Class SalariedEmp
SalariedEmp::SalariedEmp(String& n,
double tr, double sal)
: Employee( n, tr ) {
salary = sal;
}
doubl...
Class HourlyEmp
class HourlyEmp : public Employee {
private:
int hours;
double hourlyRate;
public:
HourlyEmp(string&,doubl...
… Class HourlyEmp
HourlyEmp ::HourlyEmp( String& n,
double tr, int h, double hr )
: Employee( n, tr ) {
hours = h;
hourlyR...
… Class HourlyEmp
double HourlyEmp::calcSalary()
{
double grossPay, tax;
grossPay = hours * hourlyRate;
tax = grossPay * t...
Class CommEmp
class CommEmp : public Employee
{
private:
double sales;
double commRate;
public:
CommEmp( String&, double, ...
… Class CommEmp
CommEmp::CommEmp( String& n,
double tr, double s, double cr )
: Employee( n, tr ) {
sales = s;
commRate = ...
… Class CommEmp
double CommEmp::calcSalary()
{
double grossPay = sales * commRate;
double tax = grossPay * taxRate;
return...
A Sample Payroll
int main() {
Employee* emp[10];
emp[0] = new SalariedEmp( “Aamir”,
0.05, 15000 );
emp[1] = new HourlyEmp(...
…A Sample Payroll
void generatePayroll(Employee* emp[],
int size) {
cout << “NametNet Salarynn”;
for (int i = 0; i < size;...
Sample Output
Name Net Salary
Aamir 14250
Fakhir 7520
Fuaad 14400
…
Never Treat Arrays
Polymorphically
Shape Hierarchy Revisited
Shape
Line Circle Triangle
draw
calcArea
draw
calcArea
draw
calcArea
draw
calcArea
Shape Hierarchy
class Shape {
…
public:
Shape();
virtual void draw(){
cout << “Shapen”;
}
virtual int calcArea() { return ...
… Shape Hierarchy
class Line : public Shape {
…
public:
Line(Point p1, Point p2);
void draw(){ cout << “Linen”; }
}
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,...
Sample Output
Shape
Shape
Shape
…
…Polymorphism & Arrays
int main() {
Point p1(10, 10), p2(20, 20), …
Line _line[ 10 ];
_line[ 0 ] = Line( p1, p2 );
_line[ ...
Sample Output
Shape
// Run-time error
Because
Line Array
Shape Array
_shape[ i ].draw();
*(_shape + (i * sizeof(Shape))).draw();
0000
0010
0020
0030
0000
0015
0...
Original drawShapes()
void drawShapes(Shape* _shape[],
int size) {
for (int i = 0; i < size; i++) {
_shape[i]->draw();
}
}
Sample Output
Line
Line
Line
…
Because
…
Shape* _shape[]
_line1
_line2
_line3
_shape[i]->draw();
(_shape + (i * sizeof(Shape*)))->draw();
0000
0004
0008
...
Upcoming SlideShare
Loading in …5
×

Polymorphism (oop) lecture30

851 views
649 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
851
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
25
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Polymorphism (oop) lecture30

  1. 1. Object-Oriented Programming (OOP) Lecture No. 30
  2. 2. Polymorphism – Case Study A Simple Payroll Application
  3. 3. 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.
  4. 4. OO Model Employee SalariedEmp HourlyEmp CommEmp getName calcSalary sales commRate hours hourlyRate salary calcSalarycalcSalary calcSalary name taxRate String String operator = operator << pStr
  5. 5. Class Employee class Employee { private: String name; double taxRate; public: Employee( String&, double ); String getName(); virtual double calcSalary() = 0; }
  6. 6. … Class Employee Employee::Employee( String& n, double tr ): name(n){ taxRate = tr; } String Employee::getName() { return name; }
  7. 7. Class SalariedEmp class SalariedEmp : public Employee { private: double salary; public: SalariedEmp(String&,double,double); virtual double calcSalary(); }
  8. 8. … 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; }
  9. 9. Class HourlyEmp class HourlyEmp : public Employee { private: int hours; double hourlyRate; public: HourlyEmp(string&,double,int,double); virtual double calcSalary(); }
  10. 10. … Class HourlyEmp HourlyEmp ::HourlyEmp( String& n, double tr, int h, double hr ) : Employee( n, tr ) { hours = h; hourlyRate = hr; }
  11. 11. … Class HourlyEmp double HourlyEmp::calcSalary() { double grossPay, tax; grossPay = hours * hourlyRate; tax = grossPay * taxRate; return grossPay – tax; }
  12. 12. Class CommEmp class CommEmp : public Employee { private: double sales; double commRate; public: CommEmp( String&, double, double, double ); virtual double calcSalary(); }
  13. 13. … Class CommEmp CommEmp::CommEmp( String& n, double tr, double s, double cr ) : Employee( n, tr ) { sales = s; commRate = cr; }
  14. 14. … Class CommEmp double CommEmp::calcSalary() { double grossPay = sales * commRate; double tax = grossPay * taxRate; return grossPay – tax; }
  15. 15. 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; }
  16. 16. …A Sample Payroll void generatePayroll(Employee* emp[], int size) { cout << “NametNet Salarynn”; for (int i = 0; i < size; i++) { cout << emp[i]->getName() << ‘t’ << emp[i]->calcSalary() << ‘n’; } }
  17. 17. Sample Output Name Net Salary Aamir 14250 Fakhir 7520 Fuaad 14400 …
  18. 18. Never Treat Arrays Polymorphically
  19. 19. Shape Hierarchy Revisited Shape Line Circle Triangle draw calcArea draw calcArea draw calcArea draw calcArea
  20. 20. Shape Hierarchy class Shape { … public: Shape(); virtual void draw(){ cout << “Shapen”; } virtual int calcArea() { return 0; } };
  21. 21. … Shape Hierarchy class Line : public Shape { … public: Line(Point p1, Point p2); void draw(){ cout << “Linen”; } }
  22. 22. drawShapes() void drawShapes( Shape _shape[], int size ) { for (int i = 0; i < size; i++) { _shape[i].draw(); } }
  23. 23. Polymorphism & Arrays int main() { Shape _shape[ 10 ]; _shape[ 0 ] = Shape(); _shape[ 1 ] = Shape(); … drawShapes( _shape, 10 ); return 0; }
  24. 24. Sample Output Shape Shape Shape …
  25. 25. …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; }
  26. 26. Sample Output Shape // Run-time error
  27. 27. Because Line Array Shape Array _shape[ i ].draw(); *(_shape + (i * sizeof(Shape))).draw(); 0000 0010 0020 0030 0000 0015 0030 0045
  28. 28. Original drawShapes() void drawShapes(Shape* _shape[], int size) { for (int i = 0; i < size; i++) { _shape[i]->draw(); } }
  29. 29. Sample Output Line Line Line …
  30. 30. Because … Shape* _shape[] _line1 _line2 _line3 _shape[i]->draw(); (_shape + (i * sizeof(Shape*)))->draw(); 0000 0004 0008 0012

×