Resources‎ > ‎Programming‎ > ‎C++‎ > ‎C++ Tutorial‎ > ‎Lesson 4‎ > ‎

sample solutions

Practice 1

int &getRefData()
int *getPointerData()

The return value of the above two function can be the "left hand side" of a substitution statement. Hence the member variable data can be changed without usingadd|subValue() functions. Although data was declared as a private variable, these two function exposed it, hence breaking the effect of encapsulation.

You can change them as:

const int &getRefData() const{
    return data;

const int *getPointerData() const {
    return &data;

Practice 2

The problem is to have two delete statements in the loop. For the 2nd loop, pp1 and p2 are already deleted.

for (int...) {

delete pp1, pp1 = NULL;
delete pp2, pp2 = NULL;

You should also substitute NULL to the variable, which should be released.

Practice 3

  • MyObject does not have a declaration of its constructor. Since the constructor(s) are not inherited, you need to define appropriate constructor(s) in the subclass.