virtual methods

This is a discussion on virtual methods within the C++ Programming forums, part of the General Programming Boards category; // Listing 14.2 Using dynamic_cast. // Using rtti you can ignore this post, please read the next one, where I ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    virtual methods

    // Listing 14.2 Using dynamic_cast.
    // Using rtti
    you can ignore this post, please read the next one, where I made the question more understadable
    Code:
    #include <iostream>
    using namespace std;
    
    enum TYPE { HORSE, PEGASUS };
    
    class Horse
    {
    public:
    	virtual void Gallop(){ cout << "Galloping...\n"; } //why make this one
    
    private:
    	int itsAge;
    };
    
    class Pegasus : public Horse
    {
    public:
    
    	virtual void Fly() {cout<<"I can fly! I can fly! I can fly!\n";//and
                                                     //this here virtual methods?
    };
    
    const int NumberHorses = 5;
    int main()
    {
    	Horse* Ranch[NumberHorses];
    	Horse* pHorse;
    	int choice,i;
    	for (i=0; i<NumberHorses; i++)
    	{
    		cout << "(1)Horse (2)Pegasus: ";
    		cin >> choice;
    		if (choice == 2)
    			pHorse = new Pegasus;
    		else
    			pHorse = new Horse;
    		Ranch[i] = pHorse;
    	}
    	cout << "\n";
    	for (i=0; i<NumberHorses; i++)
    	{
    		Pegasus *pPeg = dynamic_cast< Pegasus *> (Ranch[i]);
    
    		if (pPeg)
    			pPeg->Fly();
    		else
    			cout << "Just a horse\n";
    
    		delete Ranch[i];
    	}
     int x; cin>>x;
    	return 0;
    }
    Last edited by incognito; 02-16-2002 at 02:54 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Correct me if I am wrong, but I have a question. When you make two virtual methods with the same name on two different classes then depending on the object forwhich you're calling the virtual method for is going to be called, the correct one is called?

    Code:
    ex.
    
    class Pegasus : public Horse
    {
    public:
    
    	virtual void Fly() {cout<<"I can fly! I can fly! I can fly!\n";}
    
    
    class Horse
    {
    public:
    	void Gallop(){ cout << "Galloping...\n"; }
    	virtual void Fly() { cout << "Horses can't fly.\n" ; }
    If I were to make a horse object....and would called on the virtual method Fly(), I would get "Horses can't fly", or if I were to make a Pegasus object and calle don the Virtual Fly() method, I would get "I can fly......."?
    Last edited by incognito; 02-16-2002 at 02:48 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Correct me if I'm wrong, but aren't there
    Code:
    code tags?

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    [sarcasm]
    oh Good one Brian, too bad that doesn't answer my question.
    [/sarcasm]


    Damn they need to make sarcasm tags!!!!!!!!!!!
    Last edited by incognito; 02-16-2002 at 02:50 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Does that make you happy?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    can't really understand your question but i'll try anyways.

    if it was a pegasus, the method in pegasus would be called.
    if it was a horse, the method in horse would be called.

    if you had a horse pointer pointing to a pegasus, the method in pegasus would be called.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Yeah that's it thank you.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    No problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Pointers to member functions using virtual methods
    By Will B-R in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2006, 06:59 AM
  3. Not 100% sure when to use virtual methods in base class
    By Silvercord in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2003, 02:19 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 10:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21