virtual function declaration

N

newbie

class AbstractBox {
public:
virtual double area() const = 0;
}

class BoxA : public AbstractBox {
virtual double area() { return 0.1; }
}

class BoxB : public AbstractBox {
double area() { return 0.2; }
}

Very dump example, but I hope you get my question: does the 'virtual'
keyword in BoxA take any effect? I don't think so, but I do see code
like BoxA, but not like BoxB.

Thanks
 
O

osmium

newbie said:
class AbstractBox {
public:
virtual double area() const = 0;
}

class BoxA : public AbstractBox {
virtual double area() { return 0.1; }
}

class BoxB : public AbstractBox {
double area() { return 0.2; }
}

Very dump example, but I hope you get my question: does the 'virtual'
keyword in BoxA take any effect? I don't think so, but I do see code
like BoxA, but not like BoxB.

It has no effect, once virtual always virtual. I see it simply as good
manners for a coder to pass on what information he already knows to the poor
slob who must maintain this nightmare he has come up with.
 
P

pmouse

Virtual is only used on base classes, i.e., classes that might be
inherited by others. There is a huge difference in this case, but none
in yours. (Suppose BoxB is inherited from BoxA, then yes, virtual
should be declared on A, otherwise it's just a good manner to keep the
class extend-able)

Regards,

PQ
 
R

Richard Powell

Virtual is only used on base classes, i.e., classes that might be
inherited by others. There is a huge difference in this case, but none
in yours. (Suppose BoxB is inherited from BoxA, then yes, virtual
should be declared on A, otherwise it's just a good manner to keep the
class extend-able)

So if a function is virtual in the base class, and an inherited class
has the same function, then the inherited function is virtual too.


But what if the function overloads the arguments, is it still virtual:

class AbstractBox {
public:
virtual double area() const = 0;
};

class BoxA : public AbstractBox {
virtual double area() { return 0.1; }
};

class BoxB : public AbstractBox {
double area() { return 0.2; } /* this is virtual too */
};

class BoxC : public AbstractBox {
double area(int a) { return a*0.2; } /* is this virtual too? */
};
 
V

Victor Bazarov

Richard said:
So if a function is virtual in the base class, and an inherited class
has the same function, then the inherited function is virtual too.


But what if the function overloads the arguments, is it still virtual:

class AbstractBox {
public:
virtual double area() const = 0;
};

class BoxA : public AbstractBox {
virtual double area() { return 0.1; }

This function does not override 'area' in the base class. It hides
the other function since the signature is different. And since the
'AbstractBox::area' is pure, this class (and others) are still
abstract.
};

class BoxB : public AbstractBox {
double area() { return 0.2; } /* this is virtual too */

No, it isn't virtual. The signature of this function is not the
same as 'area' in the base class 'AbstractBox'. 'const' is missing.
};

class BoxC : public AbstractBox {
double area(int a) { return a*0.2; } /* is this virtual too? */

No, it's not, for the same reason: the signature is different.

V
 
J

Jim Langston

Victor Bazarov said:
This function does not override 'area' in the base class. It hides
the other function since the signature is different. And since the
'AbstractBox::area' is pure, this class (and others) are still
abstract.


No, it isn't virtual. The signature of this function is not the
same as 'area' in the base class 'AbstractBox'. 'const' is missing.


No, it's not, for the same reason: the signature is different.

The compilation of this test program did throw some suprises for me.

#include <iostream>

class Base
{
public:
virtual ~Base() {}
virtual int Foo() { return 1; }
virtual int Bar() { return 2; }
};

class Derived: public Base
{
public:
int Foo() { return 10; }
int Bar( int x ) { return x; }
};

int main()
{
Base* Inst = new Derived;
std::cout << Inst->Foo() << "\n";
std::cout << Inst->Bar() << "\n";
std::cout << Inst->Bar( 20 ) << "\n"; // Won't compile
std::cout << dynamic_cast<Derived*>( Inst )->Bar() << "\n"; // Won't
compile
std::cout << dynamic_cast<Derived*>( Inst )->Bar( 20 ) << "\n";
}
 
T

Thomas J. Gritzan

Jim said:
The compilation of this test program did throw some suprises for me.

#include <iostream>

class Base
{
public:
virtual ~Base() {}
virtual int Foo() { return 1; }
virtual int Bar() { return 2; }
};

class Derived: public Base
{
public:
int Foo() { return 10; }
int Bar( int x ) { return x; }
};

int main()
{
Base* Inst = new Derived;
std::cout << Inst->Foo() << "\n";
std::cout << Inst->Bar() << "\n";
std::cout << Inst->Bar( 20 ) << "\n"; // Won't compile

Inst is a Base* and Base doesn't have a function Bar(int).
std::cout << dynamic_cast<Derived*>( Inst )->Bar() << "\n"; // Won't
compile

You try to call Bar(void) on a Derived* but the declaration of Bar(int)
shadows Bar(void) from the base class. You can:

1) override Bar(void) in Derived -or-
2) insert "using Base::Bar;" in Derived to make the Bar(void) function part
of the Derived class too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,242
Latest member
BarbMott55

Latest Threads

Top