600.109 - Intro C++ Practice Test 3 ------------------------------------------------------------------------------- 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; -> string s; s = "abra ca dabra"; cout << s.at(2) << " " << s.length() << " " << s.substr(0,6) << endl; s = s + "last"; cout << s << endl; -> 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 -> Which user defined types in C++ can have data (fields) of different types? -> Which type of class member function is used to initialize objects when they are declared in main? -> What is the *.cpp file which contains the function definitions only (not the data members or prototypes) of a class called? -> 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(ostream &); // display data members }; #endif /************* end of Bat.h file **************************/ /****** write Bat.cpp class implementation file here ******/ // Bat.cpp #include "Bat.h" #include /****** 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. -> 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 #include #include - Create a Bat object with length 30 inches, weight 55, radius 2.5 inches. - Display the length of the object just created on the screen. - Assume two Bat objects bat1 and bat2 have been declared and initialized. Copy the data from bat1 into bat2. - Write the stats for a Bat object named bat2 to the screen. -> 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. -> 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 teaching assistant b) barca-lounger chair 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); -> 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; } -> 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) { } ------------------------------------------------------------------------ // 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); } ------------------------------------------------------------------------ // List.h header file for list class, uses integer Node class #include "Node.h" class List { private: Node* head; // beginning of list is the only data public: List(); // constructor for empty list List(Node * h); // constructor with first node passed void addHead(int i); // add data to head of list void addTail(int i); // add data to tail of list void printList(); // output list };