U
utab
Dear all,
I was experimenting on inheritance and I did some simple tries on a
simple class. See the code below:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Base{
//Base(int val):a(val){std::cout << "Ctor :" << a << std::endl;}
//line void print(){std::cout << id << std::endl;}
void print(){std::cout << str << '\n' ;}
//private:
protected:
std::string str;
std::vector<int> vec;
int a;
};
struct derivedublic Base{
derived(int val): a(val){ }
private:
int a;
};
int main(){
//Base b(4);
Base b;
derived d(6);
b.print();
return 0;
}
This code just outputs an empty line. That is fine.
When I uncomment the line in the base class for the ctor(for Base)
definition and the related declaration in the main function(plus
commenting out Base b in the main), I am getting an error related to
the default ctor:
simple.cc: In constructor 'derived::derived(int)':
simple.cc:20: error: no matching function for call to 'Base::Base()'
simple.cc:8: note: candidates are: Base::Base(int)
simple.cc:7: note: Base::Base(const Base&)
My question is that when there is a ctor other than the default ctor,
do I have to explicitly define the default ctor? Or am I missing sth
important in the context of this simple inheritance case study?
My best,
I was experimenting on inheritance and I did some simple tries on a
simple class. See the code below:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Base{
//Base(int val):a(val){std::cout << "Ctor :" << a << std::endl;}
//line void print(){std::cout << id << std::endl;}
void print(){std::cout << str << '\n' ;}
//private:
protected:
std::string str;
std::vector<int> vec;
int a;
};
struct derivedublic Base{
derived(int val): a(val){ }
private:
int a;
};
int main(){
//Base b(4);
Base b;
derived d(6);
b.print();
return 0;
}
This code just outputs an empty line. That is fine.
When I uncomment the line in the base class for the ctor(for Base)
definition and the related declaration in the main function(plus
commenting out Base b in the main), I am getting an error related to
the default ctor:
simple.cc: In constructor 'derived::derived(int)':
simple.cc:20: error: no matching function for call to 'Base::Base()'
simple.cc:8: note: candidates are: Base::Base(int)
simple.cc:7: note: Base::Base(const Base&)
My question is that when there is a ctor other than the default ctor,
do I have to explicitly define the default ctor? Or am I missing sth
important in the context of this simple inheritance case study?
My best,