Resources‎ > ‎Programming‎ > ‎C++‎ > ‎C++ Tutorial‎ > ‎

Lesson 6

Practice 1 (Referencing and const)

Type and try the following code. If you observe error/warning, discuss how it should be fixed.

#include <iostream>
using namespace std;

class MyClass {
    int num;
public:
    MyClass(int n = 0) : num(n) {}
    MyClass(MyClass &clazz) : num(clazz.num) {}
    void operator=(MyClass &clazz) {
        if (this == &clazz)
            return;
        num = clazz.num;
    }
    bool operator==(MyClass &clazz) {
        return num == clazz.num;
    }
};

int main() {
    MyClass clazz1(3999);
    const MyClass clazz2(4345);  //<-- I don't want the content of this object be changed

    if (clazz1 == clazz2)
        cout << "clazz1 and clazz2 is the same" << endl;
    else
        cout << "clazz1 and clazz2 is not the same" << endl;

    clazz1 = clazz2;
    if (clazz1 == clazz2)
        cout << "clazz1 and clazz2 is the same" << endl;
    else
        cout << "clazz1 and clazz2 is not the same" << endl;

    MyClass clazz3(clazz1);
    if (clazz1 == clazz3)
        cout << "clazz1 and clazz3 is the same" << endl;
    else
        cout << "clazz1 and clazz3 is not the same" << endl;

    return 0;
}

Practice 2 (another Referencing and const)

Type and try the following code. If you observe error/warning, discuss how it should be fixed.

#include <iostream>
using namespace std;

class MyClass {
    int num;
public:
    MyClass(int n = 0) : num(n) {}
    MyClass(MyClass &clazz) : num(clazz.num) {}
    void operator=(MyClass &clazz) const {
        if (this == &clazz)
            return;
        num = clazz.num;
    }
    bool operator==(MyClass &clazz) const {
        return num == clazz.num;
    }
};

int main() {
    MyClass clazz1(3999);
    const MyClass clazz2(4345); //Do not modify this!

    if (clazz1 == clazz2)
        cout << "clazz1 and clazz2 is the same" << endl;
    else
        cout << "clazz1 and clazz2 is not the same" << endl;

    clazz1 = clazz2;
    if (clazz1 == clazz2)
        cout << "clazz1 and clazz2 is the same" << endl;
    else
        cout << "clazz1 and clazz2 is not the same" << endl;

    MyClass clazz3(clazz1);
    if (clazz1 == clazz3)
        cout << "clazz1 and clazz3 is the same" << endl;
    else
        cout << "clazz1 and clazz3 is not the same" << endl;

    return 0;
}

Practice 3 (Mutability and Protection of member variables)

Type and try the following code. If you observe error/warning, discuss how it should be fixed.

#include <iostream>
#include <string>
using namespace std;

class MyClass {
    int num1;
    float num2;
    string name;
public:
    MyClass(int n1 = 0, float n2 = 0.0, string s = "unnamed") : num1(n1), num2(n2), name(s) {}
    MyClass(const MyClass &clazz) : num1(clazz.num1), num2(clazz.num2), name(clazz.name) {}
    void operator=(cost MyClass &clazz) {
        if (this == &clazz) 
            return;
        num1 = clazz.num1;
        num2 = clazz.num2;
        name = clazz.name;
    }

    void setName(string n) const {
         name = n;
    }

    bool operator==(const MyClass &clazz) const {
        return (num1 == clazz.num1 && num2 == clazz.num2 && name.compare(clazz.name) == 0);
    }
    void display() {
        cout << "num1 = " << num1 << ", "
             << "num2 = " << num2 << ", "
             << "name = " << name << endl;
    }
};

int main() {
    MyClass c1(2345, 3.4, "c1");
    MyClass c2(98776, 4.3, "c2");

    c1.display();
    c2.display();

    cout << "is c1 and c2 same? : " << (c1 == c2) << endl;

    c1.setName("myName is c1");
    c1.display();
    c2.display();

    return 0;
}

Practice 4

Complete the following code. If you observe error/warning, discuss how it should be fixed.

#include <iostream>
using namespace std;

// a class to store a dynamically generated char array.
class MyArray {
    char *ptr; // dynamically allocated char array to store a string.
    MyArray(){}
public:
    // constructor, that takes one arg.
    // *p is the string to be stored in the dynamically allocated char array
    MyArray(char *p) {
        /*
         *  Implement here
         */
    }

    // return the pointer to the dynamically allocated char array.
    char *getstr() {return ptr;}

    // substitute the specified MyArray object ra
    MyArray operator=(const MyArray &ra) {
        /*
         *  Implement here
         */
    }
    ~MyArray() {
        cout << "MyArray::Destructor : " << ptr << endl;
        delete[] ptr;
        ptr = NULL;
    }
};

int main() {
    MyArray mArray1("mArray1's string");
    MyArray mArray2("mArray2's string");

    cout << "mArray1's content = " << mArray1.getstr() << endl;
    cout << "mArray2's content = " << mArray2.getstr() << endl;

    cout << "mArray2's value after self copy: " << (myArray2 = myArray2).getstr() << endl;
    cout << "mArray1's value after copy from mArray2: " << (myArray1 = myArray2).getstr() << endl;
   
    return 0;
}
Sample Solution
Subpages (1): Sample Solutions
Comments