Exercise: Transform struct User
Into A Class¶
Description¶
The files user.h
(download
) and
user.cpp
(download
) contain
declaration and definition of struct User
: the structure
definition, and the implementation of the constructor. The structure
is written in C, in an object oriented style. Transform that
struct
into an equivalent C++ class
.
Identify opportunities for C++ transformation
C++ brings
std::string
. See here for documentation.The C function
user_init()
looks like a constructor: it initializes an object ofstruct User
.
Copy both files into your work directory for transformation.
Use the following main program for testing (you are free to modify according to you own testing wishes).
#include "user.h" #include <iostream> using namespace std; int main() { User joerg("Joerg Trittsicher", "Faschingbauer", 55); cout << "Firstname: " << joerg.firstname << endl; cout << "Lastname: " << joerg.lastname << endl; cout << "Age: " << joerg.age << endl; return 0; }
In your work directory’s
CMakeLists.txt
, add the following to build programuser-main
:ADD_EXECUTABLE(user-main user-main.cpp user.cpp)