Virtual fuction in C++

This is a discussion on Virtual fuction in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi , What is the need of virtual function in C++? Here is my program: #include<iostream.h> class parent { public: ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Unhappy Virtual fuction in C++

    Hi ,

    What is the need of virtual function in C++? Here is my program:

    #include<iostream.h>

    class parent
    {
    public:
    virtual void display()
    {
    cout<<"Inside class parent"<<endl;
    }
    };

    class child1 : public parent
    {
    public:
    void display()
    {
    cout<<"Inside child1 "<<endl;
    }
    };

    class child2 : public parent
    {
    public:
    void display()
    {
    cout<<"Inside child2 "<<endl;
    }
    };

    void main()
    {
    child1 c1;
    child2 c2;

    parent* ptr;
    ptr = &c1;
    ptr->display();
    ptr = &c2;
    ptr->display()
    }

    In this program, if I take out the keyword "virtual" ,the output will be:
    Inside class parent
    Inside class parent

    But, by using c1,c2...I can access the child class method(ie display())...Then why we use the virtual function here??????

    Anybody can explain me???

    Thanks in advance
    Kokila.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    the virtual function makes certain that the compiler knows that this function shouldn't be inherited. if you don't put in "virtual" in at least the base class, your code's behavior is "undefined" on other compilers.

    in short: you got lucky(relatively speaking; real luck would be winning the lottery). you can't predict what other compilers will do. just add virtual. if anyone else maintains your code in the future, it will make the meaning much clearer.

  3. #3
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    Virtual functions are used to implement probably the most powerful tool in C++, polymorphism. This is the idea that a pointer becomes smart enough to be capable of changing its own associated type into the type of the object that it points to at or during runtime. Virtual functions exsist in the base class and in all derived classes with some modificaiton. Here is a simple example.

    Code:
     
    #include <iostream>
    using namespace std; 
    
    class Person
    { 
    private: 
    	int   age; 
    	float weight;
    public: 
    	Person(){age = 0; weight = 0.0;} 
    	
    	//Virtual functions that will be derived
    	//If the base class has nothing to be encapsulated 
    	//in derived objects then these functions would 
    	//be coded as pure virtual functions. 
    	//virtual void getData() = 0;
    
    	virtual void getData()
    	{
    		cout << "\nENTER";
    		cout << "\nAGE:";   cin >> age; 
    		cout << "\nWEIGHT:";cin >> weight; 
    	} 
    	virtual void showData()  
    	{ 
    		cout << "\nAGE:"    << age
    		     << "\nWEIGHT:" << weight;
    	} 
    }; 
    class Student : public Person 
    { 
    private: 
    	int   sch_year;
    	float salary; 
    public: 
    	Student(){sch_year = 0; salary = 0.0;}  
    
    	//Functions with modifications 
    	void getData() 
    	{ 
    		Person::getData();
    		cout << "\nYRS IN SCHOOL:"; cin >> sch_year;
    		cout << "\nPAY PER HR:";    cin >> salary; 
    	} 
    	void showData() 
    	{ 
    		Person::showData();
    		cout << "\nYEARS LEARNING:" << sch_year 
    		     << "\nSTUDENT SALARY:" << salary; 
    	} 
    }; 
    class Teacher : public Person 
    { 
    private: 
    	int tch_year; 
    	float salary; 
    public: 
    	Teacher(){tch_year = 0; salary = 0.0;}
    	
    	//Functions with modifications
    	void getData() 
    	{ 
    		Person::getData();
    		cout << "\nYRS TEACHING:";  cin >> tch_year;
    		cout << "\nSALARY PER HR:"; cin >> salary;
    	} 
    	void showData() 
    	{ 
    		Person::showData();
    		cout << "\nYEARS TEACHING:" << tch_year 
    		     << "\nTEACHER SALARY:" << salary; 
    	}
    }; 
    
    int main() 
    {
    	{ 
    	
    		Student stu_obj;//Declare a derived object 
    
    		Person *type;   //Declare a pointer to an 
    		                //object of the base class 
    	
    		
    		cout <<"\nSTUDENT DATA"; 
    		type = &stu_obj;//point that pointer somewhere else		
    		type->getData();/*Using late binding, the compiler can now tell at runtime
    		                  what type of object the pointer is pointing to and call the 
    						  correct function for that type of object.*/
    		type->showData();
    		
    		
    		
    		cout << "\n\nTEACHER DATA"; 	
    		type = new Teacher; //We now point to a different type of object
    		type->getData();   //again, the appropriate functions are called.  
    		type->showData(); 
    	} 
    	return 0; 
    }
    Last edited by DISGUISED; 05-13-2002 at 04:47 PM.

  4. #4
    Registered User OxYgEn-22's Avatar
    Join Date
    Apr 2002
    Posts
    36
    really, I though that virtuals were for being called like this..

    You call a student that is derived from a person class.. the virtual, lets say, destructor in the person class will let the student destructor of the derived class go first, then call itself?? or is this just to keep the right order of being called incase of a compiler that dosent support it?
    Is that air you're breathing?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 05:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 02:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

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