Solved

Virtual functions

Posted on 2003-11-01
6
287 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

VM backup deduplication is a method of reducing the amount of storage space needed to save VM backups. In most organizations, VMs contain many duplicate copies of data, such as VMs deployed from the same template, VMs with the same OS, or VMs that h…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now