virtual function problem

does the calling of a virtual function also depend on the return type

Page 1 of 1

6 Replies - 1288 Views - Last Post: 18 August 2010 - 07:26 PM Rate Topic: -----

#1 tehreem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-January 10

virtual function problem

Posted 18 August 2010 - 05:29 PM

it is known that virual function concept is applied that a call on the pointer of the base class
which points to an object of derived to a function,if made virtual although pointer is of base class
calls the function of derived class.But if the derived class has a function named the same as the base class function made virtual but the return type is different,does this make a differece or not?
Is This A Good Question/Topic? 0
  • +

Replies To: virtual function problem

#2 KYA  Icon User is offline

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

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: virtual function problem

Posted 18 August 2010 - 05:37 PM

Have you wrote some code that illustrates this scenario and run it?
Was This Post Helpful? 0
  • +
  • -

#3 tehreem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-January 10

Re: virtual function problem

Posted 18 August 2010 - 06:59 PM

View PostKYA, on 18 August 2010 - 04:37 PM, said:

Have you wrote some code that illustrates this scenario and run it?


sir this was a question in my exam,my teacher said in the class it is based on return type,but in exam
she didn't mark it saying that it return type doesn't matter,while ryt now on u askin i coded it nd
it doesn't work givin an error

Info :Compiling C:\Documents and Settings\Tehreem\Desktop\myspace\foo.cpp
Warn : foo.cpp(12,3):Functions containing missing return statements are not expanded inline
Error: foo.cpp(17,2):Virtual function 'dfoo::func()' conflicts with base class 'foo'
Warn : foo.cpp(19,3):Functions containing missing return statements are not expanded inline
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

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

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: virtual function problem

Posted 18 August 2010 - 07:11 PM

Post what you attempted to compile please.
Was This Post Helpful? 0
  • +
  • -

#5 tehreem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-January 10

Re: virtual function problem

Posted 18 August 2010 - 07:17 PM

View PostKYA, on 18 August 2010 - 06:11 PM, said:

Post what you attempted to compile please.


#include <iostream.h>
#include <conio.h>

class foo
{

public:
virtual int func()
{
cout<<"\nbase";
return 3;
}
};
class dfoo:public foo
{
float func()
{
cout<<"\nderived";
return 4.5;
}
};

void main()
{
clrscr();
int a;
dfoo obj1;
foo* ptrb;
ptrb=&obj1;
a=(*ptrb).func();
cout<<"this is the answer"<<a;
getch();
}


nd the error is:
Error: foo.cpp(18,2):Virtual function 'dfoo::func()' conflicts with base class 'foo'
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

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

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: virtual function problem

Posted 18 August 2010 - 07:22 PM

Method signatures need to match otherwise what's the point? Just overload.
Was This Post Helpful? 1
  • +
  • -

#7 tehreem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-January 10

Re: virtual function problem

Posted 18 August 2010 - 07:26 PM

thanx alot dat means it does depend on return type,i was toping the course but better late than never :P
thnx alot! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1