CSC326H1 20199 631598475152ece326 final ...
CSC326H1_20199_631598475152ece326_final_answer.pdf-Programming Languages Final Exam Solution ECE326,
Showing 7-10 out of 20
CSC326H1 20199 631598475152ece326 final answer.pdf...
CSC326H1_20199_631598475152ece326_final_answer.pdf-Programming Languages Final Exam Solution ECE326,
CSC326H1 20199 631598475152ece326 f...
CSC326H1_20199_631598475152ece326_final_answer.pdf-Programming Languages Final Exam Solution ECE326,
Page 7
Programming Languages
Final Exam Solution
ECE326, Fall 2019
Question 4.
Object-Oriented Programming
[28 marks]
struct X {
char x[15];
virtual void foo() {
print("X.foo");
}
void bar() {
print("X.bar");
}
};
struct Y : X {
int y;
void bar() {
print("Y.bar");
}
void baz() {
print("Y.baz");
}
};
struct A {
int a;
virtual void qux()=0;
void baz() {
print("A.baz");
}
};
struct B : A {
long y;
virtual void tor() {
print("B.tor");
}
void bar() {
print("B.bar");
}
};
struct M : B, Y
{
int m;
virtual void qux() { print("M.qux"); }
virtual void tor() { print("M.tor"); }
};
int main() {
M mp;
Y & yp = mp;
X & xp = yp;
B & bp = mp;
A & ap = mp;
/* method calls for part d. */
return 0;
}
a)
Suppose the address of
mp
is
0x7ffd69d24c90
, what is the address of
yp
? [1 mark]
0x7ffd69d24ca8
Page 7 of 20


Page 8
Programming Languages
Final Exam Solution
ECE326, Fall 2019
b)
Draw the data structure layout for class M. Assume 8-byte alignment and the target architecture is
64-bit. Draw a line to show increasing address. [9 marks]
M
int m
4 bytes
Y
int y
4 bytes
X
padding
char c[15]
__vptr
1 byte
15 bytes
8 bytes
B
long b
8 bytes
A
padding
int a
__vptr
4 bytes
8 bytes
4 bytes
1 mark for each correctly placed variable/padding/vptr.
-1 mark per incorrectly placed class
-1 mark for extra padding/vptr
c)
For the inheritance hierarchy shown above, Write the entries in each of the five classes’ virtual table
in the correct order. Also state of the size of class M in bytes. [9 marks]
X::vtable
A::vtable
X::foo
nullptr
Y::vtable
B::vtable
X::foo
nullptr
B::tor
M::vtable
X::foo
M::qux
M::tor
sizeof(M) =
56
increasing
address
Page 8 of 20


Page 9
Programming Languages
Final Exam Solution
ECE326, Fall 2019
d)
What is the output for the following method calls? If the method call is illegal (either a runtime or
compile-time error), state the issue. [9 marks]
xp.foo()
X.foo
xp.bar()
X.bar
xp.baz()
Illegal – class X does not have method named baz
ap.qux()
M.qux
ap.tor()
Illegal – class A does not have method named tor
bp.tor()
M.tor
bp.baz()
A.baz
mp.foo()
X.foo
mp.bar()
Illegal – bar is ambiguous between X.bar and B.bar
Page 9 of 20


Page 10
Programming Languages
Final Exam Solution
ECE326, Fall 2019
Question 5.
Template Metaprogramming
[10 marks]
In assignment 3, one of the tasks involved was building the client to send request packets to the server.
Recall that the format of Value looks like this in a packet:
type
size
buf
4 bytes
4 bytes
size
bytes
Integer Example:
1
8
42
4 bytes
4 bytes
8 bytes
Where type is one of:
enum ValueType { INTEGER = 1, FLOAT = 2, STRING = 3, };
And that a Row contains an array of values like this:
count
value[0]
value[1]
...
value[count-1]
4 bytes
struct Request {
Packet packet;
void putvalue(long val);
void putvalue(double val);
void putvalue(const char *);
template<typename... Args>
void putrow(Args&&... args);
} req;
class Packet {
/* internal buffer */
public:
//
use this function to
//
add data to the end
//
of the buffer
void pack(const char * bytes,
int size);
};
a)
Complete the implementation for serializing a 64-bit integer into a packet. You do not have to
perform endianness conversion. The
type
field needs to be
INTEGER
, and the
size
field needs to
sizeof(long)
. Hint: use
packet.pack
to serialize data into the buffer. [0 marks]
void Request::putvalue(long val) {
int temp = INTEGER;
packet.pack((const char *)&temp, sizeof(int));
temp = sizeof(long);
packet.pack((const char *)&temp, sizeof(int));
packet.pack((const char *)&val, temp);
/* 1 mark for each line of code */
/* THIS QUESTION WAS REMOVED FROM FINAL EXAM */
/* DO NOT MARK */
}
Page 10 of 20


Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
University of Toronto
University_of_Toronto
School:
Programming_Languages
Course:
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades

Students also viewed documents