Files: Header, Implementation, Test

  • Thread starter Thread starter Jammin_James
  • Start date Start date
  • Tags Tags
    files Test
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
Jammin_James
Messages
49
Reaction score
0
Hey all!

I've been reading through my book trying to figure all this out for my Data Structures class and I'm typing this out while I take a break from reading the book.

I'm looking over the examples from class and my teacher has header, implementation and test files. There is no input from a user that I can see in these programs, which is where these test files come in. Also, I think I understand why people use header and imp files, but I'm having troubles understanding how aurguments are passed between them. I'm also having trouble with the declarations in header and implementation files.

IN HEADER:

class Date
{
Date(arguments)
}

IN IMPLEMENTATION FILE:

Date::Date(arguments)

Is my teacher making the function declaration twice?

I could link the code if you'd like because I really doubt any of this made sense, haha.
 
Physics news on Phys.org
Jammin_James said:
Hey all!

I've been reading through my book trying to figure all this out for my Data Structures class and I'm typing this out while I take a break from reading the book.

I'm looking over the examples from class and my teacher has header, implementation and test files. There is no input from a user that I can see in these programs, which is where these test files come in. Also, I think I understand why people use header and imp files, but I'm having troubles understanding how aurguments are passed between them. I'm also having trouble with the declarations in header and implementation files.

IN HEADER:

class Date
{
Date(arguments)
}

IN IMPLEMENTATION FILE:

Date::Date(arguments)

Is my teacher making the function declaration twice?

I could link the code if you'd like because I really doubt any of this made sense, haha.

Yeah putting the actual code is better than things out of context.


But you are missing the basics with this one.


The declaration is

class Date
{
Date(args);
myMethod(args);
}

That is the declaration. And the fact that the function and the class share the same name, means it is a special type of method: a constructor


Now you still need to define the function ( you can do it in the declaration, but let's keep it like your code ).

so in a cpp file you need to define it.

the syntax

Date::Date(args)
{
//some code
}

is the definition.

Data::myMethod(args)
{
// some code
}

and that defines my new method
 
Now let me explain your other issue.

If you are creating a simple program and you want to define a few classes for your program. You can do it with one file.

But the reason for header files is so you can create the declarations so that you can implement code in modules.

You build classes, such as your date class. And all you have to do is include the header file any source file, and that source file will see the definition of the Date class.

And when you build your program, you just need to make sure you have the definition source file, ( or object or library file.. ) and you have a Date class that can be reused over and over again.

That is how the standard libraries work. You get the header files and include them, and somewhere in library files are the definitions.