Sandra-24
asked on
Virtual functions
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
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
This code fragment is not full. How do you call test function?
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
{
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.
ASKER
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.