#include <iostream>
usingnamespace std;
class base {
public:
int b;
};
class derived:public base {
public:
int d;
};
int main() {
derived D;
base * bp=& D;
cout<<D.b<<endl;
cout<<D.d<<endl;
cout<<bp->b<<endl;
cout<<bp->d<<endl;//error message: d cannot be resolved
return 0;
}
then What can I do to let the more universal pointer to use that specific new object?
if this cannot be done, then I feel the usage of polymorphism is very limited!!! Hope I am wrong.
class base {
public:
virtualint func(int x) = 0;
};
class derived :public base {
public:
int func(int x)
{
cout << "My age is ";
return x;
cout << endl;
}
};
int main() {
derived D;
base * bp = &D;
cout << bp->func(19) << endl;
system("pause");
return 0;
}