Nstitl) Te: Ian - o :... Ec No Ogy, . Aragpur
Nstitl) Te: Ian - o :... Ec No Ogy, . Aragpur
l
' \( ' . ' .,
I .nd..
Ian I.. nstitl)te
.. o f'r:T1 .. hno 1ogy, K
. .. h..aragpur
. 1
,1;;:'
~ :...ec
-""''
~~~
Department,of computer Seience and Engineering_
'
"l ~ ~
''j
,Mid-Semester Examination, Spring 2013-14
~'%
~ 1~
. '
Students: .129 Date: 18-Feb-,14 (A.\'l')
~ ~, .Full marks: 60 Time: 2:hours
'
I
v. N~e - - - -~- . ROil- No.
'>
~
1-
" ~~
.r
J,<!o;
I
Instructions:
2.-
~
On the top of every odd,page 'Yrite your. roll number .. \
~: Answer all' gves~ions in the space. provided il).this paper itsel~. No extra ~heet will be provided.
,, '
~ '1 ROUGH WORK
'
'' l
'' Use t~is SP,acefor yo.'!-lr ro11;gh work, if any. This part will not qe evaluated.
.:1 '
.r f
"
\ '
. -1!'
A -~~''
~ . r' .,
' '. ..,
,,
- ~':'11'"~ .,
n, . I'
~ ~
, ;
.~-rt ! '~-.;I~~ (C
1f I~ ~
l
j .I
'!1 ~~,~ 't
"~~~" lJi:Jl!.-~,
1. You need to develop Complex data type in C++ to deal with complex numbers. A complex number c is ..
.,,,
represented as a doublet (re_ + j . im_) where re_ & im_ are real numbers and represent the real and imaginary
parts of c respectively; and j*j = -1. These numbers should support the operations for Complex type as
defined in the following class definition.
"
class Complex {
public:
~
Complex(double = 0.0, double= 0.0); II Constructors
"
Complex( ______________________________ ); II Copy Constructor
II BINARY OPERATORS
Complex operator+(const Complex&); II Add two complex numbers
Complex operator-(const Complex&); II Subtract the 2nd comp~ex number from the 1st
Complex operator*(const Complex&); II Multiply two complex numbers
Complex operatorl(const Complex&); II Divide the 1st complex number by the 2nd
II UNARY OPERATORS
double operator*() const; II Norm of the complex number
Complex operator!() const; II Complex conjugate
II !lOs
friend operator<<( _______________ os, -------------------- c) {
os << "(" << c.re_ << ", " << c.im_ << ")";
-----------------~--'
}
II CONSTANTS OF DATATYPE 1l
static const Complex sc_cUnityR; II Defines 1+j0
static const Complex sc_cUnityi; II Defines O+j1 l {
static const Complex sc_cZero; II Defines O+jO 1
tl' ' 1-
c\...,.,_
private:
double re_;
double im_;
II The real part
II The imaginary part
''
I'
r
T
}; \
\''\
l
2 "
\
-
I"j
Roll No:
~~."
'
(a) Fill i11.Jlle blank::; to complete the c.lass definition. [0.5 * 14 = 7 Marks]
~
Note: 'If a blapk isto be .. left as blank; just write BLANK in its nlace.
' (b) Partial implementations of a select set of methods are given b'elow. Fill in the l;>lanks to complete the
given implementations, [(0.5+0.5) + (1+1) + 2 + 1 + 2 8 Marks] =
Com~lex: :Complex(double re = O.Q, double, im = 0.0): -----'-'---- ------,---- {}
return ------'"--------"--------~-----------------;
}
::.__~ __ _.:._ __________ -,.........-~---~.._ .......
~--~-., .....
"""'-.'-~'"-t-- __,. __ _..,....,,.,;..-1"-- ...!;'->;'..~ ,"-J--~
"' ,.,..;_ " .. ..;, ~
~":' ~ ~.;:-: ~ --:_.-..~ ~~ ,_:__ ~:: _-,- ....., ... ~~-~~
return
}
ROUGH WORK
Use this space for your rough work, if any. This part will not b.e evaluated.
'
3
1
1!
I,
Complex: :-complex() { cout << "Destructed from: " <<this << endl; }
~
cout << "s =" << s << endl; II decimal for.mat is used fc;>r int
return r;
}
cout << "p =" << p << endl; II hex format is used for address
return;
}
And assume that the remaining methods are as implemented by you in Question 1.
4
j, I
I,
1' ,,
"f 11
~
I1.
Roll No:
l'. r.
Write the output from the fcillow;ing,code: [1 *15 = .J5 Marks]
~~
int maip()
{,
Comple'x z(3.3, 4.4); ..
Complex *p = new Compiex(z);
cout << "Data. = " .<< *P .<< endl;
cout.t << "Worm ="
<< *C(int *)p - 1) << endl;
deleie R;
return ,0;
}
Assume:
c
&z: in main() ) OxOOE4F9FO
p (in operato:r new()) = Ox01.263900
sizeof(int) ='4
sizeof'CComp1ex) = 16
The output is:
,f
~
-
------.~-------------7.-------;--------------
.
----~------~-----~------~------~----------
----~------~------~-------------~---------
-----------~---------------------------~--
.
------------------------------------------
'
~
----~-------------------------~-----------
--------:':'":-.------------~.--------------:------
---~.-----.::..------.-------------------!:...------
i.)
---,-----------------------~-------..:..----::-,:---
---"-----------~----~------~-----------~---
'.,...\;,,;;.il>li~~~- ... ,;
I
(a) You need'to design class ComplexArray that suitably wraps arrays of Complex objects. Unlike a usual
C++ array, you want this array to check for violations of array index bounds. So when a ComplexArray :~
object has size or maximum number of elements as ARRAY-SIZE, then trying to access any element with
index less than 0 or more than ARRAY_SIZE-1 should report a violation.
Consider the following (partial) definition of class ComplexArray and its intended application codes:
class ComplexArray {
unsigned int size_; II Maintain the size (number of elements) of the array
Complex *pArr_; II The array
public:
ComplexArray(unsigned int size): II Construct the array object
size_(size), pArr_(new Complex[size]) {}
a[2] Complex(3, 4); II Fill up the third number- should violate the bound
return 0;
}
Complete the implementation of the array indexing operator of the class. [1 +1+2 +1 = 5 Marks]
ROUGH WORK
Use this space for your rough work, if any. This part will not be evaluated.
6
r ~
l:
!I
tX 11 ~;.:
lil
~'!
'(~) Jl}stifY why the ;following code does not compile? (1 )\1~rkj
.....
'.~ 1 '+ 9omplex(1,1.h ~
i'
Hov.: would you change the overload.ing of operator; to Il,lake'the above code valid?' You just need lo
~
provide the signature of the overlo(j.ded operatorfunction- no implementationis required. [2 .Marks]
(c) suppose your Compl'~x application project comprises the 'folli:Jwing files:
"
'File Name File. Contents ,,..,
F
,non-inline J\1ethod~ not implemented in situ
(wit.hin the ,class definitt()n)
"~ friend functions rwt impiemente.!=I 'in. situ
. (witb~n the class definltio~)
Static Dal~ Members
Use tnis spdce for: your ro?J,gh work, if any. This paiLwill not be evaluated.
~~
,. ''!'
" '
7
~;
~~~.
11
~
m\ ~~i-~tii!;t_\~.wt,1t~
4. (a) In the context of the following code fill up (dashed lines) the tables below: (0.5*8 + 0.5*8 = 8 Marks]
II Class Definitions I II Application Codes
class A { public: A a; 'i '
virtual void f(int) {} B b;
virtual void g(double) { } c c;
int h(A *) { }
}; A *pA;
class B: public A { public: B *pB;
void f(int) { }
virtual int h(B *) { }
};
class C: public B { public:
void g(double) { }
il!t h(B *) { }
};
pA->g(3.2); II A::g
pA->h(&a); A: :h
pA->h(&b); A: :h
pB->h(&b);
ROUGH WORK
Use this space for your rough work, if any. This part will not be evaluated.
..!-~
8
Roll No:
(b) In the context of Question 4a, write the output from the following snippet: [0.5*4 = 2 Marks]
~ I I Assume &b = Ox002FFAC8 I The output is:
pA = static_cast<A *>(&b);
cout << pA << endl;
pA = dynamic_cast<A *>(&b);
cout << pA << endl;
5. (a) Write the output from the following program: [0.5*12 =6 Marks]
class Ml { public: The output is:
M10 { cout << "Ml Constructed" << endl; }
-Ml 0 { cout "Ml Destructed" endl; }
};
class M2 { public:
M20 { cout << "M2 Constructed" << endl; }
-M20 { cout "M2 Destructed" endl; }
};
class Cl { protected:
Ml ml;
public:
C1 () { cout << "C1 Constructed" << endl; }
virtual -c1 () {
cout << "C1 Destructed" << endl; }
virtual void f(M1&) = 0 {
this->gO;
cout << "Pure Method Invoked" << endl; }
virtual void gO { cout "C1: :g" endl; }
};
,;
void main() {
M1 m;
-;: C1 *P = new C2;
p->f(m);
delete p;
}
9
,/
(b) You are given the following class X. You' are not allowed to edit this Ciass.
'"'1
"\, class X {
~.
....,.'
int. data_;
~
public:
X(int d): data_(d) {}
-xo n
int GetData'O conl31; { :retm;-11 data_; }
friend ostream& opem3.tor(os'tream& os, cons~ 'lc&'x) {
OS << "X. Object.= II << x.d<~;ta:... << endl';
return .os;
~'
}
};
You have written .P. new class y and you intend to provide type c.onyersions from objects of type x to
objects of type .Y and vice-versa. Complete class Y. [2 *' 2 = 4 Marks]
class Y { 1
.double datac.;
public:
Y(double d): data_(d) {}
II X to'Y Conversion
..
1
'
~
II Y to X Conversion
------------------------~-----------------------------1-- t -----------;-
friend ostream:& operator (6streiip!& os, 'const Y& y) {
~
os ~"Y Object.= " <<;: Y1ata_. en'dl; t~
}
"return os; '
' ,>;.;
};
ROUGlf WORK
Use ;this space for your r,ough work, if any. ;J'his part will .not be evaluated.
( !)
- " ,.,ll
~
),
':\;
~;
.,
.-
f~"'' ' ""' ~ _:., ~~
10 '
~
;/