c++关于抽象类的一个问题,新手求帮助。。。
#include <iostream.h>
class A
{
private:
//…其它成员
public:
virtual void func(int data)
{
cout<<"class A:"<<data<<endl;
}
void func(char *str)
{
cout<<"class A:"<<str<<endl;
}
};
class B: public A
{
//…其它成员
public:
void func()
{
cout<<"function in B without parameter! \n";
}
void func(int data)
{
cout<<"class B:"<<data<<endl;
}
void func(char *str)
{
cout<<"class B:"<<str<<endl;
}
};
void main()
{
A *pA;
B b;
pA=&b;
pA->func(1);
pA->func("haha");
pA->func();//为什么不能调用呢?难道只能调用抽象类规定好的函数?
// return 0;
}
#include <iostream>
using namespace std;
class A
{
private:
//…其它成员
public:
virtual void func(int data)
{
cout<<"class A:"<<data<<endl;
}
void func(char *str)
{
cout<<"class A:"<<str<<endl;
}
};
class B: public A
{
//…其它成员
public:
void func()
{
cout<<"function in B without parameter! \n";
}
void func(int data)
{
cout<<"class B:"<<data<<endl;
}
void func(char *str)
{
cout<<"class B:"<<str<<endl;
}
};
int main()
{
A *pA;
B b;
pA=&b;
pA->func(1);
pA->func("haha");
((B*)pA)->func();
return 0;
}
class A
{
private:
//…其它成员
public:
virtual void func(int data)=0 //这样才叫抽象类呀,楼主补下基础呀
{
cout<<"class A:"<<data<<endl;
}
void func(char *str)
{
cout<<"class A:"<<str<<endl;
}
};