Re: polymorphism
* Anees:
I am using visual studio 2008. Can anyone help, why does the following
polymorphism not working? It is keep giving "base" as output :(
#include <iostream>
using namespace std;
class base {
public:
base() {
print();
}
This will always call base::print, no matter whether print is virtual, because
while executing the base constructor the type of the object is base.
There is a FAQ item that discusses this.
It's nearly always a good idea to read the FAQ.
protected:
virtual void print() {
cout << "base" << endl;
}
};
class derived:public base {
protected:
virtual void print() {
cout << "derived" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[]){
derived d;
This is a non-standard Microsoft monstrosity.
Why write more in order to make the code non-standard?
In standard C++ write just
int main()
See, it's much less to write.
And it's standard.
}
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!