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

Lesson 3

Practice 1

Create a MyTime class, which has "hour", "minute" and "second" as member variables. You can design your own constructors and member functions.

Practice 2

Run the following code to use MyTime class from above. Modify the code if it's necessary to use your MyTime class

#include <iostream>
#include "MyTime.h"

using namespace std;

int main() {
    MyTime a(9, 30, 35);
    MyTime b = a;
    
    cout << "a = " << a.getHour() << ": " << a.getMinute() << ": " << a.getSecond() << endl;
    cout << "b = " << b.getHour() << ": " << b.getMinute() << ": " << b.getSecond() << endl;
}

Discuss what's happening at MyTime b = a. Add another constructor to your MyTime class to achieve the same behaviour as MyTime b = a

Practice 3

Run the following code to use MyTime class from above. Modify the code if it's necessary to use your MyTime class

#include <iostream>
#include "MyTime.h"

using namespace std;

int main() {
    MyTime c = MyTime(9, 30, 35);
    
    cout << "c = " << c.getHour() << ": " << c.getMinute() << ": " << c.getSecond() << endl;
}

Discuss what's happening at MyTime c = Time(9, 30, 35) by comparing it toMyTime a(9, 30, 35) and MyTime b = a. How many instances of MyTime are created until the main() completes? Confirm your prediction using the constructor you created in Practice 2.

Practice 4

A usual standalone desktop application, it is quite easy to debug the code by simply execute each statement one by one. However, in some cases (such as an application communicating to other entities through networks), you won't be able to step into execution of some statements executed elsewhere. In those cased what you can do would be to write log at the beginning and end of function call. For instance, when you try to read something from a file in C:

int read_from_file(const char *filename) {
    FILE *fp;
    char line[512];

    printf("read_from_file(%s) started.\n", filename);  // 1) step in

    /* read from a file and do something */

    printf("read_from_file(%s) ended.\n", filename);    // 2) step out
    return 0;
}

Your task here is to create a FunctionStepIOLog class,

  • which can be instantiated at the beginning of a function;
  • whose constructor takes the name of the function as an argument;
  • which prints out the message similar to 1) and 2) in the above code (just after stepping into a function and just before stepping out from the function).

Practice 5 (adv)

Use the similar mechanism as above, create a class (AutoFreePtr) which automatically frees an alloced char array.

class AutoFreePtr {
    char *ptr;   // a pointer to an allocated char array
public:
    // you need implement here
}

You can use this class to do something like:

void removeSpace(char *str) {   
   int n = strlen(str);
   AutoFreePtr tmp = new char[n+1];
   strcpy(tmp, str);
   int i, j;
   for (i = j = 0; i < n; i++) 
       if (str[i] != ' ')
           tmp[j++] = str[i];
   tmp[j] = '\0';
   strcpy(str, tmp);
}

Practice 6 (adv)

Extend the above AutoFreePtr class to support an array of other primitive data types. You might not be able to fully support the array operation like in Prac 5 (discuss why).


Sample Solutions

Subpages (1): sample solutions
Comments