Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

IUE Faculty of Engineering and Computer Sciences 2010-2011 Spring Semester CS116 Introduction to Programming II Midterm Exam II (May

11th, 2011)

This exam document has 5 pages and 4 questions. The exam duration is 100 minutes long. Write the followings on every page: your

name and number, and your instructors name.

The followings are strictly prohibited:

Making use of any books, papers or memoranda, or computers or mobile telephones. Speaking or communicating with other students. Purposely exposing written papers to the view of other students.

Questions Points

1 /20

2 /25

3 /25

4 /30

Total /100

Name: Number: Instructor Name:

Name:

Number:

QUESTION 1 (20 pts)


Explain the error(s). #include <iostream> using namespace std; class BaseClass { public: BaseClass( ) { x = 0; y = 0; } virtual void incrementX ( ) = 0; int getX( ){ return x; } void setX( int value ){ x = value; } protected: int y = 0; private: int x; }; class DerivedClass : public BaseClass { public: DerivedClass( ) : BaseClass( ) { } virtual void incrementX ( ) { x = x + 1 ; } }; int main( ) { BaseClass firstObject; DerivedClass* secondObject = new DerivedClass; cout<<secondObject.getX(); return 0; }

Sample solution:
Error#1: " int y=0; " /* attribute can not be initialized that way */ Error#2: " x=x+1; " /* private member of BC is hidden in DC */ Error#3: " BaseClass firstObject; " /*abstract class can not be used to instantiate object */ Error#4: "secondObject.getX();" /*secondObject is a pointer*/

Name:

Number:

QUESTION 2 (25 pts)


Implement the followings: a) Create a concrete base class Printer. Add a virtual function called print(). b) Create a subclass called InkJet from base class Printer. Implement its print() method. c) Create a subclass called Laser from base class Printer. Implement its print() method. d) Create a class called Office. Add a public function called printText which takes a Printer pointer as a parameter. e) Test these classes in the main function. Sample solution:
#include <iostream> using namespace std; class Printer{ public: virtual void print(){ cout<<"This text is printed by base class Printer"<<endl; } }; class InkJet:public Printer{ public: virtual void print(){ cout<<"This text is printed by derived class Inkjet"<<endl; } }; class Laser:public Printer{ public: virtual void print(){ cout<<"This text is printed by derived class Laser"<<endl; } }; class Office{ public: void printText(Printer * ptr){ ptr->print(); } }; int main() { Printer prnt; InkJet *iPtr=new InkJet; Laser laserPrinterObject; Office myOffice; myOffice.printText(&prnt); myOffice.printText(iPtr); myOffice.printText(&laserPrinterObject); return 0; }

Name:

Number:

QUESTION 3 (25pts)
Implement a shape hierarchy with Shape as an abstract base class that has the pure virtual methods print and area. You should also implement the following derived classes: Triangle and Rectangle. Each derived class implements the print method to output the appropriate attributes in the class and the area method to compute the area of the object. Note: You are free in designing the data members of the classes. It is not necessary to write any test client.

Sample solution:
#include <iostream> using namespace std; class Shape{ public: virtual void print()=0; virtual double area()=0; }; class Triangle:public Shape{ public: virtual void print(){ cout<<"height:"<<height<<" base:"<<base<<endl; } virtual double area(){ return height*base/2; } private: double height; double base; }; class Rectangle:public Shape{ public: virtual void print(){ cout<<"length:"<<length<<" width:"<<width<<endl; } virtual double area(){ return length*width; } private: double length; double width; };

Name:

Number:

QUESTION 4 (30 pts)


Part_a. (10 pts) Declare and define a Person class that includes data members to represent a persons name, and identification number. The class interface includes set and get methods that provide appropriate access to the data members and an output() method that prints the data information to the screen. The class interface also includes a parameterized constructor and a destructor. Part_b. (10 pts) Derive a Professor class from Person. This subclass should add one boolean data member: retired. If a professor is not retired, the value of this data is False, otherwise it is True. The interface of the subclass also adds appropriate set and get methods for this data member. The subclass should overload the output() method within the derived class. It is not necessary to write any explicit constructor or destructor. Part_c. (10 pts) Write a main program to test the Person and Professor classes. You should first instantiate sample objects from both classes and then set the corresponding data members and finally output the information. Note: If you have no answers for Part_a and/or Part_b, assume that those class declarations are available in Person.h and Professor.h header files. You may include the files in your test program to implement an appropriate main solution.
Sample solution: #include <iostream> #include <string> using namespace std; class Person { public: Person(string n = "Anonymous", int i = 9999) : name(n), id (i) {} void output () { cout << "name = " << name << " id = " << id << endl; } void setName(string n) { name = n;} string getName () { return name;} void setId (int i) { id = i;} int getId () { return id;} private: string name; int id; }; class Professor : public Person { public: // Following Professor constructor is optional Professor(string name = "Anonymous", int i = 99999) : Person(name,i) {} void output() { Person::output(); // You may repeat the parent class code instead. if (retired) cout << "Retired\n"; else cout << "Not Retired\n"; } void setRetired (bool b) { retired = b;} bool getRetired () {return retired;} private: bool retired; };

main() { // Call Constructor with default parameters. Person p1; p1.output(); // Set Attributes. p1.setName("Ayse"); p1.setId(33333); p1.output(); // Call Constructor wtih one default parameter. Person p2("Ahmet"); p2.output(); // No default parameters. Person p3("Ahmet", 12345); p3.output(); // Instantiate subclass Professor. Pointer. Professor *prof1 = new Professor(); // Set its attributes. prof1->setRetired(false); prof1->setName("Cahit Aybet"); prof1->setId(55555); prof1->output(); delete prof1; // Non pointer instantiation. Professor prof2("Turhan Tunali", 111111); prof2.setRetired(false); prof2.output(); }

You might also like