我写了下面的代码。具有多个级别的抽象类
#include <iostream>
using namespace std;
class CI
{
public:
virtual void display() =0;
};
class Inter: public CI
{
public:
int parseData (int);
};
Inter::parseData (int data)
{
cout <<"Parsing the data "<<data;
return data*100;
}
class Last: public Inter
{
public:
void display();
};
void Last::display()
{
cout <<" Last:: Displaying From Last "<<endl;
}
class USB: public Inter
{
public:
void display();
};
void USB::display()
{
cout <<" USB:: Displaying From Last "<<endl;
}
int main (int argc, char ** argv)
{
int temp;
CI *obj = new Last;
obj->display();
temp = obj->parseData (100);
cout <<"Parsed DAta .. "<<temp<<endl;
delete obj;
obj = new USB;
obj->display();
temp = obj->parseData (200);
}
我的问题是:
为什么我不能够调用obj-> parseData功能?根据我的理解,因此类'Last'和'USB'是从'Inter'类派生的,它应该可以被调用吗?
请告诉我我的理解错在哪里?
否因为obj是CI类型的,parseData不是虚拟的,它只会在CI类中寻找parseData实现 – sethi
如果你想要一个方法可用,你必须使用一个指向它声明的级别的指针。 'parseData'没有在'CI'中声明。 – StoryTeller