Let's say I have a base class called Human
. Baby
and Adult
are sub-classes of Human
. Human
has a function run
as a pure virtual function and I don't want Baby
to inherit run
but if I don't override run
, Baby
would become an abstract class.
class Human{
public:
// Some human attributes.
virtual void run()=0;
};
class Adult: public Human{
public:
void run(){
// Adult running function
}
};
class Baby: public Human{
public:
void run()=delete; //error
// If I don't override `run` then Baby is marked as an abstract class
void crawl(){
// Baby crawling function.
}
};
Error if I mark run
with =delete
prog.cpp:19:10: error: deleted function ‘virtual void Baby::run()’
void run()=delete;
^~~
prog.cpp:6:22: error: overriding non-deleted function ‘virtual void Human::run()’
virtual void run()=0;
^~~
Might be a bad example but what I want is to inherit everything except for run
in Baby
. Is it possible?
Disclaimer: I'm not implementing it anywhere. Just out of curiosity.
Virtual member functions are inherited. A class derived from an abstract base class will also be abstract unless you override each pure virtual function in the derived class. The compiler will not allow the declaration of object d because D2 is an abstract class; it inherited the pure virtual function f() from AB .
¶ Δ A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax.
Derived classes do not have to implement all virtual functions themselves. They only need to implement the pure ones. That means the Derived class in the question is correct. It inherits the bar implementation from its ancestor class, Abstract .
A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract. Classes having virtual functions are not abstract. Base class containing pure virtual function becomes abstract.
A pure virtual function is declared by assigning 0 in declaration. See the following example. A pure virtual function is implemented by classes which are derived from a Abstract class. Following is a simple example to demonstrate the same. 1) A class is abstract if it has at least one pure virtual function.
Pure Virtual Functions and Abstract Classes in C++. Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. For example, let Shape be a base class.
Once a function is declared as virtual, it stays virtual no matter how many layers of derived classes it may pass through. When a derived class does not override a virtual function, then the function, as defined in the base class, is used. For example, try this version of the preceding program in which Derived_Class2 doesn't override who ().
It turns out that we can define pure virtual functions that have bodies: In this case, speak () is still considered a pure virtual function because of the “=0” (even though it has been given a body) and Animal is still considered an abstract base class (and thus can’t be instantiated).
This is not possible. Consider the following code:
Human *CreateAPerson() {
return new Baby();
}
int main() {
Human *human = CreateAPerson();
human->run();
}
The compiler would have no way to know that human->run()
is illegal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With