#include <iostream>
class A
{
private:
constint var;
public:
A(constint v): var(v) {};
void fa() { std::cout << "in fa(), var=" << var << std::endl; };
};
class B: public A
{
private:
constint var;
public:
B(constint 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; };
^
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
#include <iostream>
class A
{
private:
constint var;
public:
A(constint v): var(v) {};
void fa() { std::cout << "in fa(), var=" << var << std::endl; };
};
class B: public A
{
private:
constint var;
public:
B(constint 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) {};
^
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:
#include <iostream>
class A
{
private:
constint var;
public:
//A::var is const and is initializated by constructor initialization list
A(constint v): var(v) {}; // A constructor
void fa() { std::cout << "in fa(), var=" << var << std::endl; };
};
class B: public A
{
private:
constint var;
public:
//B::var is const and is initializated by constructor initialization list
B(constint 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
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).
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.
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.