virtual keyword has nothing to do with return type of a method. It only tells that the following method is virtual (you can skip virtual in subclasses).
Try this:
#include <iostream>
class A
{
public:
A() {}
void nonVirt() { std::cerr << "A::nonVirt()" << std::endl; }
virtual void virt() { std::cerr << "A::virt()" << std::endl; }
virtual ~A() {}
};
class B : public A
{
public:
void nonVirt() { std::cerr << "B::nonVirt()" << std::endl; }
void virt() { std::cerr << "B::virt()" << std::endl; }
};
int main()
{
A a;
B b;
A *ptr = &b;
a.nonVirt();
a.virt();
b.nonVirt();
b.virt();
ptr->nonVirt();
ptr->virt();
return 0;
}
#include <iostream>
class A
{
public:
A() {}
void nonVirt() { std::cerr << "A::nonVirt()" << std::endl; }
virtual void virt() { std::cerr << "A::virt()" << std::endl; }
virtual ~A() {}
};
class B : public A
{
public:
void nonVirt() { std::cerr << "B::nonVirt()" << std::endl; }
void virt() { std::cerr << "B::virt()" << std::endl; }
};
int main()
{
A a;
B b;
A *ptr = &b;
a.nonVirt();
a.virt();
b.nonVirt();
b.virt();
ptr->nonVirt();
ptr->virt();
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks