4
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
class student
{
public:
student(std::string, int a_year);
~student();
int get_year() const;
void set_year(int);
private:
const std::string name;
int year;
};
#endif // STUDENT_H
student.cpp
student::~student()
{
std::cout << "entering dtor" << std::endl; // no meaningful behaviour yet…
}
You can wonder how the “destruction” of the student object s1 is done before we had a
destructor defined. Keep in mind that a C++ compiler does a lot more than what you can
perceive from a first look. Due to backward compatibility between classes and structs
(which already existed in C) the compiler creates for you automagically a number of
methods (sometimes called the canonical methods) if you do not declare them yourself:
• the default constructor (ctor without any parameter, if you declare a ctor with
parameters this one will not be created)
• the default dtor
• the copy constructor (to clone an object)
• the assignment operator