سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top