C++ for beginners, part 8a (finishing the project)

Well, we’ve got to the point where we can now make a multi-function program, with a rudimentary interface! I tried it earlier, and it was surprisingly easy, so I think I can walk y’all through it in one post. I guess that analogy to The Wire back in part 1 was more accurate than I thought. After 7 installments of meticulous plotting and details, the season finale falls into place with ease and inevitability. Plus, they had a character called Herc too.

There’s already a fair bit of code.

Pic1Top left is the function we wrote (here) that will give you the choice of repeating the function until you’re bored. The other three are the three different functions/methods. What would be nifty is if we could have the different methods in one script, and the interface in another.

All we’ve done so far is write source code, as solitary .cpp files, then compiled, built and ran them. If we wanna have different functions and components in different files, we need to link them properly. It is easier to actually do than explain, so here’s an absurdly simple example:

Project structure

  • Go to File, New, Project… Console application. You’ll see the following (and if you don’t, click View, Manager to see the active projects):

Pic2

  • In code::blocks, ‘main’ already contains the hello world program. If not you can always add one (which we’ll need to do at least once anyway. We want one source file for the interface and one for the functions). Go to File, New, File… Console application. Give it a name and add it to the active project, with both debuggers checked (you’ll be prompted for that). If there’s any confusion, this will help. Now we have this:

Pic3

  • (You can rearrange the layout of source files by dragging the tabs). Anyway. we’ll make ‘function’ have a stupidly simple function:

Pic4

  • Yep. Babytown frolics etc. A couple of important points to note:
  1. Like we said, you should only define ‘main’ once in a program. So our function gets a different name.
  2. The parameter types need to be defined explicitly for each one. Within a piece of code, you can use ‘int a, b;’ and it will make both of them of type ‘int’, but in defining a function, they should be explicit. Here ‘a’ and ‘b’ are the parameters. When they get called using specific values, they’re called arguments.
  3. We use return instead of cout. This is because basicfunction will be called from ‘main’, so we want basicfunction to return the answer one level up, to main, and then main can output it to the user. (Since we don’t use cout in this file, we don’t actually need that #include <iostream> file here).
  • As for the main body of the code, equally simple:

Pic5

  • We say that the parameters a and b specifically take the arguments of 3 and 7. Then we call basicfunction on them. That’s it. Let’s compile:

Pic6

  • Not declared? Heck. We need a header.

Header files and header lines

When you write large programs, with components in different files, you may have components which are interdependent. So how does the compiler know in what order to compile without hitting a wall? You use header files. While the source (.cpp) files contain definitions of functions (what they do), headers contain declarations, which in this case, states ‘basicfunction is a name of something, which takes this type of parameter’. It’s kinda like a placeholder, saying ‘hey, compiler, i know you don’t know what this does yet, but just have faith and keep compiling’. It’s tricky to explain, but more info here if necessary.

So go to File, New, File… C/C++ header. Add to active project and give it the same name (just to keep track of it easier) as the .cpp containing the function you wanna declare. It will be saved as a .h file. You’ll get this:

Pic7

  • Thankfully, Code::Blocks automatically creates a ‘header guard’. This protects against adding the same code repeatedly when things are interdependent or different components require the same headers.
  • The header guard basically says ‘if this header hasn’t already been included, here’s the declaration, [your declaration]’. If the header already has been included, then it won’t repeat itself! So here’s our declaration:

Pic8

  • Then all we need to do is #include this in ‘main’. You include header files you created yourself with quote marks, not chevrons. Then compile and run. Success!

Pic9

Our finished functions and header

  • We want the main body as .cpp, the functions as a .cpp, and a header file for including the functions. We’ll start with the functions, compared to the original:
  • Pic10I’ve collapsed two of the functions so it doesn’t look messy, but you get the idea. We’ve given each function it’s own name instead of ‘main’. [That one is called ‘gauss’ because the triangle trick is attributed to Gauss]. We’ve taken out the declarations of n1, n2, thresh from inside, and put them as parameters for each function. Since it’s ‘return’ instead of ‘cout’, we don’t need to #include <iostream>. That’s it.
  • Our header. Piece of cake:

Pic11

Our finished interactive program

  • Right, we already wrote something in part 3 which we can cannibalize for this. It will offer the different methods as different options to choose from. The user chooses one, then inputs two factors and an upperbound. The program returns an answer, and asks if the user wants to run a function again. If ‘y’, they can choose a function etc, if ‘n’, the program ends. If it’s not ‘n’ or ‘y’, the program gives the benefit of the doubt and assumes ‘y’.
  • So –

Cliffhanger! I’ll put that all in part 8b so that this post isn’t so overwhelming and it all stays manageable. It’s really easy. So, this isn’t quite like The Wire. I guess we’re going extended season finale, like Battlestar Galactica, or The Shield. Which, you know, isn’t bad company.

‘Till next time!

bbend

Advertisements
This entry was posted in C++, Problem 1 and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s