Object Oriented Programming - CS304 Fall 2011 Mid Term Paper PDF
Object Oriented Programming - CS304 Fall 2011 Mid Term Paper PDF
1
MIDTERM EXAMINATION 2011
Q.1 can constant object access the none constant member function of each class.
Answer:- (Page 106)
"Const objects can access only const member functions so chances of change of state of const objects once they
are created are eliminated."
Q.2. Give at least two problems that we should check when we overloading assignments operator ("=") in
string class (unsolved)
Q3. Give c++ code to overloaded unary "--" oprators to comples member class.
Answer:- (Page 165)
class Complex{
...
Complex operator -- (int);
// friend Complex operator --(const Complex &, int);
}
Complex Complex::operator -- (int){
complex t = *this;
real - = 1;
return t;
}
Complex operator -- (const
Complex & h, int){
complex t = h;
h.real - = 1;
return t;
}
Q5.explain the deference between the static variable of a class with none static variable with the help of
example
Answer:- click here for detail
The difference between static and non static members is only that a non static member is tied to an instance of a
class although a static member is tied to the class, and not to a particular instance. That is, a static member is
shared by all instances of a class although a non static member exists for each instance of class.
2
MIDTERM EXAMINATION 2011
3
static data members.......2.mark
Answer:- (Page 108)
“A variable that is part of a class, yet is not part of any object of that class, is called static data member”
class Exam
{
char *ExamName;
int No_of_paper;
public:
Exam()
{
ExamName = "Final Term";
No_of_paper = 5;
}
int main()
{
const Exam exam1;
getch();
return 0;
}
Answer:-
Corrected Code
class Exam
{
char *ExamName;
int No_of_paper;
public:
Exam()
{
ExamName = "Final Term";
No_of_paper = 5;
}
7
{
return ExamName;
}
int getpaper()
{
return No_of_paper;
}
};
int main()
{
Exam exam1;
getch();
return 0;
}
Answer:-
The scope of private data member of a class is that, only an access is allowed within the class. Only member
function of class can access and modify the value of data member value. Derived class or class instance/object
can‟t directly access. Data Member will created for each separate object in the memory and will eliminated
when object will be destroy. Object could be destroy by calling its destructor. Example
Class arthimetic
{
Private:
int a,b;
Public:
Add()
{
8
A+b; // direct access by member function Correct Access
}
};
Int main()
{
Arthimetic c1;
C1.a=4;// is illegal access to class private data member error will generate by compiler
}
Answer:-
Class complex
{
Private:
Double val;
Complex operator ^(complex c1)
{
Return Power(val,c1.val);
}
};
9
Question No: 22 ( Marks: 5 ) (Another related question come in Nov-2011 mid paper)
Write c++ of overloading - operator in complex numbers class which can work in these give situation.
If we have two objects of complex number class as follows,
Complex obj1;
Complex obj3 = 47.4- obj2;
Complex obj4 = obj2-47.4;
Answer:-
Handout Page No.146
We have made them as friend so that we can write them as non member functions and they ar not called with
respect to complex no. Class object instead we pass both arguments (complex no. object and double value) to
this function compiler invoke them according to arguments passed. Their implementation is similar as give
below.
Class complex
{
private:
double real, img;
public:
friend complex operator -(double c1, complex obj);
Complex operator -(double c1)
{
Complex temp;
temp.real=real-c1;
temp.img=img-c1;
return temp;
}
};
//Friend function Definition
Complex operator -( double c1, complex obj)
{
Complex temp;
temp.real=c1-real;
temp.img=c1-img;
return temp;
}
10
void setRollNo(int aRollNo){
rollNo = aRollNo;
}
};
Avoiding Error
void Student::setRollNo(int aRollNo){
if(aRollNo < 0){
rollNo = 0;
}
else
{
rollNo = aRollNo;
}
}
MIDTERM EXAMINATION
2010
Q.1
What is difference between simple association and composition?
Answer:- (PAGE 51-52)
In Association, interacting objects have no intrinsic relationship with other object. It is the weakest link
between objects. While in Composition An object may be composed of other smaller objects, the relationship
between the “part” objects and the “whole”.
Q.2
Friend functions minimize "Encapsulation", What is your opinion?
Answer:- (PAGE 153)
Friend functions minimize encapsulation as we can access private data of any class
using friend functions,
This can result in:
Data vulnerability
Programming bugs
Tough debugging
Q.3
Write three important properties of constructors?
1. Tell the reason why we can not overload the following four operators in c++
. , .* , :: , ?:
2. Considering the complex number class can we subtract two complex numbers by overloading plus
“+ “Operator. Justify your answer as well.
Answer:-
Constructor Properties (Page 74)
Constructor is a special function having same name as the class name
Constructor does not have return type
Constructors are commonly public members
The reason why we can not overload the following four operators in c++ (Click here for detail)
Because they are strictly compile time. There is a very strong argument against sizeof and unary &, since they
11
are defined for all types, and have results which can be used in constant expressions (evaluated at "compile"
time)
Q.4
How we resolve the following problems in overloading of assignment operator in string class, (explain
with the help of c++ code)
Self referencing
Assigning a string value to more than one strings in a single line like, stringobject1 = string object2 =
stringobject3 = stringobject4
Answer:-
Handout Page:150
class String{
…
public:
…
String & operator = (const String &);
};
String & String :: operator = (const String & rhs){
size = rhs.size;
delete [] bufferPtr;
if(rhs.size != 0){
bufferPtr = new char[rhs.size+1];
strcpy(bufferPtr,rhs.bufferPtr);
}
else bufferPtr = NULL;
return *this;
}
Q.5
Consider the class given below explain the order in which variables e,f and g will be initialized after
creating object of this class,
class XYZ{
int e;
int f;
int g;
public:
XYZ ();
};
12
MIDTERM EXAMINATION 2010
Question No: 17 (Marks: 2) (unsolved)
Can we create an array of objects for a class having default constructor?. Justify your answer.
class ABC{
int x;
int y;
int z;
public:
ABC();
};
ABC::ABC():x(10),z(x),y(x)
{
…
}
Answer:-
X=10,z=10 and y=10
class Complex{
...
friend ostream & operator << (ostream & os, const Complex & c);
};
class Complex{
...
friend istream & operator >> (istream & i, Complex & c);
};
14