CS304 - Object Oriented Programming
Course Page
Q & A
Short Question & Answers
Q1: Write the names of two types of Templates.
a. Function Templates (in case we want to write general function like printArray).
b. Class Templates (in case we want to write general class like Array class)
Q2: Describe the way to declare a template function as a friend of any class.
template< class T > class B { int data; friend void doSomething( T ); // granting friendship to template doSomething in // class B friend A< T >; // granting friendship to class A in class B };
Q3: Can a constructor throw an exception? How to handle the errors, when the constructor fails?
Yes, Constructor can throw an exception But In constructor if an exception is thrown than all objects created so far are destroyed. Exception due to constructor of any contained object or the constructor of a parent class can be caught in the member initialization list.
Q4: Why these operators cannot be overloaded: . , .* , ?: , ::
Reason: They take actual current object name, rather than value in their argument
Q5: What is graceful termination method of Error handling; give its example using C++.
Program can be designed in such a way that instead of abnormal termination, that causes the wastage of resources, program performs clean up tasks, mean we add check for expected errors (using if conditions), Example – Graceful Termination int Quotient (int a, int b ) { if(b == 0){ cout << “Denominator can’t “ << “ be zero” << endl; // Do local clean up exit(1); } return a / b; }
Q6: How can you describe Virtual destructor? Why Virtual destructor is used?
When delete operator is applied to a base class pointer, base class destructor is called regardless of the object type. Only base class part of object will be deleted other parts will remain as it is this result in memory leak (wastage of memory), Virtual Destructors make it possible that complete object is being deleted so there is no memory leak (waste of memory).
Q7: Motor car can be called a good candidate for being an object so, this ibject contains Encapsulation characteristic. You are required to apply the concept of Encapsulation on “Motor Car”.
Yes Motor Care is a good candidate for apply the concept of encapsulation In Motor Car we have the following Objects which fully describes the phenomena of encapsulation. 1. Steer Wheels 2. Accelerate 3. Change Gear 4. Apply Brakes 5. Turn Lights On/Off In encapsulation we just provide the user an interface and hide the Full implementation from the user because user don’t want to know the implementation he Just want his work should be done perfectly For example: Breaks of car, we gave just a paddle to the User and encapsulate how it works inside the engine. Same for accelerator, gear, steer wheels and turn on and off of Light.
Q8: What do you know about exception in initialization? Explain with example.
Exception in Initialization: List Exception due to constructor of any contained object or the constructor of a parent class can be caught in the member initialization list. Example Student::Student (String aName) : name(aName) /*The constructor of String can throw a exception*/ { ... } Exception in Initialization: List The programmer may want to catch the exception and perform some action to rectify the problem Example Student::Student (String aName) try : name(aName) { ... } catch (…) { }
Q9: How can we set the default values for non type parameters?
We can set default value for this non type parameters, as we do for parameters passed in ordinary functions, template< class T, int SIZE = 10 > class Array { private: T ptr[SIZE]; public: void doSomething(); … }
Q10: Explain the statement vector ivec (4,3)
This would create a vector of four elements, each one initialized to 3. ivec looks like this: 3 3 3 3
Q11: Tell the procedure of making a class abstract in C++ by giving an example.
In C++, we can make a class abstract by making its function(s) pure virtual. Conversely, a class with no pure virtual function is a concrete class (which object can be instantiated) A class having pure virtual function(s) becomes abstract class Shape { … public: virtual void draw() = 0; }
Q12: Write down a list of four intangible objects?
1. Time 2. Light 3. inteligence 4. Temperature 5.software
Q13: Give the pseudo code of non case sensitive comparison function of string class.
int caseSencompare( char* str1, char* str2 ) { for (int i = 0; i < strlen( str1 ) && i < strlen( str2 ); ++i) if ( str1[i] != str2[i] ) return str1[i] - str2[i]; return strlen(str1) - strlen(str2); }
Q14: Describe three properties necessary a container to implement generic algorithms.
We claimed that this algorithm is generic, because it works for any aggregate object (container) that defines following three operations a. Increment operator (++) b. Dereferencing operator (*) c. Inequality operator (!=)
Q15: Mobile is a good entity for being an object. Write characteristics, behavior and unique ID.
Characteristics: Buttons, Battery, Screen Behavior: Calling, Sending Message, Internet, Multimedia Unique ID: Nokia
Course Instructor

Mr. Shafiq-ur-Rehman
M.Sc.(Electrical Engineering)
George Washington University, USA

C++ How to Program
by Paul J. Deitel

Object-Oriented Software Engineering by Jacobson, Christerson and Jonsson

The C++ Programming Language by Bjarne Stroustrup