Hello,
in my pursuit to be a great c++ programming :) i am learning now classes, files, files such as cpp and header files, i read online
to help me, and doing an example myself, i am trying to get the functions onto the other cpp file called Log, and the header file called log, and the main cpp file called... Main. so my 2 questions are:
1. how do i get the functions/methods onto the log.cpp from the Log.header file, reading the documentation, it says its best to put the functions on the Log.cpp, but declaring and putting the variables on the header, is this correct, if so how?
2. if i had like, 5-10 classes, do i put them all onto 1 header file? what would be good practice that you have seen or yourself has done in a job environment plus for performance? big thanks for taking the time to read and answer :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
//main cpp file:
#include <iostream>
#include "Log.h"
int main()
{
Log l;
l.SetLevel(l.LogLevelError);
l.Warn("Hello");
l.Info("Hello");
l.Error("Hello");
std::cin.get();
}
//Log.h file
#pragma once
class Log
{
private:
int m_LogLevel = LogLevelInfo;
public:
const int LogLevelError = 0;
const int LogLevelWarning = 1;
const int LogLevelInfo = 2;
Log(); //constraint
void SetLevel(int level)
{
m_LogLevel = level;
}
void Warn(const char* message)
{
if (m_LogLevel >= LogLevelWarning)
std::cout << "[Warning] " << message << std::endl;
}
void Info(const char* message)
{
if (m_LogLevel >= LogLevelInfo)
std::cout << "[Info] " << message << std::endl;
}
void Error(const char* message)
{
if (m_LogLevel >= LogLevelError)
std::cout << "[Error] " << message << std::endl;
}
};
//Log.cpp file
#include <iostream>
#include "Log.h"
Log::Log()
{
//std::cout << "Log Created" << std::endl;
// shouldnt the Warn, Info and Error functions go here?
}
|