Below are sample questions & answers for the 3rd test. ---------------------------------------------------------------------------- What is the exact output of each code segment? -> string s1 = "first test"; cout << s1.substr(4, 3); cout << s1.at(s1.length()-2) << endl; t ts -> string s; s = "abra ca dabra"; cout << s.at(2) << " " << s.length() << " " << s.substr(0,6) << endl; s = s + "last"; cout << s << endl; r 13 abra c abra ca dabralast -> Write C++ statements to do the following: - declare an array of 3 string class objects - set the first one to be your first name - set the second one to be your last name ============================= string sra[3]; sra[0] = "Joanne"; sra[1] = "Houlahan"; ============================== -> Which user defined types in C++ can have data (fields) of different types? struct, class -> Which type of class member function is used to initialize objects when they are declared in main? constructor -> What is the *.cpp file which contains the function definitions only (not the data members or prototypes) of a class called? implementation (*.h files are the interface) -> Given the following class interface, write the class definition (Bat.cpp) below. /************************************************************/ // Bat.h - class for baseball bats #ifndef BAT_H #define BAT_H class Bat { private: float radius; // radius of bat in inches int length; // length of bat in inches int weight; // weight of bat in ounces // must be between 50 and 70 public: Bat(float rd, int ln, int wt); // constructor // data members must get positive values void setWeight(int); // must be between 50 and 70 void setRadius(float); // must be positive int getLength(); // return length void printstats(); // display data members }; #endif /************* end of Bat.h file **************************/ /****** write Bat.cpp class implementation file here ******/ // Bat.cpp #include "Bat.h" #include // Bat.cpp #include "Bat.h" #include Bat :: Bat (float rd, int ln, int wt) { setWeight(wt); setRadius(rd); if (ln>0) length = ln; } void Bat :: setWeight(int w) // must be between 50 and 70 { if (w>=50 && w<=70) weight = w; } void Bat :: setRadius(float r) // must be positive { if (r > 0) radius = r; } int Bat :: getLength() { return length; } void Bat :: printstats() // display data members { cout << "Bat data: "; // optional cout << "length = " << length; cout << " radius = " << radius; cout << " weight = " << weight; } /****** end of Bat.cpp ******/ -> Write the prototypes and descriptive comments for two other public member functions which you would like to include in the Bat class above. You do not need to implement them. void setLength(int l); // set the length to l if > 0 void readstats(istream &); // read the data values for a bat -> Write exactly ONE C++ statement for each question below, as it would appear in a main() driver program. Assume the following: #include "Bat.h" #include - Create a Bat object with length 30 inches, weight 55, radius 2.5 inches. Bat b1(2.5, 30, 55); - Display the length of the object just created on the screen. cout << b1.getLength(); - Assume two Bat objects bat1 and bat2 have been declared and initialized. Copy the data from bat1 into bat2. bat2 = bat1; - Write the stats for a Bat object named bat2 to the screen. bat2.printstats(); -> Identify at least three classes and three different types of relationships among them which would be used in the following problem statement. Draw the classes and their relationships in a UML diagram. Put verbal descriptors for the relationship types on the arrows for clarity. [6] You run a veterinary practice specializing in cats. Each different breed has its own treatment plan and pricing structure. You need to keep health records on the pets and generate printed service bills. VetPractice ---> (has) Cat, - - -> (depends on) iostream Tabby --+> (is) Cat Persian --+> (is) Cat -> For each pair of classes below, indicate if there is an inheritance relationship between them, and if so, which is the base class and which is the subclass. [3] a) student (base) teaching assistant (subclass) b) barca-lounger (sub) chair (base) neither c) course professor -> Write exactly two statements, using functions from the Node class below which will link these nodes so that they are in the order 25, 13, 10. Node* nd1 = new Node(13); Node nd2(25); Node* nd3 = new Node(10); ================== nd1->setNext(nd3); nd2.setNext(nd1); ================== -> What's the output of the following program? // usenodes.cpp - main for list processing with Node class below #include "Node.h" #include #include main() { Node *head = 0, *tail = 0, *temp; int num; for (num=1; num<=5; num++) { temp = new Node(num); if (!temp) { cerr << "not enough memory" << endl; exit(1); } if (!tail) tail = temp; if (!head) head = temp; else { temp->setNext(head); head = temp; } } temp = head; while (temp) { cout << temp->getInfo() << " "; temp = temp->getNext(); } cout << endl; } ========= 5 4 3 2 1 ========= -> Write a function to do a search for an integer parameter value in a singly linked list (function header is given below). The function is supposed to return the address of (pointer to) the node where the parameter value is found. Don't forget to include the case where the value is not found. Assume this function is being added to the List class studied in lecture. The List and Node class headers may be found at the end of the test for your reference. [12] Node* List :: search(int num) { if (head == NULL) return NULL; Node * curr = head; while (curr) { if (curr -> getInfo() == num) return curr; else curr = curr->getNext(); } return NULL; } ------------------------------------------------------------------------ // Node.h - header file for Node class #ifndef NODE_H #define NODE_H #include class Node { private: int info; Node * next; public: Node (int data); Node (int data, Node * nptr); ~Node (); // destructor destroys the whole list void setInfo (int i); int getInfo (); void setNext (Node * nptr); Node * getNext (); void addAfter (int x); // make node with data x and add after this bool isLast (); friend class List; }; #endif -------------------------------------------------------------------- #include "Node.h" #include #include Node:: Node (int data) { info = data; next = NULL; } Node:: Node (int data, Node *nptr) { info = data; next = nptr; } Node:: ~Node () { delete next; // recursively deletes all nodes linked behind this } void Node:: setInfo (int i) { info = i; } int Node:: getInfo () { return info; } void Node:: setNext (Node *nptr) { next = nptr; } Node* Node:: getNext () { return next; } void Node:: addAfter(int x) { next = new Node(x, next); } bool Node:: isLast() { return (next == NULL); } ------------------------------------------------------------------------