Professional Documents
Culture Documents
Your Name and Number, and Your Instructor's Name
Your Name and Number, and Your Instructor's Name
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
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:
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:
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:
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(); }