Download Spring 2007 Electronic Engineering Bachelor Exam - Computer Science and more Exams Computer Science in PDF only on Docsity! Cork Institute of Technology Bachelor of Engineering (Honours) in Electronic Engineering- Stage 3 (NFQ Level 8) Spring 2007 Computer Science (Time: 3 Hours) Read instructions carefully Section A: Answer any TWO questions Section B: Answer any TWO questions Use separate answer books for Sections A and B Examiners: Prof. G. Hurley Dr. S. Foley Mr. D. OāDonovan Mr. F. OāReilly Section A 1. (a) Describe, using a basic example, how the virtual function mechanism is used to implement polymorphism, i.e., the āone interface ā multiple methodsā philosophy. [4 %] (b) Define a base class called ācomponentā consisting of i), ii) and ii) below: i) Member data: ā¢ āDevice Nameā - string ā¢ āNumber of Legsā - integer ā¢ āValueā - float ā¢ āCurrentā - float ā¢ āVoltageā - float A constructor that accepts the following parameters: ā¢ āNumber of Legsā and āValueā ii) Private member functions: ā¢ SetValue(float CompValue) - sets the value of the component ā¢ SetsLegNumber(int legNo) - sets the number of legs iii) Public, pure virtual member functions: ā¢ OutputVoltage(void); - returns the number of legs ā¢ InputCurrent (float iValue); - sets the device current [10 %] 2 (c) Derive a class called āresistorā using (c) as a virtual base class. The class should possess the following member functions: i. A constructor that sets the āDevice Nameā and passes the āNumber of Legsā and āValueā to the base class. ii. Override the OutputVoltage() function to return the result āCurrent * Resistorā. [6 %] (d) Overload the member function OutputVoltage(void) function to include a bias voltage as a function argument , i.e., double OutputVoltage(float biasVoltage) using the formula āCurrent * Resistor + biasVoltageā. [5 %] 2. (a) Discuss the purpose and use of constructors and destructors. Show the typical syntax in each case. [6 %] (b) In connection with classes and inheritance, describe the sequence in which constructors and destructors are invoked? [4 %] (c) Illustrate, using an example, how constructor parameters may be passed from a derived class to a base class. [6 %] (d) Explain why a copy constructor is necessary? [2 %] (e) State the output of the program in Figure 1. Indicate, for each output, whether the copy or normal constructor is called and, in each case, indicate why. #include <iostream> using namespace std; class myclass { public: myclass() { cout << "Normal operation.\n"; } myclass(const myclass &obj) { cout << "Copy operation.\n"; } }; myclass f() { myclass ob; return ob; } void main (void) { myclass a; a = f(); } Figure 1 [7 %]