Goals
- Learn basic C++ syntax.
- Get familiar with object oriented programming.
- Get familiar with generic programming.
Exercises
Download the files for Lab 9 first.
Exercise 1: C++ warm up
C++ syntax is very similar to C. Almost every C program can compile under C++ compiler. This exercise helps you walk through basis of compiling a C++ program.
First compile hello.cpp using:
$ g++ -Wall -Wextra hello.cpp -o helloIt is always a good practice to compile C/C++ code with -Wall -Wextra flag.
Check-off
Show your TA that you are able to compile and run the code.
Exercise 2: Object Oriented Programming
One of the features that distinguishes C++ from C is classes. In this exercise, we are going to write a simple class.
Check-off
Finish the implementation of class Int in object.cpp, which should have:
- A default constructor that set value to 0.
- A constructor that initializes value using an integer.
- A destructor that outputs value upon destruction.
- A getter for private member value.
- Overloading self-increment operator.
Exercise 3: Template Metaprogramming
We can use macro in C to write generic functions. However, macros are very horrible due to its implementation. In C++, we can use templates to solve this problem. In this exercise, you should implement generic functions using templates.
Check-off
Finish the implementation of mul and max in template.cpp. Show TA your program's result.