NOTE : -
THIS CONTENT IS NOT ORIGINAL
20. The method bodies can be defined below the class definition (and Makefile usage example)
If a method cannot be inline, or you do not want it to be inline, or if you want the class definition to contain the minimum amount of information (or you simply want the usual separate .h header file and .cpp source code file), then you need only put the prototype of the method inside the class and define the method below the class (or in a separate .cpp source file):
For beginners:
If you intend to develop a serious C or C++ program, you need to separate the source code into .h header files and .cpp source files. This is a short example of how it is done. The program above is split into three files :
A header file vector.h:
A source file vector.cpp:
And another source file main.cpp:
Assuming vector.cpp is perfect, you compile it once and for all into a .o "object file". The command below produces that object code file, called vector.o:
g++ -c vector.cpp
Each time you modify the main.cpp source file, you compile it into an executable file, say test20. You tell the compiler explicitly that it has to link the vector.oobject file into the final test20 executable:
g++ main.cpp vector.o -o test20
Run the executable this way:
./test20
This has several advantages:
g++ -c main.cpp
g++ main.o vector.o test20
This wanders away from the "differences between C and C++" topic but if you want to look like a real programmer, you need to condense the above commands into a Makefile and compile using the make command. The file content beneath is an oversimplified version of such a Makefile. Copy it to a file named Makefile. Please note, and this is very important, that the space before the g++ commands is mandatory and that it is a Tab character. Do not type the spacebar here. Instead use the tabulation key (full left of your keyboard, above the caps lock).
In order to use that Makefile to compile, type:
make test20
The make command will parse through the file Makefile and figure out what it has to do. To start with, it's told that test20 depends on main.o and vector.o. So it will automatically launch "make main.o" and "make vector.o". Then it will check if test20 already exists and checks the date stamps of test20, main.o andvector.o. If test20 already exists and main.o and vector.o have a date stamp earlier than test20, the make command determines that the current version oftest20 is up to date, so it has nothing to do. It will just report it did nothing. Otherwise, if test20 does not exist, or main.o or vector.o are more recent thantest20, the command that creates an up-to-date version of test20 is executed: g++ main.o vector.o -o test20.
This next version of the Makefile is closer to a standard Makefile:
You trigger the compilation by just typing the make command. The first line in the Makefile implies that if you just type make you intended "make test20":
make
This command erases all the files produced during compilation and all text editor backup files:
make clean
If a method cannot be inline, or you do not want it to be inline, or if you want the class definition to contain the minimum amount of information (or you simply want the usual separate .h header file and .cpp source code file), then you need only put the prototype of the method inside the class and define the method below the class (or in a separate .cpp source file):
using namespace std;
#include
class vector
{
public:
double x;
double y;
double surface(); // The ; and no {} show it is a prototype
};
double vector::surface()
{
double s = 0;
for (double i = 0; i < x; i++)
{
s = s + y;
}
return s;
}
int main ()
{
vector k;
k.x = 4;
k.y = 5;
cout << "Surface: " << k.surface() << endl;
return 0;
}
| Output |
| Surface: 20 |
For beginners:
If you intend to develop a serious C or C++ program, you need to separate the source code into .h header files and .cpp source files. This is a short example of how it is done. The program above is split into three files :
A header file vector.h:
class vector
{
public:
double x;
double y;
double surface();
};
A source file vector.cpp:
using namespace std;
#include "vector.h"
double vector::surface()
{
double s = 0;
for (double i = 0; i < x; i++)
{
s = s + y;
}
return s;
}
And another source file main.cpp:
using namespace std;
#include
#include "vector.h"
int main ()
{
vector k;
k.x = 4;
k.y = 5;
cout << "Surface: " << k.surface() << endl;
return 0;
}Assuming vector.cpp is perfect, you compile it once and for all into a .o "object file". The command below produces that object code file, called vector.o:
g++ -c vector.cpp
Each time you modify the main.cpp source file, you compile it into an executable file, say test20. You tell the compiler explicitly that it has to link the vector.oobject file into the final test20 executable:
g++ main.cpp vector.o -o test20
Run the executable this way:
./test20
This has several advantages:
- The source code of vector.cpp need be compiled only once. This saves a lot of time on big projects. (Linking the vector.o file into the test20 executable is very fast.)
- You can give somebody the .h file and the .o file(s). That way they can use your software but not change it because they don't have the .cpp file(s) (don't rely too much on this, wait until you master these questions).
g++ -c main.cpp
g++ main.o vector.o test20
This wanders away from the "differences between C and C++" topic but if you want to look like a real programmer, you need to condense the above commands into a Makefile and compile using the make command. The file content beneath is an oversimplified version of such a Makefile. Copy it to a file named Makefile. Please note, and this is very important, that the space before the g++ commands is mandatory and that it is a Tab character. Do not type the spacebar here. Instead use the tabulation key (full left of your keyboard, above the caps lock).
test20: main.o vector.o
g++ main.o vector.o -o test20
main.o: main.cpp vector.h
g++ -c main.cpp
vector.o: vector.cpp vector.h
g++ -c vector.cpp
In order to use that Makefile to compile, type:
make test20
The make command will parse through the file Makefile and figure out what it has to do. To start with, it's told that test20 depends on main.o and vector.o. So it will automatically launch "make main.o" and "make vector.o". Then it will check if test20 already exists and checks the date stamps of test20, main.o andvector.o. If test20 already exists and main.o and vector.o have a date stamp earlier than test20, the make command determines that the current version oftest20 is up to date, so it has nothing to do. It will just report it did nothing. Otherwise, if test20 does not exist, or main.o or vector.o are more recent thantest20, the command that creates an up-to-date version of test20 is executed: g++ main.o vector.o -o test20.
This next version of the Makefile is closer to a standard Makefile:
all: test20
test20: main.o vector.o
g++ main.o vector.o -o test20
main.o: main.cpp vector.h
g++ -c main.cpp
vector.o: vector.cpp vector.h
g++ -c vector.cpp
clean:
rm -f *.o test20 *~ #*
You trigger the compilation by just typing the make command. The first line in the Makefile implies that if you just type make you intended "make test20":
make
This command erases all the files produced during compilation and all text editor backup files:
make clean
BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:
Post a Comment