473,785 Members | 3,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3312
"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::a rea' 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.********@com Acast.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::a rea' 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<De rived*>( Inst )->Bar() << "\n"; // Won't
compile
std::cout << dynamic_cast<De rived*>( 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<De rived*>( 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<De rived*>( 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
2350
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
3307
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
1340
by: cppsks | last post by:
Consider the following: class BaseInterface { public: virtual void something() = 0; }; class AbstractSubclass : public BaseInterface {
5
2614
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 classes yClass which inherits xClass. xClass has a virtual method which simply writes a line of text stating its origin, yClass implements the same method which writes a line of text stating its origin also (i.e. "From yClass"). I ran the...
2
2515
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 pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
10
2271
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: Selections();
6
2274
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
1850
by: Ashwin | last post by:
hi guys, can anyone explain this class Base { public: virtual void foo() = 0; virtual void bar() = 0; };
17
3549
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;" instead? I think declaring a function as "=0" is the same
4
180
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
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.