I'm new to programming , please bare with me. So this means that the implementation file and the program file" the one with the main function\driver file",must contain the header file in the include directive that names the interface file? And all this is really about linking and compiling the interface and implementation files?
I do not think the issue is compilation and linkage. I think both the function and the driver can be in the same file unless the assignment specifically states that they must be in seperate files (does it?).
The driver program in this case is main(). What he means is that you have a function, say f() that he wants you to demonstrate. To do this, he wants you to drive f(), i.e. to call it with different input that shows that f() works as expected.
For instance, lets say f() adds two numbers x and y and returns the sum. Then you could have a function and a driver program such as:
1 2 3 4 5 6 7 8 9 10 11 12
f(int x, int y) // This is the FUNCTION you wrote
{
return x+y;
}
int main() // This is the DRIVER PROGRAM - it exercises f()
{
cout << "Sum of 2 and 4 is " << f(2,4) << endl;
cout << "Sum of 0 and -1 is " << f(0,-1) << endl;
cout << "Sum of 200 and 12000 is " << f(200, 12000) << endl;
return 0;
}
Your instructor is not being 100% correct in his lingo, though. main() is not a program - the combination of f() and main() and libraries, when compiled, linked etc. is a program, but that is a whole other story...
Hello,
So to 'run' this...Means that the function f() does not have to be in any implementation file or found in some library stated in an include directive. Function f() is just declared in bits of code and main() just"drives the out put"
// function example
#include <iostream>
using namespace std;
int f(int x, int y) // This is the FUNCTION you wrote
{
return x+y;
}
int main() // This is the DRIVER PROGRAM - it exercises f()
{
cout << "Sum of 3 and 14 is " << f(3,14) << endl;
cout << "Sum of 0 and -1 is " << f(0,-1) << endl;
cout << "Sum of 200 and 12000 is " << f(200, 12000) << endl;
system("pause");
return 0;
}
So then its possible to " steer" f() with cin objects?
What the assignment specifically states, I don't know, I'm not in the class.