A derived class constructor using base class constructor

Is this example correct? This example from a book

Constructor of the Base Class
1
2
3
4
5
6
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.
Last edited on
@pogrady
@cire
thank guys for your helping.

@cire
This is what I thought.

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>
using namespace 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;
}


.cpp file for the Person Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Person.h"

Person::Person(char* n="", char* nat="U.S.A", int s=1)
{
	name = n;
	nationality = nat;
	sex = s;
}

void Person::printName()
	{
		cout << name; 
	}

void Person::printNationality() 
	{
		cout << nationality;
	}




.h file for the Person Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include <string>
#include <iostream>
using namespace std;

class Person
{
public:
	Person(char* , char* , int);
	
	void printName();
	void printNationality();

private:
	string name, nationality;
	int sex;
};




It works only if I put them in main file. In other words, without separate them. The error in this line

Person creator("Bjarne Stroustrup", "Denmark");

Error: no instance of constructor "Person::Person" matches the argument list.

Any suggestions

In .cpp file don't write default parameters initialization. Write as below.

1
2
3
4
5
6
Person::Person(char* n, char* nat, int s)
{
	name = n;
	nationality = nat;
	sex = s;
}


In .h file you have to write default parameters initialization as below.

Person(char* n="", char* nat="U.S.A", int s=1);
@tvrameshmc
thank you so much. It worked.
Topic archived. No new replies allowed.