Programming Fundamentals II (C++ II) Final Exam June 17 th, Sun, 2007 NAME:...Number:... Question 1 Define class section that has the following attributes: coursename, coursenumber as integer, credithours as integer, Size (total number of student) as integer, and List as a list of all students IDs. Since the total number of students in a section is variable, List has variable length. Define a constructor for this class that will initialize each of the class attributes. Define the class destructor. Hint: a student ID can be stored in long integer.
Question 2 Define class Book which has two data members, pages (which is an integer that represents the number of pages) and ISBN (which is an integer that represents the ISBN or ID of the book). Write a constructor which uses two input parameters to initialize the data members. Define print as a method that will print out ISBN and pages of the object. Define TextBook as a class that inherits all attributes and methods of Book class. In addition, TextBook has data member price as float (which represents the price of the book). Write a constructor which uses three input parameters to initialize pages, ISBN and price, by calling the constructor of the parent (or base) class. For this class TextBook Define increase_cost as a method that will increase the price of the book by value x. Also, Define print as a method that will print out all attributes of a Textbook object (pages, ISBN, and price), the method should do this by calling print method of the parent class.
Question3 What is the output of the following code A. #include <iostream.h> class A{ protected: int x; public: A() {x=90;} A(int x1) {x=x1;} void print() {cout<<x<<endl;}}; class B:public A{ public: B(){cout<<"In B1"<<endl;} B(int x1):a(x1) {cout<<"in B2"<<endl;}}; void main(){ A a1; B b1; B * b2 = new B(200); a1.print(); b1.print(); b2->print(); } B. #include <iostream.h> class A{ int *a; public: A(int x, int y) {a=new int[2]; a[0]=x; a[1]=y;} void print() {cout<<a[0]<<a[1];} }; void main(){ A a1(10,20); A a2(0,0); a2=a1;a1.print(); a2.print(); } C. void main(){ int a[3]={20,30,40}; int *p=&a[0]; cout<<*p<<*(a+1); p++; *p=*(a+1)+10; cout<<a[0]<<a[1]<<a[2]; }
Question 4 Assume that you have the following class: class Student{ private: int ID; char *name; int grades[5]; }; Add the suitable code to the class Student to make the following code possible. void main() { Student s1, s2, s3; s1++; /* the above statement will increment by 5 each of the elements of grads in object s1*/ cout<< (s3 >s1); /* the above statement will print 1 if the average all elements of grades in s3 is higher than the average elements of grads in object s1, otherwise the statement will print 0 */ If (s1==s2) cout<< same ; /* the above statement will print 1 if s1 is equal to s2, and will print 0 otherwise */ }
Question 5 Define function LocateElement that will take two inputs: an integer x and an array of integer numbers. The function then will return the following outputs: The index (or position) of the first element in the array that has the same value as x. The count of all elements in the array that have value equal to x. The address of last element in the array that has the same value as x. Note: Never use cin or cout in the function
Question 6 Assume the following class is defined. The class name is Car and has two private attributes: ID which is an integer and type which is a string (examples of model "escotta", "corrolla". The class has the following access methods: SetID, Settype, GetID, GetType. The class doesn't have a default constructor; instead it has a constructor that takes two parameters to initializes ID and type. Don't define the Car class or any of the mentioned methods. Define the class copy constructor for the Car class. Define class Employee which has the following private attributes: EmpID as integer, EmpName as string, and EmpCar as object of Car. Implement the class constructor that will receive three parameters to initialize all attributes. Define method print for this class that will print out all the values of all attributes of an Employee object. Note: This Employee class should never have default constructor. Write necessary statements in main function that will define obj1 as an object of Employee class.