# Corporate World Some thoughts on solving this problem.

## Presentation on theme: "Corporate World Some thoughts on solving this problem."— Presentation transcript:

Corporate World Some thoughts on solving this problem.

[0][1][2][3][4][5][6][7] [0] One possible data structure an Array of structs (or a class) where one member is a List CName [0][1][2][3][4][5][6][7]CName [n] Ename Salary Rank

[0][1][2][3][4][5][6][7] Another possible data structure - an Array of Lists List1 List2 List(n) Name Salary Name Salary In a list: location [0] contains companyName position determines company rank locations 1..n contain employeeName

An Object-Oriented Design Corporate World Companies Employees

An Object-Oriented Design (containment) CorporateWorld Object Companies List Employees List Has-A

An Object-Oriented Design (inheritance) Sorted List List Is-A

An Object-Oriented Design (containment vs. inheritance) Company List Employee List Is-A Company List Employee List Has-A

An Object-Oriented Design (containment vs. inheritance) Company List Employee List Is-A Company List Employee List Has-A Which one makes more sense?

An Object-Oriented Design (containment vs. inheritance) Company List Employee List Is-A Company List Employee List Has-A Which one makes more sense? I would say a Company List contains (Has-A) Employee List In order words, a Company List is not a type of Employee List

An Object-Oriented Design (containment vs. inheritance) Employee List List Is-A Employee List List Has-A

An Object-Oriented Design (containment vs. inheritance) Employee List List Is-A Employee List List Has-A Which one makes more sense?

An Object-Oriented Design (containment vs. inheritance) Employee List List Is-A Employee List List Has-A Which one makes more sense? An Employee List Is-A type of the general List.

An Object-Oriented Design We saw earlier that the List ADT can be generalized by creating a template List class. If the List ADT behavior is acceptable as is, there is no reason to derive a new class from it. We simply instantiate it with an Employee Record data type.

template class List { public: // TYPEDEF and MEMBER CONSTANTS enum { CAPACITY = 30 }; // typedef double Item; ----------------------------------------------------- Item current( ) const; private: Item data[CAPACITY]; Modified Specification File

Modified Implementation File List ::List() { used = 0; current_index = 0; } template size_t List ::size() const { return used; }

Modified Implementation File // helper functions template void List ::moveUp(size_t start) { for (size_t i = used; i > start; i--) data[i] = data[i-1]; } template void List ::moveDown(size_t start) { for (size_t i = start; i < used-1; i++) data[i] = data[i+1]; }

EmployeeRecord struct EmployeeRecord { char eName[10]; int rank; double salaryYTD; };

Instantiate Class List test;

Test Driver of List EmployeeRecord employee; strcpy(employee.eName,"Joe"); employee.rank = 1; employee.salaryYTD = 30000; test.insert(employee);

Driver (continued) for (test.start( ); test.is_item( ); test.advance( )) { cout << test.current().eName << endl; cout << test.current().rank << endl; cout << test.current().salaryYTD << endl; }

Output from Driver Joe 1 30000

[0][1][2][3][4][5][6][7] [0] CName [0][1][2][3][4][5][6][7]CName [n] Ename Salary Rank Now we have the List part of Company List

Company List Class class companyList { public: void insert(const EmployeeRecord & inRec) employeeList.insert(inRec);} EmployeeRecord current() const {return employeeList.current();} bool is_item() {return employeeList.is_item();} void start() {employeeList.start();} void advance() {employeeList.advance();} size_t size() const {return employeeList.size();} void remove_current() {employeeList.remove_current();} const char* companyName() {return CName;} void setName(char cname[]) {strcpy(CName,cname);} private: char CName[10]; List employeeList; };

Instantiate Class and Test it companyList companyEmployeeList; companyEmployeeList.setName("IBM"); cout << companyEmployeeList.companyName() << endl; strcpy(employee.eName,"Sally"); employee.rank = 11; employee.salaryYTD = 75000; companyEmployeeList.insert(employee);

Results from test IBM Jill 22 22000 Sally 11 75000

Functional vs Object-oriented design At this point, we might want to stop and write several functions that manipulate this data structure. This would be a traditional top down design, breaking each action (command) into a separate function and passing the Company Lists between functions.

Or we may want to build a Corporate World object Corporate World Companies Employees

A Corporate World class class CorporateWorld { public: void getCompanyNames(); void JOIN(char[],char[]); void DUMP(); void PAYDAY(); void QUIT(char[]); private: companyList cList[10]; int NumberOfCompanies; int UnEmploymentList; };

Test Driver CorporateWorld CW; CW.getCompanyNames(); CW.JOIN("Joe","IBM"); CW.JOIN("Pete","IBM"); CW.JOIN("Sally","Digital"); CW.PAYDAY(); CW.DUMP(); CW.QUIT("Harry");

Results from test IBM NAME: Joe Salary: 4000 Rank: 1 Digital NAME: Sally Salary: 2000 Rank: 1 Compaq NEC NAME: Harry Salary: 1050 Rank: 1 XEROX UNEMPLOYED NAME: Pete Salary: 2050

Summary Designing a solution to any programming problem is generally a combination of both design paradigms. You should approach the problem with a style that you are comfortable with and tools you are confident in using.