i need a good example of virtual functions

This is a discussion on i need a good example of virtual functions within the C++ Programming forums, part of the General Programming Boards category; Code: ive tried to understand tutiorals about virtual but i still dont understand so this is just example class myclass ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    i need a good example of virtual functions

    Code:
    ive tried to understand tutiorals about virtual but i still dont understand
    
    so this is just example 
    
    class myclass
    {
    virtual void func1();
    public:
    };
    
    void myclass::func1()
    {
    do what ever;
    }
    
    now what can be done with this being virtual within the class

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    class MyName
    {
    private:
    	char UserName[NAME_SIZE];
    
    public:
    	void SetUserName ( char *NewUserName );
    
    };
    void  MyName::SetUserName ( char *NewUserName ) 
    {
    	strcpy( UserName, NewUserName );
    }
    
    
    int main()
    {
    	MyName MyClassName;
    	char Name[30];
    
    	cout<< "What is your name?";
    	cin >> Name;
    
    	MyClassName.SetUserName( Name );
    	
    }
    that sets

    that make sense?
    Last edited by twomers; 02-14-2006 at 11:39 AM.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    you didnt put it as virtual

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    hmmm, sorry, I didn't notice that ...

    I'm afraid I don't know anything about that ... try this though:

    Click Here

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You need to make a class and derive it from myclass. Then, you need to implement func1 again in the derived class (usually doing something different than myclass's version). Then, you can use myclass polymorphically.

    One way to use it is to have a separate function that takes a myclass reference and calls its func1() method. Then you can pass an instance of myclass or the derived class and the correct version of func1 will be called:
    Code:
    #include <iostream>
     
    class myclass
    {
    public:
      virtual void func1();
    };
     
    void myclass::func1()
    {
      std::cout << "myclass::func1" << std::endl;
    }
     
    class derivedclass : public myclass
    {
    public:
      virtual void func1();
    };
     
    void derivedclass::func1()
    {
      std::cout << "derivedclass::func1" << std::endl;
    }
     
    void UseIt(myclass& myc)
    {
      myc.func1();
    }
     
    int main()
    {
      myclass a;
      derivedclass b;
      UseIt(a);
      UseIt(b);
    }

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Contrary to what some people may believe, you don't go from writing a "hello world" program to understanding virtual functions a few days later. Virtual functions enable you to enact what's called "polymorphism". That involves understanding what pointers are and how they work, what iheritance is and how it works, and finally how virtual functions play a role. To get "polymorphic behavior", you need several ingredients:

    1) A common base class
    2) A pointer to the base class
    3) A virtual function in the base class
    4) Derived classes that override the virtual function in the base class

    Code:
    #include <iostream> 
    using namespace std;
    
    class Animal
    {
    public:
    	virtual void identify()
    	{
    		cout<<"I'm an Animal."<<endl;
    	}
    };
    
    class Dog : public Animal
    {
    	void identify()
    	{
    		cout<<"I'm a Dog."<<endl;
    	}
    };
    
    class Cat : public Animal
    {
    	void identify()
    	{
    		cout<<"I'm a Cat"<<endl;
    	}
    };
    
    
    int main()
    {
    	Animal myAnimal;
    	Dog myDog;
    	Cat myCat;
    
    	Animal* ptr = &myAnimal;
    	ptr->identify();
    
    	ptr = &myDog;
    	ptr->identify();
    
    	ptr = &myCat;
    	ptr->identify();
    
    	
    	return 0;
    }
    "Polymorphic behavior" means that you get different results from the same code. The program does the exact same thing three times:

    ptr->identify();

    but there are three different results. It's as if there is an internal switch statement inside ptr, and ptr first checks to see what type of object is stored inside it before deciding what version of identify() to call.
    Last edited by 7stud; 02-14-2006 at 01:25 PM.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    thanks alot guys , these simple examples are great
    any reason why dog and cat dont have public sections?

    and


    Animal* ptr = &myAnimal; // would this mean the same thing as
    Animal *animal= new Animal; //this
    Last edited by Anddos; 02-14-2006 at 01:15 PM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    any reason why dog and cat dont have public sections?
    No. That was just an oversight on my part.
    Code:
    Animal* ptr = &myAnimal; // would this mean the same thing as
    Animal *animal= new Animal; //this
    No they are not the same. Stick with the first unless you understand and have a reason to use the second. The second line involves what's called "dynamically" creating an object.
    Last edited by 7stud; 02-14-2006 at 01:25 PM.

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Just for fun so you can really appreciate the effects of virtual, recompile 7studs program, but remove virtual keyword from the base class leaving everything else the same and observe the output.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    whats this
    Code:
    void UseIt(myclass& myc)
    {
      myc.func1();
    }
    is this a special meaning when working with virtual
    i understand you making te object but you didnt do void Useit(); in the class

    and why did

    UseIt(a);
    UseIt(b);

    call the func1(); seeing as its not the constructor
    Last edited by Anddos; 02-15-2006 at 04:42 AM.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    UseIt is a function that is completely separate from the classes. Think of it as something like the getline function for strings if you are familiar with that. What it does is use an instance of myclass (or one of myclass's derived classes) by calling func1() on the instance passed in.

    It takes a myclass reference as its parameter. When the parameter is a base class reference or pointer, you can pass an instance of the derived class to the function. This is the idea of substitutability and is the most important point of virtual methods. The UseIt function doesn't need to know about how many derived classes there are (if any), it just uses the interface specified by the base class, and if derived classes want to do something different they can without you having to change the UseIt method.

    So in main(), UseIt is called twice. The first time it is given a, which is an instance of myclass. It works and "myclass::func1" is output through the call to func1(). The second time it is given b, an instance of derivedclass. It works on b and outputs "derivedclass::func1" because even though myc is a myclass reference, it refers to the derivedclass object b, so the derivedclass's version of func1() is called.

    You can do the same thing with 7stud's example by adding this function:
    Code:
    void IdentifyAnimal(Animal* ptr)
    {
      ptr->identify();
    }
    and calling it from main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concerning virtual functions
    By shadovv in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2005, 10:22 PM
  2. virtual functions in C
    By sangi in forum C++ Programming
    Replies: 7
    Last Post: 11-19-2004, 12:25 PM
  3. Virtual Functions
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2004, 03:13 PM
  4. recursive virtual functions
    By ygfperson in forum C++ Programming
    Replies: 0
    Last Post: 05-25-2003, 08:00 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