Solved

Virtual functions

Posted on 2003-11-01
6
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

By default, Carbonite Server Backup manages your encryption key for you using Advanced Encryption Standard (AES) 128-bit encryption. If you choose to manage your private encryption key, your backups will be encrypted using AES 256-bit encryption.
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

690 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