It's a little longer, as we're spreading the functions over multiple lines rather than keeping them condensed. But, as mutexe said, the implementations would more than likely go in a cpp file, rather than in the header declarations.
Any time you see the class name followed by a ":" it is a constructor.
It is basically a function that initializes all of the classes variables when a new instance of that class is called.
For example:
1 2 3 4 5 6 7 8 9 10
class Example{
int x;
char c;
public:
void printHello(){cout<<"Hello!\n";}
Example(//when there are no arguments it is a default constructor )
:x(6),
c('a'){printHello();}
};
In the above "Example" class every time a new instance of the class is created, by default the x value will be set to 6, the c value will be set to a, and the program will output "Hello!".
You can add additional constructors like this:
1 2 3 4 5 6
pretend we are still in class Example
Example(int a, char b)
: x(a),
c(b){}
now doing something like Example ex=Example(11,b);
would create a new instance of the Example class where x=11, c=b, and no function is called on creation.
I think i understand most of it now. Thanks!
One more question, i would like to know why is & used for myLicense and getLicense() but not myYear? is it just because of preference of which to pass by reference or some technical requirement?
thank you
means you don't make a copy of the variable when you pass by reference, which would happen if you pass by value. if a method takes a structure or large object it would be inefficient to pass by value.
It doesn't matter with primitive data type as they are so small.
to be honest i dont think i've ever seen initializing by reference, which looks like what's happening on line 6 with your constructor. i'd assume you'd need a reference variable declared but your liscense declaration on line 3 is not. i could be wrong though. i'm interested to get some opinions on this.