Question: A program contains the following code. Line numbers have been added to make it easier for you to refer to them in your answer, but

A program contains the following code. Line numbers have been added to make it
easier for you to refer to them in your answer, but they are not part of the program.
01 class MyClass {
02 public:
03 MyClass(int, int*);
04 void exec(void);
05 private:
06 int i1;
07 int* ip;
08};
09 MyClass::MyClass(int a, int* b){
10 this->i1= a;
11 this->ip = b;
12}
13 int MyClass::exec(void){
14*ip += i1;
15}
(a) What does an object of MyClass do?[5]
(b) Write out the type of MyClasss constructor in english words. [4]
(c) Rewrite the constructor so that it uses an initialisation list instead of explicit
assignment. [2]
(d) A programmer writes the following code which compiles without error:
int* v {0};
MyClass c(2, v);
c.exec();
When the program runs, it produces a segmentation fault. Explain what the
programmer has done wrong. [4]
(e) The code is to be converted to use a std::unique_ptr object instead of a
primitive pointer. Write down the declaration of v using such a pointer.
[4]
(f) What changes would have to be made to MyClass to allow it to operate with
a unique pointer instead of a primitive pointer?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!