Presentation is loading. Please wait.

Presentation is loading. Please wait.

Corporate World Some thoughts on solving this problem.

Similar presentations


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

1 Corporate World Some thoughts on solving this problem.

2 [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

3 [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

4 An Object-Oriented Design Corporate World Companies Employees

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

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

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

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

9 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

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

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

12 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.

13 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.

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

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

16 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]; }

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

18 Instantiate Class List test;

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

20 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; }

21 Output from Driver Joe

22 [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

23 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; };

24 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);

25 Results from test IBM Jill Sally

26 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.

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

28 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; };

29 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");

30 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

31 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.


Download ppt "Corporate World Some thoughts on solving this problem."

Similar presentations


Ads by Google