How could I call a base class virtual function from the derived class ? (C++)

In summary, the conversation is about a programmer who is trying to call a base class's function from a subclass, but is getting an error message from the compiler. After examining the code, it is discovered that there is a syntax error with the function call. The programmer thanks for the clarification and acknowledges making a rookie mistake.
  • #1
walnutTree
5
0
Dear All,
I have got a subclass (Panda), which inherits a Print function from the base class (Bear).
I'd like to be able to call the base class's function from the subclass.
But my complier gives me the following error message:

In member function `virtual void Panda::print(char)':
expected primary-expression before "char"


I use DEV-CPP.
Heres is my code causing the problem:

Code:
class Bear{
      public:
             virtual void print(char x)
             {
                 std::cout<<"bear"<<std::endl; 
             }
      };
class Panda : public Bear{
    public:
             void print(char x)
             {
                Bear::print(char x);
                std::cout<<"panda"<<std::endl;  
             }  
      };

Could you tell me what's the wrong?
Any ideas would be appreciated!
 
Technology news on Phys.org
  • #2
Look very closely at this line:
walnutTree said:
Code:
                Bear::print(char x);
It's a function call, so more like this:
Code:
                Bear::print(x);
If you're like me, you've slapped yourself in the forehead and groaned by now. :wink:
 
  • #3
thanks for clearing things up, it was a rookie mistake...
and the slapping took place as well:)
 

What is the purpose of calling a base class virtual function from a derived class?

The purpose of calling a base class virtual function from a derived class is to allow the derived class to access and modify the behavior of the base class function. This allows for more flexibility and customization in the derived class.

How do I call a base class virtual function from a derived class in C++?

To call a base class virtual function from a derived class in C++, you can use the scope resolution operator (::) to specify the name of the base class, followed by the name of the virtual function. This will call the base class function from within the derived class.

Can I override a base class virtual function in the derived class?

Yes, you can override a base class virtual function in the derived class by redefining the function with the same name and parameters in the derived class. This will replace the base class function with the new function in the derived class.

What happens if I do not call the base class virtual function from the derived class?

If you do not call the base class virtual function from the derived class, the base class function will still be executed. However, if the base class function is virtual, the derived class will not be able to modify its behavior and will only inherit the base class function's functionality.

Can I call a base class virtual function from a derived class constructor?

Yes, you can call a base class virtual function from a derived class constructor, as long as the base class function is not pure virtual. However, it is generally not recommended to call virtual functions from constructors as it can lead to unexpected behavior.

Similar threads

  • Programming and Computer Science
Replies
2
Views
667
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
6
Views
874
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
Replies
10
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
Back
Top