Hello everyone make sure to stop by and introduce yourself and do check out our design and programming sections.
Visit the Den and get some free stuff!

You are not connected. Please login or register

Understanding a header file

View previous topic View next topic Go down  Message [Page 1 of 1]

1Understanding a header file Empty Understanding a header file Tue Feb 17, 2015 3:18 pm

TheDeveloper

TheDeveloper

Moderator
Moderator
The ridiculousness of a header file is absolutely absurd to me. When I tried my first time I was getting some strange erroneous messages in my compiler, so I had to search for hours to figure out what I was doing wrong.

To understand what a header is we first need to understand C++ and how it is compiled. You can have 6 different source files for example

SOURCE TREE
SOURCES
-a.cpp
-b.cpp
-c.cpp
-d.cpp
-e.cpp
-f.cpp

They are all compiled in your program, yet a.cpp can not talk to b.cpp correspondingly, meaning without knowledge of each other they have no clue the other exists.

So why make multiple sources and headers?
The reason for multiple sources and headers is to make your code easily read and clean to other users. If you do not have an understand of comments and parameter comments then you should probably read up on this.

In laymen terms a.cpp will include b.cpp which b.cpp will include all other cpp files. And in turn we will also have a.cpp include a header file which has a different extension. This will sound familiar in a second.

If you know of JavaScript or any programming language and understand the concept of functions well by separating all the cpp files and headers we are essentially creating functions to clean our code up. Now I have never used 6 cpp files and a header, I have used 3 header files and two cpp files though.


So what are we really doing when we create a header file?

Essentially we are creating a nice object class to use inside our main cpp file. Which coincidentally will have a main function. Which is what are compiler looks for to run the program.

Let's think of a simple way to communicate between the program and screen, which usually we would use std::cout or std::cin which could get annoying especially if we have a lot to do and is repetitive.

First our header file

Code:

#include <iostream>
#include <string>
using namespace std;

class Easy {
   public:
      void ask(string question);
      string listen(string answer);
      int listen(int answer);
      char listen(char answer);
};

So we have two functions now called ask and listen. These are english laymen terms we can understand, ask is to ask a question or output some text to the user, and listen is to listen to what they are going to tell us. ask is a datatype void because we will not be returning anything to this, and listen is a datatype of string (which we can make multiples of these) because it'll return a string. We can also add the key inline to use these.

So now we need to actually tell what these functions do. So let's create a Easy.cpp file

Code:

#include <iostream>
#include <string>
#include "Easy.h"
using namespace std;

void Easy::ask(string question){
   cout << question << endl;
}


string Easy::listen(string answer){
   cin >> answer;
   return answer;
}

int Easy::listen(int answer){
   cin >> answer;
   return answer;
}

char Easy::listen(char answer){
   cin >> answer;
   return answer;
}

So now we need to get to our main.cpp and integrate these need functions to work together.

Code:
#include "Easy.h"//include our library

int main(){
   string name, age, mood; //creating some memory
   Easy easy; //making a namespace for our class Easy

   return 0;
}

Great now how to use?

Code:
#include "Easy.h"

int main(){
   string name, age, mood;
   Easy easy;

   easy.ask("How are you feeling today?");
   mood = easy.listen(mood);
   easy.ask("What is you name?");
   name = easy.listen(name);
   easy.ask(name + " ok how old are you?");
   age = easy.listen(age);
   easy.ask("Great " + name + " thanks for that information...");
   easy.ask("You are " + age + " and are feeling " + mood + " today");

   return 0;
}

Ask you can see it becomes just a little more like JavaScript using this method, though we can just create some cool functions! instead of using our header file. Which means we will have to delete or Easy.cpp and our Easy.h

Now the beginning of our main.cpp file

Code:
#include <iostream>
#include <string>
using namespace std;

We must now include these standard libraries inside main.cpp because we got rid of Easy.cpp and Easy.h

Code:
#include <iostream>
#include <string>
using namespace std;

//declaring the function prototypes
void ask(string question);
string listen(string answer);
int listen(int answer);
char listen(char answer);

Then we need to declare our prototypes above our int main
then we will create int main, then define our functions after that.

Like so:

Code:
#include <iostream>
#include <string>
using namespace std;

//declaring the function prototypes
void ask(string question);
string listen(string answer);
int listen(int answer);
char listen(char answer);

int main(){
   string name, age, mood;

   ask("How are you feeling today?");
   mood = listen(mood);
   ask("What is you name?");
   name = listen(name);
   ask(name + " ok how old are you?");
   age = listen(age);
   ask("Great " + name + " thanks for that information...");
   ask("You are " + age + " and are feeling " + mood + " today");

   return 0;
}


//defining the functions
void ask(string question){
   cout << question << endl;
}


string listen(string answer){
   cin >> answer;
   return answer;
}

int listen(int answer){
   cin >> answer;
   return answer;
}

char listen(char answer){
   cin >> answer;
   return answer;
}

And wala, we have our same functionality above in one page, and also separated! If you have a question please ask and don't be afraid. I'm not going to act like I'm a pure genius in C++ because I am still very much learning this language and I just wanted to share my knowledge.



Last edited by TheDeveloper on Fri Feb 27, 2015 3:52 pm; edited 1 time in total

2Understanding a header file Empty Re: Understanding a header file Fri Feb 20, 2015 1:57 pm

george

george

Admin
Admin
Okay I have never learned C++ but I am trying to understand it. I know in the beginning you are calling your data to be sorted and I'm not sure about what IMEI is but I might kinda know from what you wrote above.

http://gareygraphics.com

3Understanding a header file Empty Re: Understanding a header file Fri Feb 20, 2015 9:54 pm

TheDeveloper

TheDeveloper

Moderator
Moderator
Yeah I guess I should have made it more oblique on what a header file is lol. Because I just went on a rant with my code. IMEI is a code for cell phones, it tells if it is locked or not. This code test if it is a valid IMEI or not.

4Understanding a header file Empty Re: Understanding a header file Sat Feb 21, 2015 5:36 pm

george

george

Admin
Admin
Oh I see. I am going to try and get familiar with this eventually. Wink

http://gareygraphics.com

5Understanding a header file Empty Re: Understanding a header file Fri Feb 27, 2015 3:53 pm

TheDeveloper

TheDeveloper

Moderator
Moderator
I've updated the original with a better one.

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Share this topic!

URL Direct
BBcode
HTML
Understanding a header file

Permissions in this forum:
You cannot reply to topics in this forum