I know it’s frequently asked question but I can’t figure out a way for my code to work, so I’ll be glad to get some help.Here’s the code:
#include "stdafx.h"
#include "iostream"
#include "string"
#include "cmath"
#include "ctime"
using namespace std;
class quad{
int Lice;
public:
quad(int x, int z){
Lice = x*z;
}
int ShowLice(){
return Lice;
}
};
class tri : public quad{
int Lice;
public:
tri(int a, int b, int c){
Lice = a*b*c;
}
};
int main(){
quad ob1(2,2);
tri ob2(2,2,2);
cout<<ob1.ShowLice();
cout<<ob2.ShowLice();
return 0;
}
I use VS2008 and the error from the compiler is :
project1.cpp(20) : error C2512: 'quad' : no appropriate default constructor available
Thanks, Leron.
Change it to
No point in having a Lice member in both derived and base class. Make it protected so that both derived and child classes reuse the same field.
If you have a non default constructor in base class you have to invoke the base class constructor with the right arguments from the derived class constructor like i have shown above.
Also if you use inheritance it is a good idea to provide a virtual destructor in base class. The problem might not be apparent in your example code.