// OK, I can't define a print method for ints, but I can teach // cout how to print animals and humans. That gives me a way // to deal with animals, humans, and ints with the same // functions. #include #include struct animal { string name, species; int weight, price; animal(string n, string s, int w, int p) { name=n; species=s; weight=w; price=p; } }; struct human { string name; int weight, phone; human(string n, int w, int p) { name=n; weight=w; phone=p; } }; ostream & operator<<(ostream & os, animal * ap) { os << " " << ap->name << "/" << ap->species << ":" << ap->weight << "g," << ap->price << "c"; return os; } ostream & operator<<(ostream & os, human * hp) { os << " " << hp->name << ", " << hp->weight << " lbs, " << hp->phone; return os; } template void printall(T * data, int N) { for (int i=0; i T * reprice(T * a) { a->price = (int)(a->price*1.1 + 0.5); return a; } template T * fatten(T * a) { a->weight = (int)(a->weight*1.2 + 0.5); return a; } template void map(T * f(T *), T * * data, int N) { for (int i=0; iweight > f->weight) f = db[i]; return f; } void main() { animal * * list = new animal * [10]; list[0] = new animal("anne", "ant", 1, 1); list[1] = new animal("bert", "badger", 400, 50); list[2] = new animal("colin", "cobra", 50, 75); list[3] = new animal("donna", "donkey", 1200, 123); list[4] = new animal("ella", "elephant", 345678, 3200); list[5] = new animal("frank", "frog", 120, 7); list[6] = new animal("geoffrey", "georaffe", 3456, 5450); list[7] = new animal("hanna", "hamster", 12, 43); list[8] = new animal("iggy", "iguana", 160, 72); human * * people = new human * [7]; people[0] = new human("James", 145, 6341234); people[1] = new human("Jason", 132, 8756134); people[2] = new human("Jemimah", 107, 3627212); people[3] = new human("Jilly", 121, 5551212); people[4] = new human("Joe", 180, 2223333); int * numbers = new int[7]; numbers[0] = 0; numbers[1] = 1; numbers[2] = 22; numbers[3] = 333; numbers[4] = 4444; numbers[5] = 55555; numbers[6] = 666666; cout << "original animals\n"; printall(list, 9); map(reprice, list, 9); map(fatten, list, 9); cout << "after inflation\n"; printall(list, 9); animal * a = findfattest(list, 9); cout << "fattest animal is " << a << "\n"; cout << "\n"; cout << "original people\n"; printall(people, 5); map(fatten, people, 5); cout << "after inflation\n"; printall(people, 5); cout << "\n"; cout << "original numbers\n"; printall(numbers, 7); }