473,395 Members | 1,938 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,395 software developers and data experts.

Can we use a virtual function in the class in which the virtual function is defined?

Expand|Select|Wrap|Line Numbers
  1. class Parent{
  2.   public:
  3.     virtual int width();
  4.     virtual int height();
  5.     int area(){return width()*height();};
  6.     //Virtual functions are used here to define another function, but this produces an error.  
  7. //Is is possible to do something like this? 
  8. };
  9.  
  10. class Child: public Parent{
  11.   int w;
  12.   int h;
  13.   public:
  14.     Child(int a,int b){w=a;h=b;};
  15.     int width(){return w;}
  16.     int height(){return h;}
  17. };
Dec 27 '12 #1
10 2872
Hi,

You have a semicolon at the end of this line:

Expand|Select|Wrap|Line Numbers
  1. int area(){return width()*height();};
You don't need the last semicolon here. Remove it. The problem is with this semicolon and not with your virtual functions.
Dec 27 '12 #2
weaknessforcats
9,208 Expert Mod 8TB
You may not understand the purpose of a virtual function.

A virtual function is used when the derived portion of an object is accessed from a pointer to the base portion of the object. This is what allows polymorphism in C++.

Therefore, you are not allowed to call a virtual function using an object of the class that defines it.

You can, however, call a base class virtual function from a member function in a derved object:

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3.   public:
  4.     virtual void Funct();
  5. };
  6. class Derived :public Base
  7. {
  8.    public:
  9.      virtual void Funct();
  10. };
  11. void Derived::Funct()
  12. {
  13.     Base::Funct();  //OK
  14. }
  15.  
Dec 27 '12 #3
Thanks for your reply, but I already knew what you just said.
Dec 27 '12 #4
I don't think this is the problem.
Dec 27 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
It may be. You are calling derived class functions in the base class.

virtual functions in the base class define an interface to the hierarchy. You make your calls in the derived class. By placing these functions in the base class you force every derived class to support them. Some derived classes may not have length or width so keep the derived functions with the derived class.

This is a place to review the Visitor design pattern. This allows you to acquire a Child* from an object pointed at by a Parent*. This then allows you to call Child functions on Child objects that only have Parent*.

Read this: http://bytes.com/topic/c/insights/67...tterns-visitor
Dec 27 '12 #6
I think I am going to use a template function to do this.
Expand|Select|Wrap|Line Numbers
  1. class Parent{
  2.   public:
  3.     virtual int width();
  4.     virtual int height();
  5.  
  6. };
  7.  
  8. class Child: public Parent{
  9.   int w;
  10.   int h;
  11.   public:
  12.     Child(int a,int b){w=a;h=b;};
  13.     int width(){return w;}
  14.     int height(){return h;}
  15. };
  16. template <class T> int area(T){return T.width()*T.height();};
But it's not exactly what I wanted though.
Dec 28 '12 #7
weaknessforcats
9,208 Expert Mod 8TB
You need code like this:

Expand|Select|Wrap|Line Numbers
  1. Parent* p = new Child(3,4);
  2.  
  3. Child* c = p->VisitChild();
  4.  
  5. cout << c->area();
and not a template.
Dec 28 '12 #8
Expand|Select|Wrap|Line Numbers
  1. Parent* p = new Child(3,4);
  2.  
  3. Child* c = p->VisitChild();//Could you explain in a little bit more detail please? VisitChild() appears not defined. 
  4.  
  5. cout << c->area();
Dec 29 '12 #9
changed, and worked, though still not exactly what I wanted.
Expand|Select|Wrap|Line Numbers
  1. lass Parent{
  2.   public:
  3.     virtual int width();
  4.     virtual int height();
  5.  
  6. };
  7.  
  8. class Child: public Parent{
  9.   int w;
  10.   int h;
  11.   public:
  12.     Child(int a,int b){w=a;h=b;};
  13.     int width(){return w;}
  14.     int height(){return h;}
  15. };
  16. int area(Parent p){return p.width()*p.height();};
Dec 29 '12 #10
weaknessforcats
9,208 Expert Mod 8TB
I meant that as a directional example and not a compilable one. Sorry.

A working example is in the article I gave you a link to my Post # 8 above.
Dec 29 '12 #11

Sign in to post your reply or Sign up for a free account.

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 ;
2
by: IK | last post by:
Hello All, Please excuse me for posting this here, but I don't find any other group where I will get a proper answer. This is about clarifying the C++ part of COM. I understand that COM is a...
14
by: mshetty | last post by:
Hi, I get an error "Warning: b::a_method hides the virtual function a::a_method()." on compiling the following code.. #include <iostream.h> class a {
4
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real...
4
by: pocmatos | last post by:
Hi all, I have an abstract class acting as interface to a given class of objects. And mostly everywhere around my program I'm passing things like: vector<int>, list<unsigned long>,...
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
3
by: a | last post by:
Hi, I need clarification for virtual method and pure virtual method. e.g Class Base{ virtual void func(){ ---- } } Class Child : public Base{ void func()
2
by: subramanian100in | last post by:
consider the following program #include <iostream> using namespace std; class Rec { public: Rec(int arg = 10) : val(arg) { }
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.