辅导案例-H7126

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
Candidate Number


H7126

THE UNIVERSITY OF SUSSEX



BEng (final year) AND MEng (year 3) 2019 EXAMINATION



Programming in C++


Assessment period: May/June 2019 (A2)


DO NOT TURN OVER UNTIL INSTRUCTED
TO BY THE LEAD INVIGILATOR

Candidate should answer THREE out of FOUR questions

Duration: 2 hours

If all four questions are attempted, all will be marked, but the lowest mark will
not be counted in the total for the paper.

At the end of the examination the question paper and any answer
books/answer sheets, used or unused, will be collected from you before you
leave the examination room.


Examination handout:
None


Please turn over



H7126 Programming in C++
___________________________________________________________________

2

Question 1.
a) Explain what is meant by the following terms:

i) Class
ii) Object Instance
iii) Abstract class
iv) Method overloading
v) Method overriding
[5 marks]
b) What is the output of the following program? Explain your answer.

voi d myf unct i on( i nt n, i nt &m)
{
n = 150;
m = 250;
}

i nt mai n( )
{
i nt n = 100;
i nt m = 200;

myf unct i on( n, m) ;
cout <<" n=" << n << " m=" < r et ur n 0;
}
[4 marks]
c) Define the following types of error that may occur in a C++ program. In each case
provide an example.

i) Syntax error
ii) Type error
iii) Link error
iv) Logic error
[8 marks]
d) Why is it better to use exceptions in C++ programming, rather than returning an
error code? The user written function:
myOpenFi l e( const st d: : st r i ng& name)
throws the exception:
st d: : r unt i me_er r or ( “ Coul d not open f i l e” ) .
Write C++ code that will catch the exception and print the error message to the
console when the function is called.
[3 marks]


H7126 Programming in C++
___________________________________________________________________

3

Question 2.

a) The four basic features of object-oriented programming languages are said to
be abstraction, polymorphism, encapsulation and inheritance. Define each of
these terms.
[6 marks]
b) What is an abstract class and describe how one could be created in C++?
[2 marks]
c) There are three classes to design: ani mal , cat , and dog. To hold the height,
each class has a protected field hei ght of type i nt . Each class also has a
get and set methods. ani mal is an abstract class and cat and dog are
concrete (i.e. not abstract) subclasses of ani mal .
i) Using abstract concepts write these classes described above in C++.
[9 marks]
ii) Write a function that takes advantage of abstract classes to print the
height of each cat and dog to the console.
[3 marks]
Question 3.

a) What is the difference between a function declaration and a function
definition?
[3 marks]
b) Consider the following partial class definition:
c l ass Dat e
{
pr i vat e:
i nt day;
i nt mont h;
i nt year ;
publ i c:
voi d advance( ) ;
/ / ot her code her e!

} ;

For simplicity, assume that all months have 31 days.

i. Implement a constructor that takes no parameters and will initialise
the date to 31st April 2019.
[3 marks]
ii. Implement the method advance( ) that will increase the date by
one day.
[4 marks]

Question continued over

H7126 Programming in C++
___________________________________________________________________

4



iii. Implement an overload of the default constructor so that the new
constructor can take a user specified date.
[2 marks]
iv. If two date instances are compared we need to check that the year,
month and day are both equal. Add a method to the class that could
do this by overloading the == operator.
[3 marks]
c) A fully implemented class called Bi r t hday, that can store a birthday date
has been written by a programmer. The programmer wants to now store the
birthdays of their class mates. They initialise this storage with:

Bi r t hday * b = new Bi r t hday[ 22] ;

What are the potential pitfalls of this method? Suggest a better method that
could be used when for example an extra person joins the class?
[5 marks]





























H7126 Programming in C++
___________________________________________________________________

5

Question 4.
a) A commonly used programming idiom is Resource Acquisition Is Initialization
(RAII).
i) What is the definition of RAII and what is the benefit of using it?
[4 marks]
ii) Why is better to use a smart pointer such as st d: : uni que_pt r to
manage dynamically allocated memory rather than a plain C++
pointer?
[4 marks]
b) The following code shows a class example that reads some data and
processes it. The code contents of r ead_dat a_f r om_f i l e have not been
shown here but the comments describe what it does.
i) What is the potential cause of a memory leak?
[2 marks]
ii) Suggest two ways in which this could be corrected. Explain why they
work.
[6 marks]

c l ass ReadDat aExampl e
{
publ i c:
i nt r eadDat a( i nt dat a_si ze)
{
i nt * dat a =new i nt [ dat a_si ze] ;
i nt r es= r ead_dat a_f r om_f i l e( dat a) ;
del et e [ ] dat a;
r et ur n r es;
}

pr i vat e:
i nt r ead_dat a_f r om_f i l e( i nt * dat a)
{
/ * Thi s f unct i on, gener at es i nt eger dat a and
st or es i t i n dat a. I t pr ocesses i t and r et ur ns
t he r esul t as an i nt .
I t may t hr ow an Except i on. * /

}
} ;

c) What does the const keyword mean? Give an example of its use in a
method and explain why it is useful.
[4 marks]

END OF PAPER
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468