Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 30th, 2005, 1:06 AM   #1
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
good for n00bs

here is a simple program demonstrating virtual functions. good to analyze if you're just getting into that sort of thing (like me :-) ).

#include <iostream>
#include <conio.h>
using namespace std;

//  base class
class animal
{
      public:
             //want to show when constuctor/destructor are called
             animal(){cout<<"constructor called..."<<endl;};
             virtual ~animal(){cout<<"\ndestructor called..."<<endl;};
             virtual void speak(){cout<<"\nanimal sound!\n"<<endl;}
             
};

//derived classes follow...

class dog : public animal
{
      public:
             void speak(){cout<<"\nwoof!\n"<<endl;}
};
                     
class chicken : public animal 
{
      public:
             void speak(){cout<<"\ncluck!\n"<<endl;} 
};

class cow : public animal
{
      public:
             void speak(){cout<<"\nmoo!\n"<<endl;}
};

class cat : public animal
{
      public:
             void speak(){cout<<"\nmeow!\n"<<endl;}
};  
//end of classes

           

int main()
{
    //  pointer to base class
    animal * base;
    //  variable for animal type
    int choice;
    //  variable for quitting
    char quit;
    
    do
    {
        cout<<"let's use polymorphism to make animal sounds!"<<endl;
        cout<<"1.\tdog\n2.\tchicken\n3.\tcow"<<endl;
        cout<<"4.\tcat\n5.\tbase animal"<<endl;
        cin>>choice;
    
        switch (choice)
        {
               case 1:
                    base = new dog;
                    break;
                
               case 2:
                    base = new chicken;
                    break;
                
               case 3:
                    base = new cow;
                    break;
                    
               case 4:
                    base = new cat;
                    break;      
                
               default:
                    base = new animal;
                    break;
        }           
    
        //  call appropriate "speak" function
        //  using virtual functions   
        base->speak();
    
        //  quitting condition test
        cout<<"wanna quit?"<<endl;
        cin>>quit;
          
        if (quit == 'y')
        {
                 //  free memory of animal objects
                 delete base;
        }
    
    }while (quit != 'y');
    
    //pause program
    getch();
    
    return 0;
}//  end main
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old May 4th, 2005, 10:00 AM   #2
MonkeyRevolution
Programmer
 
MonkeyRevolution's Avatar
 
Join Date: Apr 2005
Location: In the toaster. Shh!
Posts: 57
Rep Power: 4 MonkeyRevolution is on a distinguished road
Send a message via AIM to MonkeyRevolution Send a message via MSN to MonkeyRevolution Send a message via Yahoo to MonkeyRevolution
That's awesome!

This is...C-something, right? I'm not going to guess which one...it's been far too long since I've known a C-language.
__________________
I had a dream that Triumph the Insult Dog owned Microsoft...

How...appropriate.
MonkeyRevolution is offline   Reply With Quote
Old May 4th, 2005, 10:07 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
c++ it has class's in it,.
Berto is offline   Reply With Quote
Old May 4th, 2005, 10:40 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I love virtual functions - they're great when you need to replace a switch() statement with something faster.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 4th, 2005, 11:12 AM   #5
MonkeyRevolution
Programmer
 
MonkeyRevolution's Avatar
 
Join Date: Apr 2005
Location: In the toaster. Shh!
Posts: 57
Rep Power: 4 MonkeyRevolution is on a distinguished road
Send a message via AIM to MonkeyRevolution Send a message via MSN to MonkeyRevolution Send a message via Yahoo to MonkeyRevolution
Quote:
Originally Posted by Berto
c++ it has class's in it,.
Aaaaah! Thanks, Berto!
__________________
I had a dream that Triumph the Insult Dog owned Microsoft...

How...appropriate.
MonkeyRevolution is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC