Solved

Virtual functions

Posted on 2003-11-01
6
294 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 250 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Assisted Solution

by:Axter
Axter earned 250 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Create your own, high-performance VM backup appliance by installing NAKIVO Backup & Replication directly onto a Synology NAS!
Microsoft will be releasing the Windows 10 Creators Update in just a matter of weeks. Are you prepared? Follow these steps to ensure everything goes smoothly and you don't lose valuable data on your PC.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

730 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