0% found this document useful (0 votes)
27 views

Nstitl) Te: Ian - o :... Ec No Ogy, . Aragpur

1. The document appears to be an exam for a software engineering course given at the Indian Institute of Technology Kharagpur. 2. It lists the date, time, and duration of the exam as well as instructions for students taking the exam. 3. The exam consists of 5 questions worth varying points totaling 60 points. Space is provided for students to show their work and write their answers.

Uploaded by

ParantapDansana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Nstitl) Te: Ian - o :... Ec No Ogy, . Aragpur

1. The document appears to be an exam for a software engineering course given at the Indian Institute of Technology Kharagpur. 2. It lists the date, time, and duration of the exam as well as instructions for students taking the exam. 3. The exam consists of 5 questions worth varying points totaling 60 points. Space is provided for students to show their work and write their answers.

Uploaded by

ParantapDansana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

l:

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;' $oftware Engineering {CS 20006)

~ 1~
. '
Students: .129 Date: 18-Feb-,14 (A.\'l')
~ ~, .Full marks: 60 Time: 2:hours
'
I
v. N~e - - - -~- . ROil- No.
'>

~
1-
" ~~

.r

Question 'No. 1 I 2 I 3 I A I 5 T':rotal


Marks
Obtained
Max. Mark;'! )5 15 10 10 10 '60

J,<!o;
I

Instructions:

T: Write your n~mltand ro!Inumber in


'I
the space above.

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.

4. Ysethe designated spaces for rough work.'~

'5. M~rks for evyry question is sh~wn with the question.


' ~

6. No f)lr'ther clarifications to any of the questions ~ill be ,P,rovid~9..

,, '
~ '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

J ~~!;,., -~loi ..: ~'~


i~ ~ ~.
.. i- ~~tot
1
~~ .
-.: ~~ ..;;

"~~~" 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

-complex( _______________ ) {} II Destructor


Complex& operator=(const Complex&); II Copy-Assignment Operator

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_ << ")";

-----------------~--'
}

friend --------------- operator>>( _______________ is, ------------------~~ c) {


is >> c.re_ >> c.im_;

operator new( _______________ s); I I new operator

operator delete( _______________ p); II delete operator

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): -----'-'---- ------,---- {}

Complex& pmplex::operato,r=(const Complex& c) {

:lf ( _______ .., _________________ ) {


re_ = c.. re_;
im_ = .c .im_;
}
return -:-----...:;;. ___ ,
}

' Complex Compl~x::operqtor/(const Complex& c) {


'
II Using the operators of the class write t)1is as a single expressio!l

return ------'"--------"--------~-----------------;
}
::.__~ __ _.:._ __________ -,.........-~---~.._ .......
~--~-., .....
"""'-.'-~'"-t-- __,. __ _..,....,,.,;..-1"-- ...!;'->;'..~ ,"-J--~
"' ,.,..;_ " .. ..;, ~
~":' ~ ~.;:-: ~ --:_.-..~ ~~ ,_:__ ~:: _-,- ....., ... ~~-~~

double Complex: :operator;!< 0 .const { /I No.rm

II You may assume functions from the ,standard 'library to be available

return sqr1; <------'--.,.------------------------------~) ;


}

Complex Complex: :operator! ()' const { I I 'Conjugate

return
}

ROUGH WORK
Use this space for your rough work, if any. This part will not b.e evaluated.

'
3

1
1!
I,

-'-"-~-.....~c.-..---=_:_..;;..__......_____=-~--'"""~~---'-----'~; cr(<~.,:,., ~lA..~--~~,w~!t.h~~


2. Suppose that the following methods of class Complex (Question 1) are implemented as follows: .~

Complex::Complex(double re, double im)


{ re_ = re; im_ = im; cout << "Constructed at: " << this << endl; }
,,
Complex: :Complex ( const Complex& c)
, { re_ = c. re_; im_ = c. im_; cout << "Copy Constructed at: "" << this << endl; }

Complex: :-complex() { cout << "Destructed from: " <<this << endl; }
~

II Overloaded Complex::operator new that allocates Complex objects with an


II integer guard space on each side and fills them up with preceding and
II succeeding patterns (as shown in code) to check for overflow on either side
void *Complex: :operator new(size_t s) {
const unsigned int s_int = sizeof(int);

cout << "s =" << s << endl; II decimal for.mat is used fc;>r int

void *P = malloc(s_int + s + s_int); II Pointer to allocated space


cout << "p = 11 << p << endl; II hex format is used for address

int *q = (int *)p; II Poi~ter to preceding guard


cout << 11 q = " << q << endl; II hex format is used for address

*q = Oxffffffff; II Preceding .pattern fill


cout << "P-Guard =" << *q << endl; II decimal format is used for int

q = (int *)((unsigned char *)p + s_int + s); II Pointer to succeeding guard


cout << "q = " << q << endl; II hex format is used for address

*q = Oxfffffffe; II Succeeding pattern fill


cout << 11 S_Guard =" << *q << endl; II decimal format is used for int

void *r = (void *)((unsigned char *)p + s_int); II Pointer to actual object


cout << "r = " << r << endl; II hex format is used for address

return r;
}

II Overloaded Complex::operator delete that de-allocates Complex objects with


II an integer guard on each side
void Complex: :operator delete(void *P) {
const unsigned int s_int = sizeof(int);

cout << "p =" << p << endl; II hex format is used for address

void *q = (void *)((unsigned char *)p- s_int); II Pointer to allocated space


cout << "q = 11 << q << endl; II hex format is used for address

free(q); II Free allocated space

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

3. In the context of class Complex (Question 1), answer the following: ~.

(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]) {}

-complexArray() {delete [] pArr_; } II Clean-up the array object

Complex& operator[] (unsigned inti); II Bounds-checking array index operator


};

int main() { II Application codes

ComplexArray'a(2); II Array of 2 Complex numbers

a[O] =Complex(!, 2); II Fill up the first number


a[1] = Complex(2, 3); II Fill up the second number
cout << a[O] << a[1] << endl; II Print to check

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]

Complex& ComplexArray::operator[] (unsigned inti) {


if ( ________________________________________ ) { II Violation condition

cout << __________________________ .;.. ___ endl; II Violation message

return ____________________ ; II Violation object


}
return ____________________ ; II Normal object
}

ROUGH WORK
Use this space for your rough work, if any. This part will not be evaluated.

6
r ~

l:
!I
tX 11 ~;.:

lil
~'!

.j; , Roil No:

'(~) 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 ,,..,

Compiex.h Header file ..'' ,,; '"

Comp).e;x: . ~xx Implementation file


.i\PP cxx ' J\.ppiicatibn using class Complex

Identif,y where would You place the following: [0.5*4 = 2 lVl:arks]


~
,J Item File
inlirte .Methods not implemented zn situ ,~ ..

(wftp.i,n the . class d~finitio~} "' ____ ______ -;:-------.:..---:.:-


,;;:,;_

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

ROUGH WORK ")

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 *) { }
};

Function invoca-11 Which function is called when at the runtime the


tion expression pointer points to the object as:
pA = &a; pA = &b; pA = &c;
pA->f(2); A: :f
------------------~----

pA->g(3.2); II A::g

pA->h(&a); A: :h

pA->h(&b); A: :h

Function invoca- II Which function is called when at the runtime the


tion expression pointer'points to the object as:
pB = &a; I pB = &b; pB = &c;
pB->f(2);
---------------------- I -----------------------
pB->g(3.2);
----------------------- I -----------------------
pB->h(&a);

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;

II Assume &a= Ox002FFAD4


pB = static_cast<B *>(&a);
cout << pB << endl;
pB = dynamic_cast<B *>(&a);
cout << pB << 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; }
};

class C2: public C1 { private:


M2 m2;
public:
C20 { cout << "C2 Constructed" << endl; }
-c2() { cout << "C2 Destructed" << endl; }
virtual void f(M1& m) { this->C1: :f(m); }
virtual void gO { cout << "C2: :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 '

~
;/

~ .J-.~ if~ 1JP:'i:'

You might also like