Files: Header, Implementation, Test

  • Thread starter Thread starter Jammin_James
  • Start date Start date
  • Tags Tags
    files Test
Click For Summary
SUMMARY

This discussion focuses on the use of header, implementation, and test files in C++ programming, specifically regarding class declarations and definitions. The user inquires about the dual declaration of constructors in header and implementation files, which is clarified as a standard practice in C++. The distinction between declaration and definition is emphasized, with the constructor being a special method that shares the class name. The importance of header files for modular programming and code reusability is also highlighted.

PREREQUISITES
  • Understanding of C++ class structures
  • Familiarity with constructor and method definitions
  • Knowledge of header and implementation file usage in C++
  • Basic concepts of modular programming
NEXT STEPS
  • Study C++ class constructors and their syntax
  • Learn about header file best practices in C++
  • Explore modular programming techniques in C++
  • Investigate the role of test files in C++ development
USEFUL FOR

Students in computer science, C++ developers, and anyone looking to understand the structure and organization of C++ programs, particularly in relation to class implementation and modular design.

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.
 
Technology 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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
81
Views
8K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
35
Views
7K
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K