4 Replies - 268 Views - Last Post: 03 January 2013 - 08:11 AM Rate Topic: -----

#1 arcsss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-January 13

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

Posted 03 January 2013 - 03:06 AM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Should I try to work around having to use header files?

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 183
  • View blog
  • Posts: 615
  • Joined: 11-June 11

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

Posted 03 January 2013 - 04:02 AM

In C++, we use '.' operator to select members of an object of some struct or class type. And when that object is a pointer type, '->' is used instead... The case that you didn't find '->' operator in Java is because it doesn't have pointers...

*this is a hidden pointer inside every class member function which points to the object which called the member function... This is how C++ keeps track of which object is working with the member function...

The case that we keep class declaration in separate file and its implementation in another, and both of them in different than the main or any other is for the sake of simplicity and reusability... So we can make changes easily to our class and modify it and in future if we need to use the same class in another program then we can easily integrate it into it.

However, you can have all the stuff in one file, say in main.cpp, you can throw in your class implementation your other code and everything thing else; it will work just fine... But it will be difficult to manage and modify later...

Even you can have the implementation of the class in the header file too, say in MyClass.h, include it in main file and it will work just fine(but it is not a good programming practice)...

The rule of thumb is to have the declaration of the class in the header file, its implementation in source file .cpp and include it in your main file...

Example:
MyClass.h
#ifndef MyClass_H
#define MyClass_H

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

#endif



MyClass.cpp
#include "MyClass.h"

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



main.cpp
#include <iostream>
#include "MyClass.h"

int main() {
    MyClass myclass(4);

    std::cout << myclass.getData() << std::endl;
    myclass.setData(6);
    std::cout << myclass.getData() << std::endl;

    return 0;
}


Building from and IDE is simple. But from command line, you have to compile both the files:
e.g.
g++ main.cpp myclass.cpp



Coming to your code, you have a conflict with argument lists in your header file and .cpp:
5	    void setData();
6	    classType();



int data
04	void classType::setData(int data){
07	classType::classType(int data)



And, Unlike Java, class ends with a semi-colon ; on the closing brace:
class MyClass {
   ...
};    // <- semi-colon here



PS: What is the linker error you are talking about?

This post has been edited by AKMafia001: 03 January 2013 - 04:05 AM

Was This Post Helpful? 1
  • +
  • -

#3 arcsss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-January 13

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

Posted 03 January 2013 - 04:55 AM

I know about this; I was just saying that although the third piece of code is structured more like a Java file's than the .cpp and .h combination, the code is still C++.

My main question is that although implementation is supposed to be separate from declaration, is there anything wrong with my coding the implementation and declaration together as in the third piece of code and then running a program to separate the two into .cpp and .h files, in effect separating the implementation from the declaration?

Whoops. I forgot to put that 'int data' in the declaration. And I always forget that semicolon on a class's closing brace.

I forget what error it was; I was using VS2012 when I compiled a program that had a .cpp file and another .cpp file formatted like the third's (implementation and declaration in the same file). At the top of one .cpp file, I #included the other .cpp file (there were no .h files); I got a linker error when I tried to build the project.
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 183
  • View blog
  • Posts: 615
  • Joined: 11-June 11

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

Posted 03 January 2013 - 06:29 AM

You cannot include .cpp files with #include, they should be header files with .h extension(maybe .hpp or .hxx too)...

But, if you want to use a function from another file, say print.cpp, you can just put a declaration of it in your file(so the compiler would know that this function exists somewhere later) and call it...

e.g.
print.cpp
#include <iostream>

void printHello() {
	std::cout << "Hello, World!";
}



main.cpp
void printHello();

int main() {
	printHello();
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,750
  • Joined: 25-December 09

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

Posted 03 January 2013 - 08:11 AM

Quote

You cannot include .cpp files with #include, they should be header files with .h extension(maybe .hpp or .hxx too)...

Actually you can include any text file with the #include preprocessor directive. You shouldn't but you can.

Quote

My main question is that although implementation is supposed to be separate from declaration, is there anything wrong with my coding the implementation and declaration together as in the third piece of code and then running a program to separate the two into .cpp and .h files, in effect separating the implementation from the declaration?

If you are going to try to learn C/C++ then stick with the C/C++ way of doing things. C/C++ is quite different from Java, so get used to this fact.

There are many reasons for keeping the definition separate from the implementation in C/C++ programs. One of the reasons to keep the definition separate from the implementation is that when using functions that are defined in header files you don't need to know, or usually even care, how these functions are actually implemented. All you really care about is that if you call the function with the proper parameters the function will produce a specific output.

Now after saying this there are times in C++ where you will actually keep both the definition and implementation in the same compilation unit, templates for example. However when ever you can you should strive to keep the definition separate from the implementation.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1