Solved

Virtual functions

Posted on 2003-11-01
6
290 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Create your own, high-performance VM backup appliance by installing NAKIVO Backup & Replication directly onto a Synology NAS!
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 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