I am not sure if this is what you are asking but classes do not need to be global you can just use them wherever you need them. As for the member functions that you can access using the :: operator that is just there more or less for readablity. This is how I handle classes.
fakeclass.h
Code:
//header guards
#ifndef fakeclass_h
#define fakeclass_h
class fake
{
public:
void setxValue(int value);
void setyValue(int value);
void printxyValues();
int getX();
int getY();
private:
int x;
int y;
};
#endif//fakeclass_h
fakeclass.cpp
Imagine putting all of this into the above class it would get pretty ugly even with this basic class.
Code:
#include <iostream>
#include "fakeclass.h"
int fake::getX()
{
return x;
}
int fake::getY()
{
return y;
}
void fake::printxyValues()
{
std::cout<<"x value equals: "<< x <<std::endl;
std::cout<<"y value equals: "<< y <<std::endl;
}
void fake::setxValue(int value)
{
x = value;
}
void fake::setyValue(int value)
{
y = value;
}
As for a class needing to be global the way you have to think about it is that is just a new datatype just like int, float,... they exist but unless you create one globally its only useable in the function you declare it in. say you might need a(for this case) fakeclass but only need it in fakeFunction not in main you just include fakeclass.h and then you can declare an instance of it.
main.cpp
Code:
#include <iostream>
#include "fakeclass.h"
void fakeFunction()
{
//declare an instance of fake called random
//that is local to fakeFunction
fake random;
random.setxValue(10);
random.setyValue(20);
std::cout<<"random's x value: "<<random.getX()<<std::endl;
std::cout<<"random's y value: "<<random.getY()<<std::endl;
random.printxyValues();
}
int main(void)
{
//main has no knowledge of random
//but you could still use a different instance of fake
fakeFunction();
return 0;
}
Then ofcourse you could put all of this in on file but this is a simple class and even now it wouldn't be very "clean" looking. I hope this is what you were asking.