I'm going through exercises of a C++ book. For each exercise I want to minimize the boilerplate code I have to write. I've set up my project a certain way but it doesn't seem right, and requires too many changes.
Right now I have a single main.cpp
file with the following:
#include "e0614.h"
int main()
{
E0614 ex;
ex.solve();
}
Each time I create a new class from an exercise, I have to come and modify this file to change the name of the included header as well as the class i'm instantiating.
So my questions are:
- Can I include all headers in the directory so at least I don't have to change the
#include
line? - Better yet, can I rewrite my solution so that I don't even have to touch
main.cpp
, without having one file with all the code for every exercise in it?
Update:
I ended up following Poita_'s advice to generate main.cpp via a script.
Since I'm using an IDE (Visual Studio), I wanted this integrated with it, so did a bit of research on how. For those interested in how, read on (it was fairly, but not entirely, straightforward).
Visual Studio lets you use an external tool via the Tools -> External Tools menu, and contains a bunch of pre-defined variables, such as $(ItemFileName
), which can be passed on to the tool. So in this instance I used a simple batch file, and it gets passed the name of the currently selected file in Visual Studio.
To add that tool to the toolbar, right click on the toolbar, select Customize -> Commands -> Tools, and select the "External Command X" and drag it to the toolbar. Substitute X with the number corresponding to the tool you created. My installation contained 5 default pre-existing tools listed in Tools -> External Tools, so the one I created was tool number 6. You have to figure out this number as it is not shown. You can then assign an icon to the shortcut (it's the BuildMain command shown below):
alt text http://img691.imageshack.us/img691/4853/capturerg.png
Replace 9999 with the required number. There might be better ways.
You could use conditional compilation for the class name by using concatenation.
Don't know about the includes though. As suggested above, a script might be the best option.
Don't use one main.cpp which you modify for each exercise. This solution makes use of make's builtin rules, so you only have to type
make e0614
and it will generate e0614.cpp, compile, and link it. You can customize each .cpp file (they won't be regenerated as written below) and maintain all of that history to refer to as you complete exercises, rather than erasing it as you move from one to the next. (You should also use source control, such as Mercurial.)Makefile
You can generate boilerplate code with scripts, because you don't want it to be tedious either. There are several options for these scripts—and I use a variety of languages including C++, Python, and shell—but the Python below is short and should be simple and clear enough here.
Sample generate script
Make a master include file containing the names of all the headers you want.
It's a really bad idea to include *, even if you could.
try this:-
later include this in ur main program that is cpp file
...your program
Why not using object mechanisms ?
You can use an Exemplar strategy for this.
Okay, that's the base.
And for main
And here, you don't need any script ;)