Person::Person(char* n="", char* nat="U.S.A", int s=1)
{
name = n;
nationality = nat;
sex = s;
}
Constructor of the Derived Class (inherited from the base class)
1 2
Student(char* n, int s=0, char* i=""):
Person(n, s)
Why the initialized list of the base class constructor doesn't match the initialized list of the derived class constructor?
I know this book is a little bit old, I'm not sure if this wrong in VC++ 2010?
Because there is a default value for nat, which is U.S.A (which is missing a period btw), so there is no reason to include it, but it should have been done anyway.
Why the initialized list of the base class constructor doesn't match the initialized list of the derived class constructor?
I know this book is a little bit old, I'm not sure if this wrong in VC++ 2010?
The code is incorrect anywhere.
An argument must be supplied for nat since a value is supplied for s. One cannot skip arguments in the middle of a parameter list and expect default arguments to be supplied for it. Default values are only given to arguments not supplied at the end of a parameter list. If the Person constructor was changed to:
Person::Person(char*n="", int s=1, char* nat="U.S.A")
the derived class constructor would work. Leaving the base class the same and adjusting the derived class to supply the argument would also work.
I have another question. Why this example doesn't work.
main file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "Person.h"
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
Person creator("Bjarne Stroustrup", "Denmark");
cout << "The creator of C++ was ";
creator.printName();
cout << ", who was born in ";
creator.printNationality();
cout << ".\n";
system("pause");
return 0;
}