In C++ when you #include something, the included file is more or less "pasted" in the place of that directive.
There's a concept called translation unit which indicates a code unit which gets compiled and might, in the future, be referenced by other units (at linking time).
What you want is to write a translation unit and give other translation units the information on how to access that data. That is the purpose of a header file.
A .h file (.h by convention, you might call it whatever you like) is a set of declarations like
File s1class.h
class s1
{
private:
int gxd;
public:
int gx(int x,int y)
{
gxd = x;
return x;
}
};
and
app1.cpp:
#include"s1class.h"
//... use s1 class
app2.cpp:
#include"s1class.h"
//... use s1 class
Just notice one thing: you should never violate the One Definition Rule, you can declare as many times as you want something (as long as they're consistent declarations) but you CANNOT define more than once a symbol (e.g. a function). (small note: #pragma once directives and similar stuff can help you avoid re-declaring symbols more than once and make your life easier when including a header multiple times in multiple places).
So in the above code the "gx" function is defined into your header file.. that function should exist just once per translation unit where it is used.
In that particular case it is used to create the translation units for app1.cpp and app2.cpp. It should violate the ODR, right? It won't instead since a member function for a class is implicitly inlined, that means it is "stitched" into the caller's body thus being integrated part of the caller's identity.
The alternative is to just declare stuff in the header file and implement them in a related cpp file:
s1class.h
#pragma once
class s1
{
private:
int gxd;
public:
int gx(int x,int y);
};
s1class.cpp
#include "s1class.h"
int s1::gx(int x,int y)
{
gxd = x;
return x;
}
and proceed as above: by just including the header file in order to use it at link time (when the symbols are resolved).