Understanding header files and .cpp files

Hi All,

I am literally just beginning learning C++, following a beginners 21 day tutorial... so have no one I can ask these questions - other than you all!

I'm learning about "class declaration and function definition"...

So far my .cpp files have contained the class declaration and the class method / function definition... now I'm splitting the class declaration into a header file and leaving the fuction definition in my .cpp file along with my main() function... This seems silly as each time I would ever want to use the header file and #include it in any new .cpp file I create, I would have to list all the function definitions again...

I was under the impression creating a header file was so others using the class (from the header file) wouldn't need to know how the functions work internally but could work out enough from the header file to realise what functions / methods a class has and is available for them to use... but if they then need to code all the function definitions into their .cpp file it seems pointless.

In the real programming world, would you create and declare a class in a header file and create only those function definitions in its related .cpp file ie the file wouldn't contain a main() function etc

I was wondering if I'm simply getting confused because all the .cpp files we create in the tutorial obviously start with main() to demonstrate the particular issue we are studying.
For example, if you had an Employee class - you could have three files.

employee.h - header file with class declarations
employee.cpp - cpp file with class function definitions (#include the header file)
main.cpp - cpp file with main program that uses employee class (again #include the header file)
That makes sense and was how I though it should work but tried it out and it caused linker problems...

I created class declaration and saved it as employee.h...

I #include the header file in my employee.cpp file and created a main() function to test it works OK, compiled it and it does...

Now if I create a main.cpp file and #include employee.h header file when I compile it it fails with linker error saying it cant find any of the definitions but if I change the reference to #include employee.cpp file it does work when I remove the main() function from the employee.cpp file but it comes up with an error if I leave the main() function in - I guess because there are now two main() funtions existing when I compile the main.cpp file (one from employee.cpp and one from main.cpp)...

But it I remove the main() function from employee.cpp so I can #include it in my main.cpp file then I cant complile the employee.cpp file as it doesn't contain a main() function now.

What am I overlooking or more to the point what am I not getting?

The main function is the entry point for the program, so you can't have more than one main function.

What IDE/compiler are you using to build these files?
I'm using Dev C++ simply as it was one I got when I acquired the "Teach yourself C++ in 21 days"

I have a trial version of Microsoft Visual Studio 2012 which I was going to use but found it too daunting to even try to understand what type of project or solution I was trying to create so I thought I'd leave it nice and simple till I learnt some of the basics before attempting to use Visual Studio.

I realise there are other free IDE compilers but am all new to C++ of late - I did have Borland C back in the day... well almost 18 years ago when I did a part time course in C and C++ but as you can see with the questions I've been asking not much stayed it the brain cells... LOL

What IDE/Compiler do you use or recommend?

Also, going back to your original reply, which made sense and was how I expected a proper program to be created... as you said there should only be one main() function... would you create a header file and #include it in its related cpp file (with a main() function) and would you compile that cpp file? Or would you leave the related cpp file without a main() function but not compile it?

Shall I attach a sample code version of what I'm trying to explain and if you didn't mind, you could point me where I'm going wrong...?
The cpp file related to the header file would not have a main function. That cpp file contains the definitions of the function belonging to the class defined in the h file. The class header and cpp files could be used in multiple programs, so you wouldn't want a main function in them. The main function will be in the program that uses the class.

Here's a short example of three files. I don't use Dev C++ so I'm not sure how the projects are set up there. In VS there are separate project folders for header files and C++ files and you build the whole project. It all has to compile as valid code.

date.h
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
#ifndef DATE_H
#define DATE_H

class Date
{
	public:
	
	//constructor with default values if not provided
	Date(int = 1, int = 2000); 
	
	//set functions
	void setMonth(int); //set month
	void setYear(int); //set year 
	
	//get functions
	int getMonth(); //get month
	int getYear(); //get year
	
	//member functions
	void displayDate();//display date in mm/dd/yyyy format
	
	private:
	
	int month;
	int year; 
};

#endif 


date.cpp
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
#include <iostream>
#include "date.h"

	Date::Date(int m, int y) //constructor with parameters
	{
		setMonth(m);
		setYear(y);
	}
	//set functions
	void Date::setMonth(int m) //set month, if input not 1-12, will set to 1
	{
		if (m >= 1 && m <= 12)
			month = m;
		else
			month = 1;
	}
	void Date::setYear(int y) //set year 
	{
		year = y;
	}
	//get functions
	int Date::getMonth() //get month
	{
		return month;
	}
	int Date::getYear() //get year
	{
		return year;
	}
	//member functions
	void Date::displayDate() //display date in mm/yyyy format
	{
		std::cout << month << "/" << year;
	}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "date.h"

int main()
{
	int month;
	int year;
	
	Date startDate;//create date object
	
	std::cout << "\nEnter the starting date" << endl;
	std::cout << "Month: ";
	std::cin >> month;
	std::cout << "Year: ";
	std::cin >> year;
	
	startDate.setMonth(month);
	startDate.setYear(year);
	
	startDate.displayDate();
	
	return 0;	
}
Thank you for that wildblue....

I didn't expect you to spend that much time on me... lol

but grateful you did! :)

The date.h header file is as expected :)

date.cpp file - I didn't include main either on my test file the other day but then the file couldn't be compiled - is that correct? As I am still learning I created a main() function and compiled it to check it did actually work then simply commented the main() function out so I left the file how you explained it to be.

on the main.cpp file - if I #include "date.h" my compiler stated a linker error as it couldn't find the function definitions so to get round this in the file I had to use #include "date.cpp" - that works but gets confusing as you tend to expect #include to use a header file not a cpp file.

And I think that is where I was getting confused...

The three files you shown above is how I expected a program to be done and makes perfect sense...

So do you think I need to use Visual Studio or find another free IDE/Compiler? And if you think I'd be better using VS from the outset would all the projects I create for the task of these tutorials be simple Win32 console applications?

You do realise you've set yourself up to be my online mentor... hahaha... only joking :)
Does your date.cpp file include the date.h file? You shouldn't have to include a cpp file.

Are you using the Orwell version of DevC++?
No I'm using the bloodshed software v 4.9.9.2

I was going to get the orwell version but some reviews were saying there's bugs / issues with it so I kept the old version I got a few years ago.

I didn't know if to use netbeans or look for other free ones so decided to stay with this...

I do have a trial copy of Visual Studio 2012 - would I be better biting the bullet with that??? I just steered clear of it because it didn't give me simple header / cpp files it bundled all the other bits n bobs it too which seemed to much while I was simply learning the basics.

What do you use????

I noticed you avoided the comment on being my mentor... hehehe
in Orwell DevC++, you would start by creating a new project, and choosing "Console Application". After creating the project, you then need to add the other .cpp file (date.cpp in this case) to the project. The IDE then knows which files belong to this project and knows how to build it.
There are lots of issues with v 4.9.9.2, it was last updated since 2005, and doesn't support recent C++ features. It is regarded as obsolete.

If you use either Orwell DevC++ or Code::Blocks you will have an up-to-date compiler and be able to use the C++11 features.
Thank you Chervil... will have a look at both of them... :)
Topic archived. No new replies allowed.