polymorphism

A

Anees

Dear Fellows:

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();
}

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;

}
 
A

Alf P. Steinbach

* 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
 
A

Anees

* 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!- Hide quoted text -

- Show quoted text -

Thank you :)
 
V

Vicky

Dear Fellows:

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();
        }

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;

}

Never call virtual function inside ctor and dctor. It's behaviour
doesn't meet standard C++. Consult with Effective C++ by Scott Meyer.
 
A

Alf P. Steinbach

* Vicky:
Dear Fellows:

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();
}

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;

}

Never call virtual function inside ctor and dctor.

Well, that's one opinion.

For C++ some people find it practical to do so.

For e.g. Java, which is not as safe as C++ is in this regard, it's IMHO good
advice to stay away from virtual calls (on the same class) in a constructor.
It's one of the most common bug causes in Java. However, in C++ it's safe.

It's behaviour doesn't meet standard C++.

I'm happy that that's incorrect. :)

Consult with Effective C++ by Scott Meyer.

That's generally good advice.


Cheers & hth.,

- Alf
 
P

Pascal J. Bourguignon

Vicky said:
Never call virtual function inside ctor and dctor. It's behaviour
doesn't meet standard C++. Consult with Effective C++ by Scott Meyer.

Oops! RAII is broken. Who would have imagined it?


#include <iostream>

class Base {
public:
Base(){};
virtual ~Base(){};
virtual Base* initialize(){ this->print(); return(this); }
virtual void print(){ std::cout<<"Base"<<std::endl; }
};

class Derived:public Base {
public:
Derived(){};
virtual ~Derived(){};
virtual void print(){ std::cout<<"Derived"<<std::endl; }
};

int main(){
Base* d=(new Derived())->initialize();
delete d;
return(0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top