Question #6
6. The following program contains a recursive function.
a) What is the output of the program?
The output is 5. In short, the recursion goes something like this:#includeint recFunc(long number) { if (number == 0) return 0; else return 1 + recFunc(number / 10); } void main(void) { long number = 25436; cout << recFunc(number) << endl; }
Question #7recFunc(25436) = 1 + recFunc(2543) = 1 + 1 + recFunc(254) = 1 + 1 + 1 + recFunc(25) = 1 + 1 + 1 + 1 + recFunc(2) = 1 + 1 + 1 + 1 + 1 + recFunc(0) = 1 + 1 + 1 + 1 + 1 + 0 = 5
Given this definition of a Node:
Implement a recursive function that deletes each Node in a linked list. You will earn more points if you do not use any temporary local variables. (Hint: delete the nodes from the end.) This is the declaration for the function: void deleteList(Node* head);struct Node { int value; char letter; Node* link; };
Question #9void deleteList(Node* head) { if (head) { deleteList(head->link); delete head; } }
Using the Node struct defined in the previous problem and given this code below:
Evaluate each of the following expressions: (similar to #7 on page 1113)void main(void) { Node* ptr1; Node* ptr2; Node* ptr3; ptr2 = new Node; ptr2->value = 5; ptr2->letter = 'a'; ptr2->link = new Node; ptr3 = ptr2->link; ptr3->value = 7; ptr1 = new Node; ptr3->link = ptr1; ptr1->value = 7; ptr1->letter = 'b'; ptr1->link = ptr2; }
Question #11Value Expression a. 5 ptr1->link->value b. TRUE ptr1->value > ptr1->link->value c. Undefined pr1->link->link->letter d. TRUE ptr1 == ptr3->link e. 7 ptr2->link->value f. TRUE ptr2->link == ptr3 g. FALSE pr2->letter < ptr3->link->link->letter h. FALSE ptr1->link == ptr3
11. For a language to be considered an object-oriented language, it must provide 3 facilities. What are these facilities? Describe each of these facilities. (page 907)
1. Data abstraction
2. Inheritance
3. Dynamic binding (Polymorphism)
Question #12
12. Within a program, two classes can be independent of each other and have nothing in common. However, we have learned that there are 2 cases in which objects can be related. What terms do we use to define these two relationships? Explain what each term means. (page 909)
1. Inheritance
2. Composition
Question #13
13. When we derive one class from another, sometimes we want to override a function. Explain what it means to override a function. Be specific. (page 913)
Read page 913
Question #14
14. Given the Node struct defined in question #7, we've created a class called IntList that uses it to implement a linked list. Write the implementation for the Prepend public member function. You don't have to write comments, but you do have to use good programming techniques that you've learned in this class. (page 1065)
Question #15class IntList { public: IntList(void); ~IntList(void); void Prepend(int data, char c); private: Node* head; }; void IntList::Prepend(int data, char c) { Node* newNodePtr = new Node; assert(newNodePtr); newNodePtr->value = data; newNodePtr->letter = c; newNodePtr->link = head; head = newNodePtr; }
Below are the specifications and implementations of 3 classes: Animal, Dog, and Cat. Study them carefully. On the next page there is a short driver program that uses these classes. Answer the question below the driver code.
15. What is the output of the above program?const int MAX_LENGTH = 50; class Animal { public: Animal(char *name); ~Animal(void); virtual void Speak(void); void Display(void); private: char name[MAX_LENGTH + 1]; }; Animal::Animal(char* n) { strncpy(name, n, MAX_LENGTH); name[MAX_LENGTH] = NULL; cout << "Constructing an Animal named " << name << endl; } Animal::~Animal(void) { cout << "Freeing an Animal named " << name << endl; } void Animal::Speak(void) { cout << "???" << endl; } void Animal::Display(void) { cout << name; } class Dog : public Animal { public: Dog(char* name); ~Dog(void); void Speak(void); }; Dog::Dog(char* name) : Animal(name) { cout << "Constructing a Dog named " << name << endl; } Dog::~Dog(void) { cout << "Freeing a Dog" << endl; } void Dog::Speak(void) { cout << "Woof" << endl; } class Cat : public Animal { public: Cat(char* name); ~Cat(void); void Speak(void); }; Cat::Cat(char* name) : Animal(name) { cout << "Constructing a Cat named " << name << endl; } Cat::~Cat(void) { cout << "Freeing a Cat" << endl; } void Cat::Speak(void) { cout << "Meow" << endl; } void main(void) { Dog dog("Fido"); Cat cat("Kitty"); Animal* pet[2]; pet[0] = &dog; pet[1] = &cat; pet[0]->Speak(); pet[1]->Speak(); }
Extra CreditConstructing an Animal named Fido Constructing a Dog named Fido Constructing an Animal named Kitty Constructing a Cat named Kitty Woof Meow Freeing a Cat Freeing an Animal named Kitty Freeing a Dog Freeing an Animal named Fido
Back to Outlinestruct Node { int data; Node* next; Node* prev; }; class IntList { public: void Append(int data); private: Node* head; Node* tail; }; void IntList::Append(int data) { Node* newNodePtr = new Node; assert(newNodePtr); newNodePtr->data = data; newNodePtr->next = NULL; newNodePtr->prev = NULL; if (!tail) head = newNodePtr; else { tail->next = newNodePtr; newNodePtr->prev = tail; } tail = newNodePtr; }