Upload
domenic-riley
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
[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]
EnameSalaryRank
[0] [1] [2] [3] [4] [5] [6] [7]
Another possible data structure - an Array of Lists
List1
List2
List(n)
NameSalary
NameSalary
In a list: location [0] contains companyName position determines company rank locations 1..n contain employeeName
An Object-Oriented Design(containment)
CorporateWorld Object
Companies List
Employees List
Has-A
Has-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 moresense?
An Object-Oriented Design(containment vs. inheritance)
Company List
Employee List
Is-A
Company List
Employee List
Has-A
Which one makes moresense?
I would say a CompanyList contains (Has-A)
Employee List
In order words, a Company List is not atype 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 moresense?
An Object-Oriented Design(containment vs. inheritance)
Employee List
List
Is-A
Employee List
List
Has-A
Which one makes moresense?
An Employee List Is-Atype 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 noreason to derive a new class from it. We simply instantiateit with an Employee Record data type.
template <class Item> 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<Item>::List(){
used = 0;current_index = 0;
}
template <class Item>size_t List<Item>::size() const{
return used;}
Modified Implementation File// helper functionstemplate <class Item>void List<Item>::moveUp(size_t start){
for (size_t i = used; i > start; i--)data[i] = data[i-1];
}
template <class Item>void List<Item>::moveDown(size_t start){
for (size_t i = start; i < used-1; i++)data[i] = data[i+1];
}
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;
}
[0] [1] [2] [3] [4] [5] [6] [7]
[0]
CName
[0] [1] [2] [3] [4] [5] [6] [7]CName
[n]
EnameSalaryRank
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<EmployeeRecord> employeeList;
};
Instantiate Class and Test it
companyList<EmployeeRecord> companyEmployeeList;
companyEmployeeList.setName("IBM");cout << companyEmployeeList.companyName() << endl;
strcpy(employee.eName,"Sally");employee.rank = 11;employee.salaryYTD = 75000;
companyEmployeeList.insert(employee);
Functional vs Object-oriented design
At this point, we might want to stop andwrite several functions that manipulate thisdata structure.
This would be a traditional top down design,breaking each action (command) into a separatefunction and passing the Company Lists betweenfunctions.
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
IBMNAME: Joe Salary: 4000 Rank: 1DigitalNAME: Sally Salary: 2000 Rank: 1CompaqNECNAME: Harry Salary: 1050 Rank: 1XEROXUNEMPLOYEDNAME: Pete Salary: 2050