class IClass1
{
public:
virtualint add(int, int)=0;
};
class IClass2
{
public:
virtualint add(int, int)=0;
};
/* implementation method 1 for CChild, works */
class CChild
{
public:
int IClass1::add(int x, int y){return (x + y);}
int IClass2::add(int x, int y){return (x + y + 100);}
};
/* implementation method 1 for CChild END, works */
/* implementation method 2 for CChild, not work */
class CChild
{
public:
int IClass1::add(int x, int y);
int IClass2::add(int x, int y);
};
int CChild::IClass1::add(int x, int y) // this will not work, why? What should
// I write to separate declaration and
// definition?
{
return (x+y);
}
/* implementation method 2 for CChild END, not work */
#include <iostream>
class IClass1 {
public:
int add(int x, int y) {
return x + y;
}
};
class IClass2 {
public:
int add(int x, int y) {
return x + y + 100;
}
};
class CChild : public IClass1, public IClass2 {
};
int main(int argc, char* argv[]) {
int first, second;
CChild cc;
first = cc.IClass1::add(5, 5);
second = cc.IClass2::add(5, 5);
std::cout << "IClass1::add - " << first <<
"\nIClass2::add - " << second << "\n";
return 0;
}
Thanks for the replay. I was using MS VC++ to compile the 1st implementation. One mistake in class CChild, it should be declared as class CChild : public IClass1, public IClass2
In implementation 1, it works with the following test code:
The reason I want to use this kind of interface is: I prefer to define pure virtual function and its related variables within one interface, and implement the virtual function within the inherent class. It is fine if the function names are all different within every interface. But, sometimes, two interfaces will have common names, as in this example.