int main() { Cat Frisky; cout << "Frisky age: " << Frisky.getAge() << endl; cout << "Setting Frisky to 6..." << endl; Frisky.setAge(6); cout << "Creating Boots from Frisky" << endl; Cat Boots(Frisky); cout << "Frisky age: " << Frisky.getAge() << endl; cout << "Boots age: " << Boots.getAge() << endl; cout << "Setting Frisky to 7..." << endl; Frisky.setAge(7); cout << "Frisky age: " << Frisky.getAge() << endl; cout << "Boots age: " << Boots.getAge() << endl; return 0; }
//Insert //Orders cats based on their ages //Algorithm: If the cat is younger than you, insert it //before this one. //Otherwise, if last in line, or if the new cat is older than you, //and also younger than next in line, insert it after this one. //Otherwise call insert in the next in line void Node::insert(Node * newNode) { int newAge = newNode->getCat()->getAge(); int thisCatsAge = itsCat->getAge(); if ( newAge < thisCatsAge) { Cat * newCat = newNode->getCat(); Node * oldNode = new Node(itsCat); oldNode->setNext(itsNext); itsNext = oldNode; itsCat = newCat; delete oldNode; oldNode = 0; return; } if(!itsNext) { itsNext = newNode; return; } int nextCatsAge = itsNext->getCat()->getAge(); if ( newAge >= thisCatsAge && newAge < nextCatsAge ) { newNode->setNext(itsNext); itsNext = newNode; return; } itsNext->insert(newNode); }
void Node::display() { cout << "Name: " << itsCat->getName() << ", "; cout << "age: " << itsCat->getAge() << endl; if(itsNext) itsNext->display(); }
Cat::Cat(const Cat & rCat) { itsAge = new int; itsWeight = new int; *itsAge = rCat.getAge(); *itsWeight = *(rCat.itsWeight); }