virtual function

access the virtual function of the base class

Page 1 of 1

4 Replies - 782 Views - Last Post: 02 March 2009 - 08:20 AM Rate Topic: ----- 1 Votes

#1 monutiwari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-March 09

virtual function

Posted 01 March 2009 - 08:43 PM

  
#include<iostream.h>
#include<conio.h>
int i;
class worker
{
public:
int wid[2],did[2],sid[2];
char name[2][10];
char address[2][20];
void input()
{
for(i=0;i<2;i++)
{
cout<<"Enter the name:";
cin>>name[i];
cout<<"Enter the worker id:";
cin>>wid[i];
cout<<"Enter the department id:";
cin>>did[i];
cout<<"Enter the supervisor id:";
cin>>sid[i];
cout<<"Enter the address:";
cin>>address[i];
}
}
virtual void disp()
{
for(i=0;i<2;i++)
{
cout<<"The name of employee is:"<<name[i]<<endl;
cout<<"The worker id of emloyee is :"<<wid[i]<<endl;
cout<<"The supervisor id of the employee is :"<<sid[i]<<endl;
}
}
};
class super:public worker
{
public:
void disp()
{
for(i=0;i<2;i++)
{
cout<<"The name of the supervisor is :"<<name[i]<<endl;
cout<<"The department of the supervisor is:"<<did[i]<<endl;
}
}
};
void main()
{
worker *obj, obj1;
super obj2;
obj=&obj1;
obj=&obj2;
obj->input();
char ch;
while(ch!='0')
{
cout<<"Enter your choice :";
cout<<"1 For worker\n2 For super\n";
ch=getch();
if(ch=='1')
{
obj->disp();
}
else if(ch=='2')
{
obj=&obj2;
obj->disp();
}
else
{
cout<<"Wrong choice:";
}
}
getch();
}



Is This A Good Question/Topic? 0
  • +

Replies To: virtual function

#2 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 1,005
  • Joined: 30-July 08

Re: virtual function

Posted 01 March 2009 - 10:32 PM

The problem is following ...
/* Correct code here */

Now you know how to fix it.

No but really... Just showing code is not enough. If you want help you need to put some effort in it. Tell us whats wrong, and show any error messages you get.
Was This Post Helpful? 0
  • +
  • -

#3 beautyofjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-February 09

Re: virtual function

Posted 02 March 2009 - 12:25 AM

im assuming that u need to access the information of the display function in the base class from the inherited class,
thats the only reason you would use inheritance for and since u have used virtual in the base class
you have to tell the function in the derived class that its virtual as well

so go and write virtual front of void display () in the inherited class
and this should solve ur problem

if you dont write virtual front of it , it wont know that it has to access the function in the base class i hope this help
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: virtual function

Posted 02 March 2009 - 07:33 AM

The virtual function should not have anything in it (in the base class that is). It is up for the derived classes to implement their version of the virtual function.
Was This Post Helpful? 0
  • +
  • -

#5 abcd123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-March 09

Re: virtual function

Posted 02 March 2009 - 08:20 AM

View Postmonutiwari, on 1 Mar, 2009 - 07:43 PM, said:

  
#include<iostream.h>
#include<conio.h>
int i;
class worker
{
public:
int wid[2],did[2],sid[2];
char name[2][10];
char address[2][20];
void input()
{
for(i=0;i<2;i++)
{
cout<<"Enter the name:";
cin>>name[i];
cout<<"Enter the worker id:";
cin>>wid[i];
cout<<"Enter the department id:";
cin>>did[i];
cout<<"Enter the supervisor id:";
cin>>sid[i];
cout<<"Enter the address:";
cin>>address[i];
}
}
virtual void disp()
{
for(i=0;i<2;i++)
{
cout<<"The name of employee is:"<<name[i]<<endl;
cout<<"The worker id of emloyee is :"<<wid[i]<<endl;
cout<<"The supervisor id of the employee is :"<<sid[i]<<endl;
}
}
};
class super:public worker
{
public:
void disp()
{
for(i=0;i<2;i++)
{
cout<<"The name of the supervisor is :"<<name[i]<<endl;
cout<<"The department of the supervisor is:"<<did[i]<<endl;
}
}
};
void main()
{
worker *obj, obj1;
super obj2;
obj=&obj1;
obj=&obj2;
obj->input();
char ch;
while(ch!='0')
{
cout<<"Enter your choice :";
cout<<"1 For worker\n2 For super\n";
ch=getch();
if(ch=='1')
{
obj->disp();
}
else if(ch=='2')
{
obj=&obj2;
obj->disp();
}
else
{
cout<<"Wrong choice:";
}
}
getch();
}


Dear My frnd

What u wrote that wont work..

Regards
Sunny
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1