Questions tagged [virtual-functions]

In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).

The concept of the virtual function solves the following problem:

In OOP when a derived class inherits a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class methods overridden by the derived class, the method call behavior is ambiguous.

The distinction between virtual and non-virtual resolves this ambiguity. If the function in question is designated virtual in the base class then the derived class' function would be called (if it exists). If it is not virtual, the base class' function would be called.

Virtual functions overcome the problems with the type-field solution by allowing the programmer to declare functions in a base class that can be redefined in each derived class.

In C++ virtual methods are declared by prepending the virtual keyword to the function's declaration.

Source: Wikipedia (Virtual function)

1509 questions
-2
votes
2 answers

Do derived classes need their own version of a virtual function declared on base?

For instance, the class Plant has a virtual void info() method. The class flower derives from Plant. Is Plant obligated to have its own implementation of the method?
Miguel Mano
  • 103
  • 1
  • 12
-2
votes
1 answer

What is faster in C#, an if statement, or a virtual function call to a function that does nothing? And why?

Say you have a class called MyClass which has a certain behavior encapsulated using a reference (myBehavior) to another class called Behavior. By default MyClass should have no behavior. For instances which hadn't set myBehavior to anything after…
-2
votes
2 answers

Could i use virtual class functions but specify them outside class definitions?

I do not know if this is possible or even if it should be done. Can i keep my .hpp files strictly interface without including source when using a base class with virtual functions? Here is the learning/test code I would like to change to work this…
-2
votes
2 answers

How to access Constructor of Parent class in C++ with parent class having a pure virtual function

I am trying to create Circle and Rectangle from Class Shape. I want y to be assigned pi if I call Shape() constructor with a parameter (from circle class). Since Shape has a purely virtual function the compiler is showing error. How can I overcome…
Yash Jain
  • 1,091
  • 1
  • 8
  • 9
-2
votes
2 answers

Weird behaviour of compiler

I have simple program #include using namespace std; struct A { A(){} void print(int i) { cout << "A::print: " << i << endl; } }; struct B : public A { B() : A() {} virtual void print(int i)…
Alex Aparin
  • 4,393
  • 5
  • 25
  • 51
-2
votes
1 answer

Hack the virtual table of a C++ object without a reference

I know this question was asked here before but it is not exactly what I need, and as much as I try to manipulate the answers from this thread I cannot get the result I'm looking for. So basically what I want to do is manipulate the virtual table in…
Lior Naar
  • 85
  • 1
  • 9
-2
votes
1 answer

Undefined reference to "vtable"

Here is my program. It has a base class, Point, a class colored_point inherited from Point, and a class dim3_point inherited from colored_point. In the class Point there is a virtual function Initializer() and there is one in other classes as well.…
Dang Manh Truong
  • 795
  • 2
  • 10
  • 35
-2
votes
2 answers

Derived Class Calling Non-Public Base Class Virtual Function

EDITED: This question has already been asked here but didn't help in my case. I'm trying to have a hierarchy of classes, with inherited public update() functions. But I want a given derived derived class to call the functionality of all of its…
Rabadash8820
  • 2,328
  • 3
  • 27
  • 49
-2
votes
2 answers

Upcasting and polymorphism in C++

Could you please help me to understand why do I get a following output? Z The code is listed below: #include using namespace std; class X { public: virtual void shout() { cout << "X"; } }; class Y : public X { public: void…
Diesel DL
  • 83
  • 1
  • 1
  • 5
-2
votes
1 answer

Implementing virtual functions without the virtual keyword

My question is as follows: I want (if it is possible) to "implement" virtual functions without using the "virtual" keyword (out of curiosity - I know the trick with functions pointers in C , I wish to do that in C++ !!). My "idea" is to have a flag…
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
-2
votes
1 answer

Explain the error

below is a c++ code : #include using namespace std; class Base { public: virtual int f() const { cout << "Base::f()\n"; } virtual void f(string) const {} virtual void g() const {} }; class…
akash
  • 1,801
  • 7
  • 24
  • 42
-3
votes
0 answers

Calling pure virtual method is jumping to wrong functions

In below scenario for class xyz I am facing some issue when I call method2 and keep breakpoint to called place at method1 its jumping to the middle method1 having the extra std::optional argument class xyz { public: virtual int method(int, int)…
user2621476
  • 93
  • 1
  • 8
-3
votes
1 answer

"undefined reference to `____: :___`" error for pure virtual functions when I seperate my files into cpp and header files

So I have the Book class that inherits from Publication. Publication has two pure virtual functions which I implemented in Book. The code worked before I tried seperating into header and cpp files classes.h #include #include…
Saif eldeen Adel
  • 318
  • 3
  • 15
-3
votes
1 answer

function return pointer to parent class from child class

i have this issue but i can't solve it. I only know the InitData function is 100% correct. However i try to find how need to create my classes to make this code work (without do any change to the InitData function) class Bird { public: int…
mod Zz
  • 49
  • 6
-3
votes
1 answer

Is it acceptable for a derived function to not make use of all available parameters?

I have an overridden member function that does not make use of all the parameters it is passed? Does this indicate a design flaw?
pingu
  • 8,719
  • 12
  • 50
  • 84
1 2 3
99
100