Midterm Exam 5 April 20, 2015 Name: Section 1: Multiple Choice Questions (24 pts total, 3 pts each) Q1: Which of the following is not a kind of inheritance in C++? a. public. b. private. c. static. d. protected. ANS c. static. Q2: Which of the following is most likely a base class of the other three? a. automobile. b. convertible. c. minivan. d. sedan. ANS a. automobile. Q3: To declare class subclass a privately derived class of superclass one would write: a. class subclass : private superclass b. class subclass :: private superclass c. class subclass < private superclass > d. class subclass inherits private superclass ANS a. class subclass : private superclass Q4: Assuming the following is the beginning of the constructor definition for class BasePlus- CommissionEmployee which inherits from class Point, BasePlusCommissionEmployee::BasePlusCommissionEmployee( string first, string last, string ssn, double sales, double rate, double salary ) : CommissionEmployee( first, last, ssn, sales, rate ) The second line: a. Invokes the CommissionEmployee constructor with arguments. b. Causes a compiler error. c. Is unnecessary because the CommissionEmployee constructor is called automatically. d. Indicates inheritance. ANS a. Invokes the CommissionEmployee constructor with arguments. Q5: Base class constructors and assignment operators: a. Are not inherited by derived classes. b. Should not be called by derived class constructors and assignment operators. c. Can be inherited by derived classes, but generally are not. d. Can call derived-class constructors and assignment operators. ANS: a. Are not inherited by derived classes. Q6: Which of the following statements about polymorphism is false? a. With polymorphism, you can direct a variety of objects to behave in manners appropriate to those objects without even knowing their types. b. With polymorphism, new types of objects that can respond to existing messages can easily be incorporated into a system without modifying the base system.
c. Polymorphism enables you to deal in specifics and let the execution-time environment concern itself with the generalities. d. To get polymorphic behavior among existing objects, those objects must be instantiated from classes in the same inheritance hierarchy. ANS: c. Polymorphism enables you to deal in specifics and let the execution-time environment concern itself with the generalities. Actually, the reverse is true. Q7: If objects of all the classes derived from the same base class all need to draw themselves, the draw function would most likely be declared: a. private b. virtual c. protected d. friend ANS: b. virtual Q8: The main difference between a pure virtual function and a virtual function is: a. The return type. b. The member access specifier. c. That a pure virtual function cannot have an implementation. d. The location in the class. ANS: c. That a pure virtual function cannot have an implementation. Section 2 Question 1 (26 pts) Write code for two classes: Vehicle and Car. The classes need to provide constructors, destructor, and Describe functions so that the following main function can print out the output shown below. #include <iostream> #include <string> using namespace std; class Vehicle //Constructor for Vehicle //Solution: Vehicle(string num) //2 pt : regnum(num) cout << "Vehicle Constructor" << endl; // 2 pt //Destructor for Vehicle virtual ~Vehicle(void) // need to have virtual 1 pt cout << "Vehicle Destructor" << endl; // 1 pt for number 2
//Function Describe virtual void Describe(void) // Need to have virtual 2 pts cout << "Unknown vehicle, registration " << regnum << endl; string regnum; ; class Car : public Vehicle //Constructor for Car Car(string m, string num) : Vehicle(num), make( m ) // need to call Vehicle as initializer 2 pts cout << "Car Constructor" << endl; //Destructor for Car ~Car(void) cout << "Car Delete" << endl; // 1 pt for number 1 //Function Describe virtual void Describe(void) //1 pt for the whole function cout << "Car (" << make << "), registration " << regnum << endl; string make; ; int main() Car v1 ("BMW", "ABC 123");
Vehicle* vp4 = new Car ("Jaguar","XYZ 123"); vp4->describe(); delete vp4; return 0; Output: Vehicle Constructor Car Constructor Vehicle Constructor Car Constructor Car (Jaguar), registration XYZ 123 Car Delete Vehicle Destructor Car Delete Vehicle Destructor Question 2 (25 pts): Write the output of the following code, and explain why. #include <iostream> using namespace std; class A A(int n = 2) : x(n) cout << "A constructor" << endl; virtual void Add(int n) x+=n; virtual ~A() cout << x << endl; cout << "A destructor" << endl; int x; ; class B : public A B(int n) : A(n)
y=0; z=0; cout << "B constructor" << endl; void Add(int n) x = 0; y+=n; z = 1; ~B() cout << "B destructor"<< endl; std::cout << y << z; private: int y; int z; ; int main() int c=1; B *b = new B(5); A *a; a=b; a->add(c); delete a; std::cout << std::endl; return 0; Solution: A constructor B constructor B destructor 110 A destructor -- 5 pts each 3. (20 pts) Design and write code for three classes: Polygon, Rectangle, and Triangle. The classes need to provide constructors and area functions so that the following main function can print out the output shown below. The area of a rectangle can be computed with width*height, while the area of a triangle can be computed with width*height/2. One of the classes should be an abstract class. #include <iostream>
using namespace std; int main () Polygon * ppoly1 = new Rectangle; Polygon * ppoly2 = new Triangle; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; delete ppoly1; delete ppoly2; return 0; Output: 20 10 Solution: class Polygon int width, height; void set_values (int a, int b) width=a; height=b; virtual int area (void) =0; void printarea (void) cout << this->area() << endl; ; class Rectangle: public Polygon int area (void) return (width * height); ; class Triangle: public Polygon int area (void) return (width * height / 2); ;