CS304
CS304
CS304-OOP
(Solved Subjective)
LECTURE FROM
(23 to 45)
Question No. 2:
Describe the way to declare a template function as a friend of any class. 2marks
Answer:- (Page 294)
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
};
Question No. 3:
Can a constructor throw an exception? How to handle the errors, when the constructor fails?
Answer:-
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.
Question No. 4:
Why these operators can not be overloaded:
.,.*,?:,::
Question No. 5:
What is graceful termination method of Error handling; give its example using C++.
Answer:- (Page 330)
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),
AL-JUNAID TECH INSTITUTE
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;
}
Question No. 6:
How can you describe Virtual destructor? Why Virtual destructor is used?
Answer:- (Page 234)
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).
Question No. 7:
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”.
Answer:-
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.
Question No. 8:
Suppose base class and derived class has a member function with same signature. Now call that member
function through the pointers to a base class object. Now, what determines that the base class member
function or derived class function will be called?
Question No. 9:
What do you know about exception in initialization? Explain with example.
Answer:- (Page 345)
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.
AL-JUNAID TECH INSTITUTE
Example
Student::Student (String aName) : name(aName)
/*The constructor of String can throw a exception*/
{
...
}
Answer:
AL-JUNAID TECH INSTITUTE
As each container need to perform certain operations on the elements added to it like their copy while creating
another instance of container or their comparison while performaing certain operation on them like their sorting
, so the elements that we are going to add in containers should provide this kind of basic functionality.
Examples of these functionalities are given below,
When an element is inserted into a container, a copy of that element is made using,
, Copy Constructor Assignment Operator
So the elements need to be added to any container should provide copy and assignment functionality.
• Associative containers and many algorithms compare elements so the elements that are added to
associative containers should have this functionality,
o Operator ==
o Operator <
c++ does not provide functionality of comparison operator (==) or less than operator (<) by itself so we have to
provide this functionality by ourselves in element class if we want to use it in associative containers.
You can think of vectors as smart arrays. They manage storage allocation for you, expanding and contracting
the size of the vector as you insert or erase data. You can use vectors much like arrays, accessing elements
with the [] operator. Such random access is very fast with vectors. as we can access any element of vector
using its index so we can use random access iterator.
It’s also fast to add (or push) a new data item onto the end (the back) of the vector. When this happens, the
vector’s size is automatically increased to hold the new item.
You can think of vectors as smart arrays. They manage storage allocation for you, expanding and contracting
the size of the vector as you insert or erase data. You can use vectors much like arrays, accessing elements
with the [] operator. Such random access is very fast with vectors. as we can access any element of vector
using its index so we can use random access iterator
It’s also fast to add (or push) a new data item onto the end (the back) of the vector. When this happens, the
vector’s size is automatically increased to hold the new item.
Disadvantages:
• Can consume memory if used without care.
Question No. 32:
define composition and give its example with coding
Answer:- (Page 53)
An object may be composed of other smaller objects, the relationship between the “part” objects and the
“whole” object is known as Composition.
Example
Ali is made up of different body parts; They can’t exist independent of Ali.
Answer:-
The format for declaring function templates with type parameters is:
The only difference between both prototypes is the use of either the keyword class or the keyword typename.
Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.
class A{
private:
int a,b,c;
public:
AL-JUNAID TECH INSTITUTE
….
};
class B{
private:
int d,e,f;
A obj1;
public:
….
};
Question No. 44:
Is it possible to have Virtual Constructor? Justify your answer.
Answer: -
There is nothing like Virtual Constructor. The Constructor cant be virtual as the constructor is a code which is responsible for
creating a instance of a class and it cant be delegated to any other object by virtual keyword means.
In virtual inheritance there is exactly one copy of the anonymous base class object
Example:
class Vehicle{
protected:
int weight;
};
class LandVehicle :
public virtual Vehicle{
};
class WaterVehicle :
public virtual Vehicle{
};
Example
class AmphibiousVehicle:
public LandVehicle,
public WaterVehicle{
public:
AmphibiousVehicle(){
weight = 10;
}
};
~Base() { delete [] p; }
AL-JUNAID TECH INSTITUTE
};
class Derived : public Base
{
char * q;
public:
Derived() { q = new char[20]; }
~Derived() { delete [] q; }
};
void foo()
{
Base* p = new Derived();
delete p;
}
With this program, every time function foo is called, some memory will leak.
Explain why memory will leak. Also, explain how to fix this problem.
Answer:-
All of them (i, j, k) will be available in function Test().
daughter (int a)
{ cout << "daughter: int parameter\n\n"; }
};
class son : public mother {
public:
son (int a) : mother (a)
{ cout << "son: int parameter\n\n"; }
};
int main () {
daughter rabia (0);
son salman(0);
return 0;
}
Answer:-
mother: no parameters
daughter: int parameter
b1.data = 7;
}
int main(int argc, char *argv[])
{
char i;
Test(i);
system("PAUSE");
return 0;
}
AL-JUNAID TECH INSTITUTE
Question No: 69 ( Marks: 3 )
Tell the logical error/s in the code given below with reference to resource management; also describe how we
can correct that error/s.
class Test{
public:
int function1(){
try{
FILE *fileptr = fopen(“filename.txt”,“w”);
throw exception();
fclose(fileptr);
return 0;
}
catch(Exception e){
...
}
}
};
int function1(){
try{
FILE *fileptr = fopen(“filename.txt”,“w”);
fwrite(“Hello World”,1,11,fileptr);
...
throw exception();
fclose(fileptr);
} catch(...) {
fclose(fileptr); // adding fclose in catch handler as well
throw;
}
return 0;
}
class c1{
public:
virtual void function(){
cout<<”I am in c1”<<endl;
}
AL-JUNAID TECH INSTITUTE
};
class c2: public c1{
public:
void function(){
cout<<”I am in c2”<<endl;
};
class c3: public c1 {
public:
void function(){
cout<<”I am in c3”<<endl;
}
};
int main(){
Answer:-
a. Public members of base class are _ public members of derived class
b. Protected members of base class are _ protected members of derived class.
Snippet No.1
Snippet No.2
template< >
{…}
Snippet No.3
class B : public A< T* >
{…}
Snippet No.4
template< >
class B< char* > : public A
{…};
Snippet No.5
template< class T >
class B : public A< T* >
{…}
AL-JUNAID TECH INSTITUTE
Table:
#include<iostream.h>
int main()
{
try
{
cout <<”Trying.\n”;
sample_function(98.6);
cout << “Trying after call.\n”;
}
catch(int)
{
cout << “Catching.\n”;
}
Answer:-
Trying.
Starting sample_function.
Catching.
End program.
AL-JUNAID TECH INSTITUTE