Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Should I try to work around having to use header files?

$
0
0
Hi; as I understand it, I require a .cpp and a .h file whenever I intend to #include it, with the .h file being like this:

class classType{
	int data;
public:
	int getData();
	void setData();
	classType();
}



And the .cpp file being like this:

int classType::getData(){
	return data;
}
void classType::setData(int data){
	this->data = data;
}
classType::classType(int data)
	:data(data)
{
}



The problem is, this seems like an awful amount of extra work to go through having to copy/paste lots of text around, and where I can't see how the class is structured and how the member functions are implemented at the same time without jumping back and forth between the two files. Perhaps I'm spoiled by Java, in which the format would be something like (with this. in place of this-> and other small differences):

class classType{
	int data;
public:
	int getData(){
		return data;
	}
	void setData(int data){
		this->data = data;
	}
	classType(int data)
		:data(data)
	{
	}
}



And that would be all I would need to do to include this code as well as to implement it. The thing is, I'm not sure exactly why I have to use header as well as source code files in C++. I understand why in that #including definitions results in two copies of the same definition, which gives me a linker error, but I'm not sure why I have to go through the trouble and make an extra file. I'm thinking about writing a program that will take the third file and build the .cpp and .h files from it, since it seems to be very formulaic copy/paste grunt work if I start from that file. Is there any software development reason why I ought not to do this or think this way?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>