You don’t need to re-declare the class in the .cpp
file. You only need to implement its member functions:
#include "Mems.h"
#include <iostream> // only required for the std::cout, std::endl example
Mems::Mems() : n(42) // n initialized to 42
{
std::cout << "Mems default constructor, n = " << n << std::endl;
}
Note that usually you want the default constructor to be public
. Members are private
by default in C++ classes, and public
in structs.
class Mems
{
public:
Mems();
private:
int n;
};
solved Where and how to define member variables? In header or implementation file?