1
M.Sc Marine Geophysics II Semester 2009 Object-Oriented Programming in C++ Test 3
19 - 3 - 2009 Time: 2 hrs. SECTION - A Answer ANY 10 questions. Each question carries 4 marks. 1. What is meant by data encapsulation / data hiding? What is its significance? 2. What is a Virtual function? What is its use? 3. What is meant by pure virtual function? Give one example of a pure virtual function. 4. What is the necessity for an abstract class? How to recognize an abstract class? 5. What are overloaded functions and overloaded operators? Give two examples in each. 6. What is polymorphism in object-oriented programming? Give an example of it. 7. What are late binding and early binding? How is it implemented in C++? 8. What is the keyword new in C++? Show an example of dynamically creating an array of size given by the user. 9. What is meant by heap / free storage in C++? What is its use? How to avoid the risk of heap getting exhausted in a program? 10. What is the necessity for a virtual class? Show one example for it. 11. What is default argument in a function? What are the conditions for giving default arguments? 12. What is meant by inheritance in object-oriented programming? Give two examples. 13. What are constructors and destructors in C++? What are the uses for them? SECTION B Answer ALL questions. Each question carries 5 marks. 14. The specifier / declaration of a class called complx, which uses complex numbers as user defined data, is shown below. Define all those member functions of the class which are not defined. See the comments to find what is function does.
// This shows a complex class and how it is used in a program class complx { private: double real; double imag;
Max.Marks:50
// to keep the real part a COMPLEX // NUMBER // to keep the imaginary part of a COMPLEX // NUMBER
public: complx() {real = imag = 0.0;} complx(double r, double img = 0) {real = r; imag = img;} void setval(double r, double i) {real = r; imag = i;} void getval(); void showval(); double r2pabs(); double r2pang(); complx operator + (complx); complx operator - (complx); complx operator * (complx); complx operator / (complx); }; 15.
// empty constructor // constructor with two // arguments
// allows user to set the values // // // // // // // // // // // // gets the values from user displays the complex number in the usual format of displaying complex numbers returns the absolute value returns the angle in degrees adding two complex nos. and returns the result as an object of class complx. subtract one from another multiply two complex nos. divide one from another
// END OF CLASS SPECIFICATION
Give the complete class specification for showing the length as a user defined data in meters and centimeters. Call this class length. Let there be two private data members like int mt and float cm and appropriate public member functions so that one can use the length as any standard data. Show a simple test program to use the objects of this class.