0

I have recently learned about virtual functions and it is to my understanding that they are a keyword that goes in front of the return type when defining a function. They go in a base class, and make it so that if a member function of the base class is called with a base class type object, say b.display(), then the compiler will look in any derived classes for a definition of the function display() and if it fails to then just uses the base classes version of the function. This is what my textbook told me. However when I tested this out with this program it returned results that did not match my understanding. Here is the program:
Base_derived.h:

#include<iostream>
using namespace std;
class base
{
public:
	virtual int get_tag()
	{return 1;}
};
class derived: public base{
public:
	int get_tag()
	{return 2;}
};

main.cpp:

#include"derived.h"
#include<iostream>
using namespace std;
int main()
{

class derived d;
cout<<"d.get_tag():"<<d.get_tag()<<endl;
class base b;
cout<<"b.get_tag():"<<b.get_tag()<<endl;
system("pause");
return 0;
}

When the program ran it outputted:
"d.get_tag():2
b.get_tag():1"
I thought that b.get_tag() would return 2 because it is a virtual function. Thanks for any help.

4
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by nucleon
0

Your example is not quite correct. You mean to do this:

base* pb = &d;
    cout << pb->get_tag() << endl;

Simply declaring a base object and calling a method on it will never invoke a method of a derived object since the base object knows nothing about the derived object. It doesn't even know if there are any derived objects!

0

Virtual functions used to call on private variables from another class(in my understanding)

so if you have:

class one {

public:
one();
virtual void getone() {return data;}

private:
int data;

};

class two : public one {

public:
two();
virtual void getone() {return data;}
};

you would be able to access class one's variable data from class two.

0

While friend classes are similar, they also allow the manipulation of private variables from an inherited class, which is why virtual functions are used, for access.

0

Try this change and see , if you yet can't underestood the difference yet. and why we are using it.

#include"derived.h"
#include<iostream>
using namespace std;
int main()
{

class derived d;
cout<<"d.get_tag():"<<d.get_tag()<<endl;
class base& b= d;
cout<<"b.get_tag():"<<b.get_tag()<<endl;
system("pause");
return 0;
}
0

SeeTheLite: You are simply wrong here. Virtual functions are not used "for access" like friend functions; there is no similarity at all. Virtual functions are the basis of true object-oriented (as opposed to object-based) programming, so if you're confused here, you really need to study up.

0

SeeTheLite: I still can't make sense of your subtle concept. Can you give a (runnable) example of using a virtual function to access a base class's private data from a derived class?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.