writing class and using header and files correctly

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?

}




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?


It appears that you've already moved the constructor from the definition so moving the rest of the functions would be similar.

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 :

This is really a design matter, but most of the time each class would be defined in it's own header file and implemented in it's own source file. Having the classes in one or multiple header files doesn't affect program performance, however compilation may be affected. The biggest reason to separate the classes is because it will be easier to maintain and share the code between multiple programs.

Gotya thank you jlb, sorry i am confused, so below i changed my code around so that the methods/functions are now on Log.cpp file, however on the main.cpp file, its not able to see the functions, also in the Log.cpp file, its not able to see any variables from the Log.H file, I am obviously doing something wrong :(

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//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?

}

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.h file below: 
#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;
	//}

};

//Main.cpp file:
// Tutorial_01.cpp : main project file.

#include <iostream>
#include "Log.h"

int main()
{
	Log l;
	//l.SetLevel(l.LogLevelError);
	
	l.Warn("Hello", l.LogLevelError);
	l.Info("Hello", l.LogLevelError);
	l.Error("Hello", l.LogLevelError);

	std::cin.get();
}


again thank you for taking the time to help me learn, as i am trying to piece this all together :(
You still need to define the functions inside the class definition, just like you did the constructor.

And you will also need to properly scope the functions inside the implementation file, also just like you did the constructor.

I got it now, big thanks :)
here is the code below for the example, let me know if there is anything i can do to make it better in terms of performance or practice please :)

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
//main.cpp file
#include <iostream>
#include "Log.h"

int main()
{
	Log l;
	l.SetLevel(l.LogLevelWarning);
	
	l.Warn("Hello");
	l.Info("Hello");
	l.Error("Hello");

	std::cin.get();
}

//Log.h file
class Log
{
private:
	int m_LogLevel = LogLevelInfo;
public:
	const int LogLevelError = 0;
	const int LogLevelWarning = 1;
	const int LogLevelInfo = 2;

	
	void SetLevel(int level)
	{
		m_LogLevel = level;
	}

	void Warn(const char * message);
	void Error(const char * message);
	void Info(const char * message);

};


//Log.cpp
#include <iostream>
#include "Log.h"

void Log::Warn(const char* message)
{
	if (m_LogLevel >= LogLevelWarning)
		std::cout << "[Warning] " << message << std::endl;
}

void Log::Info(const char* message)
{
	if (m_LogLevel >= LogLevelInfo)
		std::cout << "[Info] " << message << std::endl;
}

void Log::Error(const char* message)
{
	if (m_LogLevel >= LogLevelError)
		std::cout << "[Error] " << message << std::endl;
}

You may as well move SetLevel() to the implementation file as well.

let me know if there is anything i can do to make it better in terms of performance

While performance will be important later, right now concentrate on compiling properly and producing the correct values. For now learning how to use multiple files probably overrides any concern about performance.


By the way what happened to your constructor?

Last edited on
Gotya thanks, i will definitely remember that to keep the with the Log.cpp file, thanks, yes practice first to get things working then go back and optimize, make sense thanks :)

i removed it, didnt think it was important at the time, though i am going to learn constructors and destructors... maybe you can let me know about constructors, your thoughts of course the Log() in the class in file Log.h?

again big thanks jlb for your help :)
Topic archived. No new replies allowed.