Module 29
Partha Pratim
Das Module 29: Programming in C++
Objectives &
Outline
Dynamic Binding (Polymorphism): Part 4
Binding:
Exercise
Staff Salary
Processing
Partha Pratim Das
C Solution
Summary Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
ppd@[Link]
Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan
NPTEL MOOCs Programming in C++ Partha Pratim Das 1
Module Objectives
Module 29
Partha Pratim Understand design with class hierarchy
Das
Objectives &
Outline
Binding:
Exercise
Staff Salary
Processing
C Solution
Summary
NPTEL MOOCs Programming in C++ Partha Pratim Das 2
Module Outline
Module 29
Partha Pratim Binding Exercise
Das
Staff Salary Processing
Objectives &
Outline
C Solution
Binding:
C++ Solution
Exercise Non-Polymorphic Hierarchy
Staff Salary Polymorphic Hierarchy
Processing
Polymorphic Hierarchy (Flexible)
C Solution
Summary
NPTEL MOOCs Programming in C++ Partha Pratim Das 3
Binding: Exercise
// Class Definitions // Application Codes
Module 29 A a;
class A { public:
Partha Pratim virtual void f(int) { } B b;
Das virtual void g(double) { } C c;
int h(A *) { }
Objectives & }; A *pA;
Outline
class B: public A { public: B *pB;
Binding: void f(int) { }
Exercise
virtual int h(B *) { }
Staff Salary };
Processing
class C: public B { public:
C Solution
void g(double) { }
Summary int h(B *) { }
};
Initialization
Invocation pA = &a; pA = &b; pA = &c;
pA->f(2); A::f B::f B::f
pA->g(3.2); A::g A::g C::g
pA->h(&a); A::h A::h A::h
pA->h(&b); A::h A::h A::h
NPTEL MOOCs Programming in C++ Partha Pratim Das 4
Binding: Exercise
// Class Definitions // Application Codes
Module 29 A a;
class A { public:
Partha Pratim virtual void f(int) { } B b;
Das virtual void g(double) { } C c;
int h(A *) { }
Objectives & }; A *pA;
Outline
class B: public A { public: B *pB;
Binding: void f(int) { }
Exercise
virtual int h(B *) { }
Staff Salary };
Processing
class C: public B { public:
C Solution
void g(double) { }
Summary int h(B *) { }
};
Initialization
Invocation pB = &a; pB = &b; pB = &c;
pB->f(2); Error B::f B::f
pB->g(3.2); Downcast A::g C::g
pB->h(&a); (A *) to No conversion (A *) to (B *)
pB->h(&b); (B *) B::h C::h
NPTEL MOOCs Programming in C++ Partha Pratim Das 5
Staff Salary Processing:
Problem Statement
Module 29 An organization needs to develop a salary processing application
Partha Pratim for its staff
Das
At present it has an engineering division only where Engineers
Objectives &
Outline
and Managers work. Every Engineer reports to some Manager.
Binding:
Every Manager can also work like an Engineer
Exercise
The logic for processing salary for Engineers and Managers are
Staff Salary
Processing different as they have different salary heads
C Solution
Summary In future, it may add Directors to the team. Then every
Manager will report to some Director. Every Director could also
work like a Manager
The logic for processing salary for Directors will also be distinct
Further, in future it may open other divisions, like Sales division,
and expand the workforce
Make a suitable extensible design
NPTEL MOOCs Programming in C++ Partha Pratim Das 6
C Solution:
Engineer + Manager
Module 29 How to represent Engineers and Managers?
Partha Pratim
Das
struct
Objectives &
How to initialize objects?
Outline
Initialization functions
Binding:
Exercise
How to have a collection of mixed objects?
Staff Salary
Processing Array of union
C Solution
Summary How to model variations in salary processing algorithms?
struct-specific functions
How to invoke the correct algorithm for a correct employee
type?
Function switch
Function pointers
NPTEL MOOCs Programming in C++ Partha Pratim Das 7
C Solution:
Engineer + Manager
Module 29 #include <stdio.h>
#include <string.h>
Partha Pratim
Das typedef enum E_TYPE { Er, Mgr } E_TYPE;
typedef struct Engineer { char *name_; } Engineer;
Objectives & Engineer *InitEngineer(const char *name) { Engineer *e = (Engineer *)malloc(sizeof(Engineer));
Outline e->name_ = strdup(name); return e;
}
Binding:
void ProcessSalaryEngineer(Engineer *e) {
Exercise
printf("%s: Process Salary for Engineer\n", e->name_);
Staff Salary }
Processing typedef struct Manager { char *name_; Engineer *reports_[10]; } Manager;
Manager *InitManager(const char *name) { Manager *m = (Manager *)malloc(sizeof(Manager));
C Solution
m->name_ = strdup(name); return m;
Summary }
void ProcessSalaryManager(Manager *m) {
printf("%s: Process Salary for Manager\n", m->name_);
}
typedef struct Staff { E_TYPE type_;
union { Engineer *pE; Manager *pM; };
} Staff;
NPTEL MOOCs Programming in C++ Partha Pratim Das 8
C Solution:
Engineer + Manager
Module 29 int main() {
Staff allStaff[10];
Partha Pratim allStaff[0].type_ = Er;
Das allStaff[0].pE = InitEngineer("Rohit");
allStaff[1].type_ = Mgr;
allStaff[1].pM = InitManager("Kamala");
Objectives & allStaff[2].type_ = Mgr;
Outline allStaff[2].pM = InitManager("Rajib");
allStaff[3].type_ = Er;
Binding:
allStaff[3].pE = InitEngineer("Kavita");
Exercise
allStaff[4].type_ = Er;
Staff Salary allStaff[4].pE = InitEngineer("Shambhu");
Processing
for (int i = 0; i < 6; ++i) {
C Solution
E_TYPE t = allStaff[i].type_;
Summary if (t == Er) ProcessSalaryEngineer(allStaff[i].pE);
else if (t == Mgr) ProcessSalaryManager(allStaff[i].pM);
else printf("Invalid Staff Type\n");
}
return 0;
}
-----
Output:
Rohit: Process Salary for Engineer
Kamala: Process Salary for Manager
Rajib: Process Salary for Manager
Kavita: Process Salary for Engineer
Shambhu: Process Salary for Engineer
NPTEL MOOCs Programming in C++ Partha Pratim Das 9
C Solution:
Engineer + Manager + Director
Module 29 How to represent Engineers and Managers?
Partha Pratim
Das
struct
Objectives &
How to initialize objects?
Outline
Initialization functions
Binding:
Exercise
How to have a collection of mixed objects?
Staff Salary
Processing Array of union
C Solution
Summary How to model variations in salary processing algorithms?
struct-specific functions
How to invoke the correct algorithm for a correct employee
type?
Function switch
Function pointers
NPTEL MOOCs Programming in C++ Partha Pratim Das 10
C Solution:
Engineer + Manager + Director
Module 29 #include <stdio.h>
#include <string.h>
Partha Pratim
Das typedef enum E_TYPE { Er, Mgr, Dir } E_TYPE;
typedef struct Engineer { char *name_; } Engineer;
Objectives & Engineer *InitEngineer(const char *name) { Engineer *e = (Engineer *)malloc(sizeof(Engineer));
Outline e->name_ = strdup(name); return e;
}
Binding:
void ProcessSalaryEngineer(Engineer *e) {
Exercise
printf("%s: Process Salary for Engineer\n", e->name_);
Staff Salary }
Processing typedef struct Manager { char *name_; Engineer *reports_[10]; } Manager;
Manager *InitManager(const char *name) { Manager *m = (Manager *)malloc(sizeof(Manager));
C Solution
m->name_ = strdup(name); return m;
Summary }
void ProcessSalaryManager(Manager *m) {
printf("%s: Process Salary for Manager\n", m->name_);
}
typedef struct Director { char *name_; Manager *reports_[10]; } Director;
Director *InitDirector(const char *name) { Director *d = (Director *)malloc(sizeof(Director));
d->name_ = strdup(name); return d;
}
void ProcessSalaryDirector(Director *d) {
printf("%s: Process Salary for Director\n", d->name_);
}
typedef struct Staff { E_TYPE type_;
union { Engineer *pE; Manager *pM; Director *pD; };
} Staff;
NPTEL MOOCs Programming in C++ Partha Pratim Das 11
C Solution:
Engineer + Manager + Director
int main() { Staff allStaff[10];
Module 29 allStaff[0].type_ = Er;
allStaff[0].pE = InitEngineer("Rohit");
Partha Pratim allStaff[1].type_ = Mgr;
Das allStaff[1].pM = InitManager("Kamala");
allStaff[2].type_ = Mgr;
allStaff[2].pM = InitManager("Rajib");
Objectives & allStaff[3].type_ = Er;
Outline allStaff[3].pE = InitEngineer("Kavita");
Binding: allStaff[4].type_ = Er;
Exercise allStaff[4].pE = InitEngineer("Shambhu");
allStaff[5].type_ = Dir;
Staff Salary allStaff[5].pD = InitDirector("Ranjana");
Processing
C Solution for (int i = 0; i < 6; ++i) {
E_TYPE t = allStaff[i].type_;
Summary if (t == Er) ProcessSalaryEngineer(allStaff[i].pE);
else if (t == Mgr) ProcessSalaryManager(allStaff[i].pM);
else if (t == Dir) ProcessSalaryDirector(allStaff[i].pD);
else printf("Invalid Staff Type\n");
}
return 0;
}
-----
Output:
Rohit: Process Salary for Engineer
Kamala: Process Salary for Manager
Rajib: Process Salary for Manager
Kavita: Process Salary for Engineer
Shambhu: Process Salary for Engineer
Ranjana: Process Salary for Director
NPTEL MOOCs Programming in C++ Partha Pratim Das 12
Module Summary
Module 29
Partha Pratim Practiced exercise with binding – various mixed cases
Das
Started designing for a staff salary problem and worked
Objectives &
Outline out C solutions
Binding:
Exercise
Staff Salary
Processing
C Solution
Summary
NPTEL MOOCs Programming in C++ Partha Pratim Das 13
Instructor and TAs
Module 29
Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor ppd@[Link] 9830030880
Outline Tanwi Mallick, TA tanwimallick@[Link] 9674277774
Binding: Srijoni Majumdar, TA majumdarsrijoni@[Link] 9674474267
Exercise Himadri B G S Bhuyan, TA himadribhuyan@[Link] 9438911655
Staff Salary
Processing
C Solution
Summary
NPTEL MOOCs Programming in C++ Partha Pratim Das 14