0

How do I call functions defined outside a source file given you shouldn't use header files?

-- EDIT: generic(?) question below

So, first, a problem should be broken down into simpler subproblems. Then, each subproblem is solved by implementing new code for a subsolution or reusing an existing subsolution. If done correctly, then each subsolution exists on its own, uncoupled to any other subsolution and called by the interface it provides. That is how I understand modularity.

To solve the superproblem, the sum of all subproblems, one central logic, the supersolution, brings together all the subsolutions and coordinates every subsolution such that the superproblem is solved.

If the solution scheme above is flawed, please clarify.

Now, how do you accomplish coding the supersolution in c++? Particularly, how can you reuse a subsolution for a different superproblem?

+ Example:
superproblem_1: isolate dialog in a novel's text file
possible subproblem_1: how to get text between quotes?
possible subsolution_1: split(novelLine, '\"')

superproblem_2: isolate dialog in a list separated by commas
possible subproblem_2: how to get text between commas?
possible subsolution_2: split(dialogList, ',')

common subsolution: split(string, character)

So, when you break up both superproblems, a common subsolution exists. If I made the programs modular, I can extract that common subsolution out of both. How can I then use the subsolution in both superproblems given that I just extracted it?

closed as too broad by Neil, gnat, Bart van Ingen Schenau, Euphoric, Giorgio Mar 24 '14 at 18:44

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    #include "repo/subsolution.h", and build subsolution as a library – James Mar 24 '14 at 17:33
  • 1
    @user2738698 Good, you've filtered all possible programs into a slightly smaller subset containing only programs which solve problems. I still think it is a tad generic though. – Neil Mar 24 '14 at 17:42
  • 1
    Your question is a bit vague. Can you provide a specific example of a problem that you are contemplating making modular? – Robert Harvey Mar 24 '14 at 17:49
  • 2
    The header file issue is one reason why some folks dislike working in C and C++. However, given that your question asks about C++ and only C++, you are going to have use header files and libraries just as James suggested. That is the method in C++ for combining modules. – Charles E. Grant Mar 24 '14 at 17:52
  • 5
    The linked question up there doesn't suggest that you, as a developer, shouldn't be using header files. It suggests that header files are bad from a language design perspective. If you're working in C or C++, you're stuck with them, good language design or not. – KChaloux Mar 24 '14 at 19:30
3

If I can #include "your_library.h" and then type "ThatGuysModularClass obj;" and continue to do exactly what I want it to do, with a well documented API (i.e.: "this method does this, that one does that", etc.) you have successfully created something modular, in my opinion.

Example:

I need to calculate the area of a triangle. For this I will need a height, base, and another object I can use to represent "2" to do the division. This is because in some parallel universe the number "2" is impossible to represent as an integer (bare with me). Well, let us use John Doe's modular library Two.h.

#include "Two.h"
Two the2;

int base, height;
base = 5; height = 12;

// do some stuff
// problem solved

I can also use John's Two.h to solve another problem, like finding the circumference of a circle given its area, or whatever.

Two.h is said to be modular, because we can re-use it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.