Hi, so I have a Software exam coming up and I was trying to do some past papers to understand what it is I have to do.
Im still a beginner with classes and such, but was wondering if someone could explain the following code
1 2 3 4 5 6 7 8 9 10
class module_record
{
private:
int module_ID;
int exam_mark;
public:
module_record(int _module_ID=-1,int _exam_mark=0)
:module_ID(_module_ID),exam_mark(_exam_mark) {}
module_record(const module_record& mr)
:module_ID(mr.module_ID),exam_mark(mr.exam_mark) {}
I understand the class module_record is created with private variables module_ID and exam_mark that can only be used by the class. The next two constructors confuse me a little - what is the "-1" for? Everything following the :module_ID... whats that for? I believe it is called an initialisation list, but what does that mean, and how is it working in this code?
The -1 and the 0 are default arguments: if you call the constructor without any arguments then it will be the same as calling it with -1 and 0. Saves a bit of typing, having lots of different constructors, writing lots of initialisations.
The bits after the : are, as you say, an initialisation list. They are a shorthand way of setting the data members in the class. See "Member initialization in constructors" in http://www.cplusplus.com/doc/tutorial/classes/
Setting default arguments saves having three separate constructors
module_record(int _module_ID,int _exam_mark)
module_record(int _module_ID)
module_record()
with some of the class members being set in the function part between curly braces { }.
"Is the equivalent of" is a bit strong - if an argument is actually specified then it will supersede the default argument.
I presume the -1 and 0 are subjective to the task given as to what they start at.
Yes, there is nothing special about -1 or 0. Values given here will depend on the problem. They also have to have the same type as the particular argument (int, double, string, bool etc.) If you glance through the reference section of this website you will find that many functions in the standard library have default arguments.
Another question - I am used to putting "~module_record(){}" for a default constructor for "module_record" or something similar. This is how we've done it all through the course. However, having had a look at a few mark schemes and what not, I've noticed they all use
"virtual ~module_record(){}" - what is the significance of virtual for a destructor?
"virtual ~module_record(){}" - what is the significance of virtual for a destructor?
The glib answer is "the same significance as for any other method".
When you define a method as virtual, you're indicating that run-time polymorphism to apply to that method. In other words, when that method is called on a pointer or reference to an object, you want the program to determine the actual type of the object being pointed to, and to use the virtual table to find the overridden method in the correct subclass, if one exists, and to call that instead.
It's essential for destructors, because when destroying an object, you need to make sure you're calling the correct destructor, rather than directly calling a base class constructor.