What topics does this cover in ?

Hi. Im currently a student enrolling for object oriented programming c++. I have a code I couldn't solve. I tried inserting a few new method but also can seems to be getting it right

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include <iostream>

using namespace std;

class myPointer {
private :
    string label;
    int *data;

    void init (int sz,string lbl)
    {
    data = new int;
    *data = sz;
    label = lbl;
    }
public:
    myPointer(){
    init(2,"X");
    }

    myPointer(int v,string lbl){
    init (v,lbl);
    }
    void set(int v,string lbl){
    *data = v;
    label = lbl;
    }
    void print(){
    cout << label << " = " << *data << endl;
    }

    };

    int main(){
    myPointer a;
    a.set(68,"Z");
    myPointer b(a);
    b.set(21,"W");
    a.print();
    b.print();

    return 0;
    }
You need to write an explicit copy constructor
1
2
3
	myPointer(const myPointer& arg_obj) {
		init(*arg_obj.data, arg_obj.label);
	}
https://www.learncpp.com/cpp-tutorial/911-the-copy-constructor/
thank you for the links :D and also the solution
Topic archived. No new replies allowed.