Trouble initialising base class members

I'm trying to put new values when I intialise the object from the derived class, but I just can't figure out why it's not working. Can someone please tell me why this doesn't work?

It says "name" and "age" on line 34 are undefined. Also, if I leave the parentheses on line 42 empty then all the foo's from line 44-47 throw an "expression must have a class type" error.

Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class BaseClass
{
public:
	string m_name;
	int m_age;

	BaseClass(string name = "default", int age = 10);
	string GetName() { return m_name; };
	int GetAge() { return m_age; };
};

BaseClass::BaseClass(string name, int age) :
	m_name(name),
	m_age(age)
{}

class DerivedClass : public BaseClass
{
public:
	int m_num2;
	int m_num3;

	DerivedClass(int num2 = 20,	int num3 = 30);
	int GetNum2() { return m_num2; };
	int GetNum3() { return m_num3; };
};

DerivedClass::DerivedClass(int num2, int num3) :
	BaseClass(name, age),	//Thought something like this was possible
	
	m_num2(num2),
	m_num3(num3)
{}

int main()
{
	DerivedClass foo("Mr Tickle",0,0,0);
	cout << "hello, ";
	cout << foo.GetName() << endl;
	cout << foo.GetAge() << endl;
	cout << foo.GetNum2() << endl;
	cout << foo.GetNum3() << endl;

	int pause;
	cin >> pause;
    return 0;
}
1
2
3
DerivedClass::DerivedClass(int num2, int num3) :
	BaseClass(name, age),	//Thought something like this was possible
	


On that second line, you're trying to call the BaseClass constructor, and you're passing it two parameters. Parameters called "name" and "age".

At this point, the only values you have to work with are the parameters you passed in to the DerivedClass constructor on the first line. What are they called? "num2" and "num3".

So where exactly are "name" and "age" coming from?


DerivedClass foo("Mr Tickle",0,0,0);
Here's an attempt to call a DerivedClass constructor with four parameters. There is no such constructor. There is no DerivedClass constructor that takes four parameters.
Last edited on
Hmmm. I think I'm missing some understanding of how inheritance and constructors work.

Name and age are in the BaseClass constructor. it was my understanding that DerivedClass would inherit these so that "foo" would have 4 data members in total. How do I initialise an object of DerivedClass like I'm attempting here? Does it not have 4 data members?
Yes, DerivedClass does inherit the base class variables. (sizeof(Derived) >= sizeof(Base))

But what you wrote doesn't make sense semantically. Think of the constructor for DerivedClass as just being another function (albeit special because it doesn't have a return type). You're passing name and age into the construction of BaseClass, but you never gave values to name and age.

Question: What do you think the values of name and age are on line 34?

As Repeater said, you don't have a 4x-arg constructor for your derived class, so line 42 doesn't make sense.
A 4x-arg constructor is not automatically created, you must make it.

You need to do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class DerivedClass : public BaseClass
{
  public:
	int m_num2;
	int m_num3;

	DerivedClass(string, int, int, int);
};

DerivedClass::DerivedClass(string name, int age, int num2, int num3) :
	BaseClass(name, age),	
	m_num2(num2),
	m_num3(num3)
{}

int main()
{
	DerivedClass foo("Mr Tickle", 2, 3, 4);
}

Last edited on
Yes that does work, and makes sense too.

Thank you both for the replies, much appreciated :)
Topic archived. No new replies allowed.