Main
Lab 6
Objectives
Practicing C++ classes: constructors, destructors; Makefiles
Create C++ Class Student
, that encapsulates student information, such as firstName
(string), lastName
(string), age
(int), etc (feel free to add more data members, e.g., a std::map containing classes and grades).
When writing your C++ class, please consider the following requirements:
- Implement the Class in separate
.hpp
(declaring) and.cpp
(function implementation) files. - Make all the data members
private
. Write public “get” and “set” functions for accessing and changing the data (e.g.,bool SetAge(int)
). Add some argument checking in the “set” functions. For example, theage
cannot be negative. The return type shows if the “set” has been successful. The return type of get functions is the type of the corresponding data member. - Use the STL class
string
(#include <string>
) for string types. Note that it is different from the C strings header. (C++ string vs. C char array: http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html ) - Write constructors and destructor for the
Student
class. Implement at least four constructors: i) Default constructor; ii) Copy constructor; iii) Move constructor iv) A constructor, which initializes the data members with input arguments. In the constructor and destructor print a debugging message withcout
that shows the function call. For instance:
“Student object constructor: < firstName >< lastName >”, OR “Student object default constructor”, “Student object destructor: < firstName >< lastName >”.
- Overload the << operator to print the student information (see example).
- Write a
main
file that declares instances of the Student class and test all the functions. Write a Makefile that compiles and links all the files and creates the binary executable. - In your
main
program, also create student directory, which is a container of Student objects. Optionally experiment with using smart pointers (unique or shared) and a container type such as vector or map.