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
-3
votes
2 answers

C++ how to avoid overriding virtual method from interface

I came from JVM world and I try to implement something in c++. I have an interface: class MyInterface { public: virtual void my_method(std::string i) = 0; virtual void my_method(int i) = 0; }; And I would like to have two…
user
  • 4,410
  • 16
  • 57
  • 83
-3
votes
1 answer

C++ derivative class with virtual function

I have a problem with some task. I need to write an derived class in which I need to be sure, that vector FVect cointains only characters <'a'; 'z'>. class Something { private: char FVect[3]; protected: virtual void setValue(int _idx, char…
-3
votes
1 answer

GNU Cim (Simula preprocessor): How to use INNER in a PROCEDURE declared VIRTUAL?

Simula (back then, Simula 67) introduced virtual procedures (amongst a lot of other things). In contrast to practice in several later languages, a derived class would not completely "override" an ancestor's definition, but be allowed to contribute…
greybeard
  • 2,249
  • 8
  • 30
  • 66
-3
votes
1 answer

"cannot declare abstract type variable because of virtual function" error despite the virtual function being defined/implemented

I'm writing code using inheritance, with a base class called BankAccount and a child class called MoneyMarket Account, and I'm receiving the following error for my code hw7main.cpp: In function ‘int main()’: hw7main.cpp:9: error: cannot declare…
christina
  • 29
  • 2
-3
votes
1 answer

virtual functions with a non-void return type

Consider the following inheritance and composition scheme. // Example program #include #include using namespace std; class TapProcessing { public: TapProcessing(){}; virtual ~TapProcessing(){}; …
Naveen
  • 458
  • 1
  • 10
  • 29
-3
votes
3 answers

What type does the 'this' keyword actually refer to?

I have the following code public class Base { public Base() {} public virtual void IdentifyYourself() { Debug.Log("I am a base"); } public void Identify() { this.IdentifyYourself(); } } public class Derived : Base { …
AMemberofDollars
  • 331
  • 2
  • 14
-3
votes
1 answer

Object Hierarchy employee program - dereferencing pointer for cout

I am new to c++ and am working on a project for class. I know I that some of my functions are not correct. I am trying to get to a point to where I can at least see the output to continue working on it. I have included a brief description of that I…
K455306
  • 119
  • 1
  • 1
  • 10
-3
votes
1 answer

How to call virtual function for all created objects which are inherited from the one? C++

For example, I have three classes: A, B::A and C::A, only B and C have virtual method print(), like that: #include using namespace std; class A { public: virtual void print() { return; //do nothing } static…
L. Antz
  • 19
-3
votes
3 answers

Why can't we call virtual function in base class using object of base class in case the virtual function is not overridden in child class?

I have an example code snippet: class A { public: virtual void func1(); virtual void func2(); }; class B { public: virtual void func2(); virtual void func3(); }; void main() { A *obj = new B; obj->func3(); } Why does the…
-3
votes
4 answers

C++ Practice Vehicle Program Issues - Beginner Programming

I have made a little snippet of what my project is essentially based on - except much bigger. I am having a hard to grasping this particular concept. I need to make a car class which inherits the vehicle class and then there must be separate classes…
Mike Blair
  • 1,821
  • 2
  • 11
  • 11
-3
votes
3 answers

Call to virtual function cause Segmentation Fault in C++

I am having a problem with a simple math analysis tool I am developing in C++, specifically the program cause Segmentation Fault at a certain point during execution. Here is a simplified version of the code: #include using namespace…
-3
votes
2 answers

Not implementing a virtual function

I wanted to ask, is there a problem with not implementing a virtual function? For example: class Function { public: virtual ~Function() {} virtual double value(double x) const = 0; virtual Function* clone() const = 0; protected: …
Dan Dv
  • 473
  • 3
  • 12
-3
votes
2 answers

unwanted method called

Lets see a basic class: class A { public function renderSomethingRecursive() { //this function can call itself self::renderSomethingRecursive(); // ERROR!!!! } abstract public function addSomething (value); } class B…
John Smith
  • 6,129
  • 12
  • 68
  • 123
-4
votes
2 answers

Manually calling a function from VTable using pointers creates segmentation fault

When manually calling the second() function I get a segmentation error. I think the problem is that along the way the program loses info dealing with where the first() function is in the memory for the constructed D class when moving the adress…
-4
votes
4 answers

How to define a static function inherited from a virtual one?

I am using C++ for a little application. I have one parent class that has a virtual method. Here it's the header file. class personDB:public person { public: unsigned int id; public: personDB(); personDB(QString dbName, QString…
roschach
  • 8,390
  • 14
  • 74
  • 124
1 2 3
99
100