473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

virtual function declaration

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

May 2 '07 #1
6 3286
"newbie" writes:
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.
May 2 '07 #2
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

On May 1, 9:11 pm, newbie <mitbb...@yahoo.comwrote:
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

May 2 '07 #3
On May 1, 9:26 pm, pmouse <pmo...@cogeco.cawrote:
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? */
};

May 2 '07 #4
Richard Powell wrote:
On May 1, 9:26 pm, pmouse <pmo...@cogeco.cawrote:
>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; }
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 2 '07 #5
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f1**********@news.datemas.de...
Richard Powell wrote:
>On May 1, 9:26 pm, pmouse <pmo...@cogeco.cawrote:
>>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; }

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.
>};
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";
}
May 6 '07 #6
Jim Langston wrote:
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.
std::cout << dynamic_cast<Derived*>( Inst )->Bar( 20 ) << "\n";
}
--
Thomas
http://www.netmeister.org/news/learn2quote.html
May 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
6
by: cppsks | last post by:
Consider the following: class BaseInterface { public: virtual void something() = 0; }; class AbstractSubclass : public BaseInterface {
5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
10
by: mark | last post by:
I have this class: class Selections { OSStatus Init(); protected: CFMutableSetRef selectionObjects; static void CFASelectionsApplier(const void* value, void* ctx); OSType ready; public:...
6
by: Ryan H. | last post by:
A very simple example of something that acts quite odd: class TopClass { public: TopClass(){ } virtual ~TopClass(){ } virtual int myFunc() = 0; };
2
by: Ashwin | last post by:
hi guys, can anyone explain this class Base { public: virtual void foo() = 0; virtual void bar() = 0; };
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
4
by: paul | last post by:
I was trying out the following sample code from Scotty Meyers: Effective C++ book: #include <iostream> class B { public: virtual void f() const { std::cout << "B::f()" << std::endl; } };
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.