What other reason apart from inheritance should a class need to have its functions as virtual?
What happens during run time where a base class is inherited and the derived class doesn't implement few of the base class function and a third class calls that undefined methods which are defined as virtual in base. seg fault or will it call the base class function?
What should I do if I don't want to define all the functions in my base class on my derived class but still have the necessary inheritance in place?
What other reason apart from inheritance should a class need to have
its functions as virtual?
There is no reasonable usage for having a virtual function, if you are not dealing with inheritance. Both are meant for each other.
What happens during run time where a base class is inherited and the
derived class doesn't implement few of the base class function and a
third class calls that undefined methods which are defined as virtual
in base. seg fault or will it call the base class function?
If Derived class don't make any declaration about the virtual function at all in its body, then (immediate) base class virtual functions are called with derived class object. On the other hand, if you simply declare virtual function in derived class but do not define it then it's a linker error. No segmentation fault.
What should I do if I don't want to define all the functions in my
base class on my derived class but still have the necessary
inheritance in place?
Though this is unclear, I would say, you simply don't declare/define virtual function (which you don't want) in derived class. It will use base class virtual functions.
If you do not reimplement a virtual method, a caller will call the base class one. This is sort of the point of using inheritance, IMO.
If you do not want a base class to implement a virtual method, you can declare it like this:
class Demo {
void foo() = 0;
};
This is what is called an abstract class. Note that you cannot create an instance of such a class. Any class which inherits from Demo must implement foo(), or it will also be an abstract class, and as such not instansiable.
Related
I want to call a derived class function that isn't defined in the base class using base class pointers. but this always raises the error:
error C2039: ‘derivedFunctionName’ : is not a member of ‘BaseClass’
Do you know how I could get around this?
Thanks,
You can't call a member that appears only in a derived class through a pointer to the base class; you have to cast it (probably using dynamic_cast) to a pointer to the derived type, first -- otherwise the compiler has no idea the method even exists.
It might look something like this:
void someMethod(Base* bp) {
Derived *dp = dynamic_cast<Derived*>(bp);
if (dp != null)
dp->methodInDerivedClass();
}
One way is to make the function virtual in the base class and then override it in the derived class. You don't have to define the function in the base class (although you can), but you can use the pure virtual syntax in the base class virtual void foo() = 0; if you don't want to provide an implementation in the base class.
This will let you override it in the derived class and still call it in the base class through a base class pointer. This is known as 'polymorphism'.
You can also just cast it to a derived type at run time if you are sure it is actually a derived class pointer at that time. If it is not, it will crash.
You can call a derived class member through a pointer to a base class as long as the method is virtual. That's what polymorphism is about.
In order for that to work you must declare a virtual method (probably pure virtual) in the base class and overload it in the derived class.
Note that you will run into issues calling the derived member from the base method. Do some ARM or Meyers reading if this doesn't make sense to you.
Note: I am not asking why a parent class need to have virtual method.
I remember something coding guideline like that(I don't remember well and just try to express as my own word,hope I have correct wording): if the parent class have virtual method,the derived class should also at least have one virtual method, even the derived class would not have derived class from itself, if the derived class really no need to have any virtual method, set destructor as virtual. The reason behind it is something like "prevent duplicate virtual table", what is it talking about?
And I also remember a related example like that:
class A{
virtual f();
};
class B : public A{
};
B as no derived class, if B has no virtual method, the consequence is something like "any place that include B will have a duplicate virtual table generated by compiler" (also is just expressed from my impression), what is the meaning of that (if it is true)?
Note the following:
If the base class has a virtual member function other than a destructor, the derived class inherits that function.
If the base class declares a member function as virtual, and the derived class declares a member function of the same name with the same argument types, then the derived class function is automatically virtual, and overrides the base class's function.
If the base class has a virtual destructor, the derived class's destructor is automatically virtual, whether user-declared or not, and overrides the base class's destructor.
The example you gave is not in any way problematic. I would advise you to forget everything you think you remember about this coding standard.
I want to call a derived class function that isn't defined in the base class using base class pointers. but this always raises the error:
error C2039: ‘derivedFunctionName’ : is not a member of ‘BaseClass’
Do you know how I could get around this?
Thanks,
You can't call a member that appears only in a derived class through a pointer to the base class; you have to cast it (probably using dynamic_cast) to a pointer to the derived type, first -- otherwise the compiler has no idea the method even exists.
It might look something like this:
void someMethod(Base* bp) {
Derived *dp = dynamic_cast<Derived*>(bp);
if (dp != null)
dp->methodInDerivedClass();
}
One way is to make the function virtual in the base class and then override it in the derived class. You don't have to define the function in the base class (although you can), but you can use the pure virtual syntax in the base class virtual void foo() = 0; if you don't want to provide an implementation in the base class.
This will let you override it in the derived class and still call it in the base class through a base class pointer. This is known as 'polymorphism'.
You can also just cast it to a derived type at run time if you are sure it is actually a derived class pointer at that time. If it is not, it will crash.
You can call a derived class member through a pointer to a base class as long as the method is virtual. That's what polymorphism is about.
In order for that to work you must declare a virtual method (probably pure virtual) in the base class and overload it in the derived class.
Note that you will run into issues calling the derived member from the base method. Do some ARM or Meyers reading if this doesn't make sense to you.
I'm now learning C++, the OO side, and I see this all the time:
class SomeClass{
virtual void aMethod()=0;
}
class AnotherClass{
void anotherMethod(){/*Empty*/}
}
class SomeClassSon : public SomeClass{
void aMethod(){/*Also Empty*/}
}
what is the difference between the 3 methods? The virtual equals zero, the empty one, and the virtual, since it is inherited, empty one.
Why can't I just make the SomeClassSon method like the father, virtual void equals zero?
For your
class SomeClass{
virtual void aMethod()=0;
}
the presence of a pure virtual method makes your class abstract. Once you have one such pure virtual method, =0, in your class, you cannot instantiate the class. What is more, any derived class must implement the pure virtual aMethod(), or it becomes an abstract class as well.
In your derived class, you overwrite the pure virtual method from above, and this makes the derived class non abstract. You can instantiate this derived class.
But, in derived class, method's body is empty, right? That's why your question makes sense: why not make the class pure virtual as well. Well, your class may entail other methods. If so, SomeClass cannot be instantiated (there is a pure virtual method), whereas child class SomeClassSon can be.
Same applies to your AnotherClass, which can be instantiated, contrary to SomeClass.
The difference is that virtual void aMethod() = 0 is a pure virtual function, meaning that:
SomeClass becomes an abstract base class,
meaning it cannot be instantiated.
Any class which inherits from SomeClass must implement aMethod, or it too becomes an abstract base class which cannot be instantiated
Note that any class with one or more pure virtual functions is automatically an abstract base class.
The "equals 0" you're referring to is called "pure virtual". It's a function that the child that wants to be instantiated HAS to implement as opposed to providing base functionality meaning that the parent class is going to define functionality that has to exist but that the parent has no knowledge of how the child will do it. Note that this makes the class abstract in that it cannot be instantiated. For example I may want to define a "Mammal" class I can inherit from and I want its children to act a certain way - but I can't simply make a "Mammal". Instead I would create a "Giraffe" class and make sure it acts like it's supposed to.
It's also explained at this SO question.
The "Empty" function you're referring to is instead functionality where the function is defined and can be called - but does nothing.
the declaration aMethod()=0 tells the compiler that this method must be provided for in subclasses. Any subclass that does not implement the method can not be instantiated. This helps you ensure any objects of the base class will have the method implemented.
A pure virtual function (your first example, with the =0) means that function must be overridden in a derived class for an object of that class to be instantiated.
The second is basically just a member function that does nothing. Since the function has a different name and the class isn't related to SomeClass, the two don't affect each other at all.
The third overrides the pure virtual function, so it's possible to instantiate SomeClassSon, but in the derived class the overridden function does nothing.
A pure virtual makes the class abstract. An empty non-virtual method doesn't do anything - it just leads to a linker error if you attempt to call it. By contrast, you can't attempt to call a pure virtual (unless you attempt to call it from a constructor, which is bad anyway) because the compiler won't let you create that object.
There's also a logical difference - the method marked virtual will be virtual through the inheritance chain - the others are just regular methods.
Do you have to declare methods replacing a pure virtual function in a base class? If so, why? Because the base class has declared the methods as pure virtual, and therefore MUST exist in derived class, then is should not be necessary to redeclare them in the derived class before you can implement them outside of the class definition. Wouldn't you agree?
Yes you have.
The reason for this is to let the compiler know that the virtual method is being implemented by the derived class since a derived class can also be abstract and have virtual methods. Since compilation units are compiled separately, the compiler would otherwise not know whether a virtual method is implemented by a derived class when compiling another compilation unit that uses the derived calss and thus whether it is an abstract class or not.
You don't have to override the declaration of the base class. If you don't, the derived class is simply abstract as well.
So, the declaration in the derived class serves a definite purpose, and you need it.
The class definition as the name says is the declaration of the class. If you are not declaring derived function (from the pure virtual one) compiler will not find it.
You have to derived the pure virtual functions only if your class will be instantiated. You can have the pure virtual function a class A, a class B that inherits from class A but not have derived functions and then a class C that inherits from class B with reimplementation of derived function. In that case only class C can be instantiated, so why compiler should add declaration of derived class in class B? That's why you have to indicate to compiler which class is reimplementing pure virtual functions.