?
Solved

Virtual functions

Posted on 2003-11-01
6
Medium Priority
?
300 Views
Last Modified: 2013-11-18
I was under the impression that virtual function would not do this, perhaps someone will knwo why they do and how to get around it.

class A {
public:
virtual void foo() { cout << "A's foo"; }
}

class B : public A
{
void foo() { cout << "B's foo"; }
}

void test(A& bar)
{
  bar.foo();
}

this prints "A's foo". How do I get it to use B's foo?

Thanks,
-Sandra
0
Comment
Question by:Sandra-24
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9665106
This code fragment is not full. How do you call test function?
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9665108
Try this,

You did not terminate your class declarations with a semicolon and B's foo was declared as private so I'm not even sure why that compiled.

#include <iostream>
#include <iomanip>

using namespace std;

class A
{
      public:
            virtual void foo() { cout << "A's foo"; }
};

class B : public A
{
      public:
            void foo() { cout << "B's foo"; }
};

int main()
{
      B test;
      test.foo();

    return 0;
}

Cheers!
Exceter
0
 
LVL 30

Accepted Solution

by:
Axter earned 1000 total points
ID: 9665127
The following code does print out "B's foo"

class A
{
public:
      virtual void foo() { cout << "A's foo\n"; }
};

class B : public A
{
public:
      void foo() { cout << "B's foo\n"; }
};

void test(A& bar)
{
      bar.foo();
}


int main(int argc, char* argv[])
{
    B b_test;
    test(b_test);
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 30

Assisted Solution

by:Axter
Axter earned 1000 total points
ID: 9665131
However, the following code prints out "A's foo"

void test(A bar)
{
      bar.foo();
}


Are you sure your code is using a reference argument?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9665143
class A
{
public:
virtual void foo() { cout << "A's foo\n"; }
};

class B : public A
{
public:
void foo() { cout << "B's foo\n"; }
};

void test(A& bar)
{
 bar.foo();
}

int main(int argc, char* argv[])
{
    int i;
    A a;
    test(a);

    B b;
    test(b);

    return 0;
}

Result is:
A's foo
B's foo

However, this is not so interesting. Virtual functions are used for array of the class instances. You can create array of A and fill it with A abd B instances. Now you can call virtual function for each array element, and required function is called:

class A
{
public:
virtual void foo() { cout << "A's foo\n"; }
};

class B : public A
{
public:
void foo() { cout << "B's foo\n"; }
};


void test1(A* bar)
{
 bar->foo();
}

int main(int argc, char* argv[])
{
    int i;

    A* array[2];
    array[0] = new A();
    array[1] = new B();

    for ( i = 0; i < 2; i++ )
        test1(array[i]);

    for ( i = 0; i < 2; i++ )
        delete array[i];
   

    return 0;
}

Result is:
A's foo
B's foo


Make foo non-virtual and you will get
A's foo
A's foo

in the both cases.
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9666576
Thanks axter, that was it exactly. I passsed B into test() by value. Sorry guys about the bad code example, I was rushed when I posted.
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question