Difficulty understanding header files

Difficulty understanding header files

Post by Deddalt on Tue Dec 29, 2009 1:41 am
([msg=32239]see Difficulty understanding header files[/msg])

Before I partially understood wrote:Basically, in this book I'm reading (Accelerated C++ 2000), the author has thrown in headers and hasn't thoroughly explained the logic behind them. For example:

Code: Select all
#ifndef GUARD_Student_info
#define GUARD_Student_info
// Student_info.h header file
#include <iostream>
#include <string>
#include <vector>
struct Student_info {
      std::string name;
      double midterm, final;
      std::vector<double> homework;
};
bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
#endif


I understand that once you use: #include <"Student_info.h"> that you can call read_hw(); or read(); but I don't understand how anything is supposed to happen with those functions after they are called, as there is no code with those functions. Could someone explain how this is supposed to make sense?


EDIT:

I think I may have just realized how it works. In this book, he goes on to say that the function has to be defined elsewhere. What I'm wondering is if you can define the functions in the same header file. For example:

Code: Select all
#ifndef GUARD_Student_info
#define GUARD_Student_info
// Student_info.h header file
#include <iostream>
#include <string>
#include <vector>
struct Student_info {
      std::string name;
      double midterm, final;
      std::vector<double> homework;
};
bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);

bool compare(const Student_info& x, const Student_info& y)
{
  return x.name < y.name;
}
std::istream& read(std::istream& is, Student_info& s)
{
    // read and store the student's name and midterm and final exam grades
    is >> s.name >> s.midterm >> s.final;
    read_hw(is, s.homework); // read and store all the student's homework grades
    return is;
}
// read homework grades from an input stream into a `vector'
std::istream& read_hw(std::istream& in, std::vector<double>& hw)
{
    if (in) {
        // get rid of previous contents
        hw.clear();
        // read homework grades
        double x;
        while (in >> x)
            hw.push_back(x);
        // clear the stream so that input will work for the next student
        in.clear();
    }
    return in;
}
#endif


If anyone can help me out on this, I'd be most appreciative :)
Deddalt
New User
New User
 
Posts: 5
Joined: Mon Dec 28, 2009 3:30 am
Blog: View Blog (0)


Re: Difficulty understanding header files

Post by BhaaL on Tue Dec 29, 2009 6:20 am
([msg=32246]see Re: Difficulty understanding header files[/msg])

Those forward declarations (also called prototypes) only define how the function is going to look like.
While compiling, the compiler of choice does a top-down parse and throws if it doesnt know functions:
Code: Select all
void a() { b(); } //will fail, b is not known
void b() { a(); } //a was defined before b, so you can call it

Using prototypes, you can say "theres a function somewhere that looks like this" and satisfy the compiler.

Upon linking, the linker checks if all references are actually there and found.
One point of why this is separated is also the use of libraries. You possibly wont get the source for the library, but you'll get the header file - that way you know "theres a function somewhere that looks like this" (uh, de-ja-vu?)

Theres no problem to have the function in the same file along with the prototype (thats infact how you do smaller, on-file programs that have to cross-reference functions that might depend on each other).
It makes most sense for C++ template methods (and classes), since they have to be known. But everything else the developer/user does not need to know can be left in the source itself.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Difficulty understanding header files

Post by Deddalt on Tue Dec 29, 2009 7:41 am
([msg=32248]see Re: Difficulty understanding header files[/msg])

Thanks :]

One more question...

Can I use prototypes in any .cpp file so I don't have to order my functions in any particular order?
Deddalt
New User
New User
 
Posts: 5
Joined: Mon Dec 28, 2009 3:30 am
Blog: View Blog (0)


Re: Difficulty understanding header files

Post by BhaaL on Wed Dec 30, 2009 5:45 am
([msg=32331]see Re: Difficulty understanding header files[/msg])

Yes, you can. Infact, you might have to for cases like my a/b example.
You only put them into header files to have better separation, and allow stuff to be distributed as sort-of API (you know the functions, but you dont have to know the code).
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Difficulty understanding header files

Post by apples on Sat Feb 13, 2010 12:23 am
([msg=34970]see Re: Difficulty understanding header files[/msg])

nice book choice, by the way. it's my favorite introductory C++ text.
User avatar
apples
New User
New User
 
Posts: 37
Joined: Sat Apr 12, 2008 8:30 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests