inheritance question

I am stuck on this little problem. I appreciate any help you could provide.

Class B inherits from class A.
What is the syntax for private variables if they have the same name in class A and class B?

Here is my failed attempt:
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
#include <iostream>

class A
{
	private:
		const int var;
	public:
		A(const int v): var(v) {};
		void fa() { std::cout << "in fa(), var=" << var << std::endl; };
};

class B: public A
{
	private:
		const int var;
	public:
		B(const int v): var(v) {};
		void fb() { std::cout << "in fb(), var=" << var << std::endl; };
};

B b(3);

int main()
{
	b.fb();
	return 0;
}

Syntax error:

C:\demo_MinGW>g++ inheritConstructor3.cpp
inheritConstructor3.cpp: In constructor 'B::B(int)':
inheritConstructor3.cpp:6:13: error: 'const int A::var' is private
   const int var;
             ^
inheritConstructor3.cpp:17:19: error: within this context
   B(const int v): var(v) {};
                   ^
inheritConstructor3.cpp:17:19: error: class 'B' does not have any field named 'v
ar'
inheritConstructor3.cpp:17:24: error: no matching function for call to 'A::A()'
   B(const int v): var(v) {};
                        ^
inheritConstructor3.cpp:17:24: note: candidates are:
inheritConstructor3.cpp:8:3: note: A::A(int)
   A(const int v): var(v) {};
   ^
inheritConstructor3.cpp:8:3: note:   candidate expects 1 argument, 0 provided
inheritConstructor3.cpp:3:7: note: A::A(const A&)
 class A
       ^
inheritConstructor3.cpp:3:7: note:   candidate expects 1 argument, 0 provided
inheritConstructor3.cpp: In member function 'void B::fb()':
inheritConstructor3.cpp:6:13: error: 'const int A::var' is private
   const int var;
             ^
inheritConstructor3.cpp:18:47: error: within this context
   void fb() { std::cout << "in fb(), var=" << var << std::endl; };
                                               ^

Thank you.
Actal problem that class A does not have a default constructor, but it is invoked in B(int) constructor. Either provide A with constructor, op call A(int) constructor from B
Thank you for the suggestions MiiNiPaa. Unfortunately I am still stuck.
My above code provided A with a constructor. What is an "op" call?

The following is my failed attempt at calling A(int) constructor from B.
What else can I try?

Thank you.
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
#include <iostream>

class A
{
	private:
		const int var;
	public:
		A(const int v): var(v) {};
		void fa() { std::cout << "in fa(), var=" << var << std::endl; };
};

class B: public A
{
	private:
		const int var;
	public:
		B(const int v): A(v) {};	// call A(int) constructor from B
		void fb() { std::cout << "in fb(), var=" << var << std::endl; };
};

B b(3);

int main()
{
	b.fb();
	return 0;
}

Syntax error:
C:\demo_MinGW>g++ inheritConstructor3.cpp
inheritConstructor3.cpp: In constructor 'B::B(int)':
inheritConstructor3.cpp:17:3: error: uninitialized member 'B::var' with 'const'
type 'const int' [-fpermissive]
   B(const int v): A(v) {};
   ^
Because B::var is const and should be initializated in constructor, which you didn't do.
Remember: A::var and B::var are two different variables.
Hi MiiNiPaa.

I am following your suggestions, but probably doing it wrong. Both classes A and B have constructors and both A and B have const var initialized by a constructor initialization lists:
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
#include <iostream>

class A
{
	private:
		const int var;
	public:
		//A::var is const and is initializated by constructor initialization list
		A(const int v): var(v) {}; // A constructor
		void fa() { std::cout << "in fa(), var=" << var << std::endl; };
};

class B: public A
{
	private:
		const int var;
	public:
		//B::var is const and is initializated by constructor initialization list
		B(const int v): var(v) {}; // B constructor
		void fb() { std::cout << "in fb(), var=" << var << std::endl; };
};

A a(2);
B b(3);

int main()
{
	b.fb();
	return 0;
}

Syntax error:
C:\demo_MinGW>g++ inheritConstructor3.cpp
inheritConstructor3.cpp: In constructor 'B::B(int)':
inheritConstructor3.cpp:19:24: error: no matching function for call to 'A::A()'
   B(const int v): var(v) {}; // B constructor
                        ^
inheritConstructor3.cpp:19:24: note: candidates are:
inheritConstructor3.cpp:9:3: note: A::A(int)
   A(const int v): var(v) {}; // A constructor
   ^
inheritConstructor3.cpp:9:3: note:   candidate expects 1 argument, 0 provided
inheritConstructor3.cpp:3:7: note: A::A(const A&)
 class A
       ^
inheritConstructor3.cpp:3:7: note:   candidate expects 1 argument, 0 provided
Last edited on
And now you did not defined default constructor or are calling parametrized one again.
MiiNiPaa,
OK, I got it this time. Thanks for your patients.
I am surprised that a default constructor is required even when the class is invoked with a parameter.

A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4).


This works:
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
#include <iostream>

class A
{
	private:
		const int var;
	public:
		A(): var(1) {}; // A default constructor
		A(const int v): var(v) {}; // A parameterized constructor
		void fa() { std::cout << "in fa(), var=" << var << std::endl; };
};

class B: public A
{
	private:
		const int var;
	public:
		B(): var(2) {}; // B default constructor
		B(const int v): var(v) {}; // B parameterized constructor
		void fb() { std::cout << "in fb(), var=" << var << std::endl; };
};

A a;
B b(22);

int main()
{
	a.fa();
	b.fb();
	return 0;
}

output:
in fa(), var=1
in fb(), var=22
Last edited on
I am surprised that a default constructor is required even when the class is invoked with a parameter.
When you are creating B object, it needs to create A part first. But which constructor should be used? Unless you tell it explicitly in your B constructor, A default constructor would be used to create A part.

I have a question: why do you have two variables with the same name in both classes? It leads to name hiding and strange behavior with pointer to base object.
I have a question: why do you have two variables with the same name in both classes? It leads to name hiding and strange behavior with pointer to base object.


The variables have the same name because they mean the same thing, and I was not aware of name hiding and strange behavior with pointer to base object.
I will use different names now.

Thank you for your advice.
The variables have the same name because they mean the same thing,

Then why don't make base class variable protected instead of private and use it in derived instead of redefining it?
Or just make base class responsible for manipulating it?
You are right. I will move the variables to protected in base class.
Although, the each class manipulates the variable in a different way, so the children will be responsible for that.

You are a good teach MiiNiPaa; thank you for your help.
Topic archived. No new replies allowed.