This program is printing possible gifts to whom the user wants to give. It asks the user to input the name and the birthday.
I have this problem involving classes. I must have misunderstood the whole thing about classes. I don't know how to create a header file and an implementation file. I don't know what to put inside the implementation file. So, how do I separate this into three files: the header, implementation and the main program. Please help me. I don't really understand. My teacher requires me to give him those three files.
I'm using Dev C++ and this program needs to run in MS Visual Studio Express 2012. And i also don't know how.
My advice: put the class declaration in a header file(.h or .hpp), say head.h, and add it tk your project. Also, create a .cpp file(implementation file) with the same name as the header, so head.cpp. In the head.cpp-implementation file-, #include your header file, head.h, contaning the class, and define the methods(member functions). Then the main .cpp file would initialize any static member variable, and also have your main function.
#include "classOne.h" // Header file of a class to be used
#include "classTwo.h" // Another class if needed
int main()
{
// compiler knows the types
// so no errors compiling this.
classOne c1(1,2); // using constructor to initialize field1 and field2
c1.function();
classTwo c2;
}
class classOne {
private:
int field1; // variables are left uninitialized in .h file
int field2;
public:
int function(); // usually there is no body for functions in .h file
int otherFunction();
// constructor
classOne(int num1, int num2); // also no body in .h file
}; // semicolon - I always forget this one
classOne.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// cpp file must include .h file, so that
// all the names are known to compiler
#include "classOne.h"
// We must use full name of a function,
// that is use its class name and '::'
int classOne::function() {
return field1; // Body is here
}
// All functions from .h files must have a body
int classOne::otherFunction() {
return field2;
}
// Body of a constructor, also with full name
classOne::classOne (int num1, int num2){
field1 = num1;
field2 = num2;
}
You can use menu: File, New,then choose headfile, and enter other information. Check "Add to project", then you can edit it. I'm using Viscul C++, and I don't know if the step is the same.