开发者

virtual functions and abstract classes

开发者 https://www.devze.com 2023-02-26 06:59 出处:网络
I read in my book: An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual fun

I read in my book:

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

Is it mandatory for an abstract class to have a vir开发者_C百科tual function? Why?

What is the difference between pure virtual function and virtual function and what is the need of them?


A pure virtual function specifies an interface that must be overridden in a derived class to be able to create objects of the derived class.

A (non-pure) virtual function specifies an interface that can be overridden in a derived class, but the base class provides a default implementation of the interface.

For most practical purposes, yes, an abstract base class must contain at least one virtual function. The whole point of an abstract base class is to specify an interface that's implemented by derived classes. That interface is specified in terms of a number of virtual functions that can be called. Without virtual functions, you haven't specified an interface, which makes it pretty hard for the abstract base class to accomplish much.


If you use an abstract class, that means you don't want to instantiate this class incorrectly. And you must use pure virtual function in that class.But declaring or writing function in class is your choice. You can write the function in derived class too.


The difference is that you cannot instantiate the abstract class - it acts as an interface.


To be abstract a class must have one pure virtual function. Only virtual function can be pure since it could be overriden and thus it's useful for polymorphism. Pure non-virtual function doesn't make sense because it doesn't do anything and couldn't be overriden, so it's useless, and doesn't exist :)


Yes, it must have at least one pure virtual function.

In case all the virtual functions for your base class have an implementation, and you would like to make it abstract nonetheless, you can use a pure virtual destructor:

class MyAbstractClass
{
    virtual ~MyAbstractClass() = 0;

    virtual void f() 
    {
        IHaveAnImplementation(); 
        SoICannotBePure();
    }
};

// The destructor can unfortunately not be defined inline
MyAbstractClass::~MyAbstractClass() {}

This only a conveniance: a pure destructor is not really a pure function since it has a definition. It is only a marker saying that the class cannot be instantiated, although it has no other abstract functions.


pure virtual means the method has no implementation so it forces any non abstract child class to provide that implementation.


In C++, the only way to make a class abstract is to put at least one pure virtual function in it. The compiler won't let you instantiate a class that contains a pure virtual function, because then you'd have an object with a function that has no definition. There are probably cryptic ways to get around this, but this is the standard practice.

The difference between a pure virtual and virtual function is that a pure virtual does not specify the implementation of the method. The =0 syntax tells the compiler that the class is not providing a definition for the function, which makes the function pure virtual and makes the class abstract. Any class deriving from the abstract base class must define the pure virtual function, or else the subclass will be abstract as well.

A "non-pure" virtual function is one which is marked with the virtual keyword, but a definition for the function is supplied in the base class. This means that the base class provides an implementation of the function, which any subclasses can override if desired. The virtual keyword allows polymorphism to work when you're using base class pointers that point to derived class objects.


A virtual function can be overridden in a derived class.

A pure virtual function must be overridden in a derived class.

A class with pure virtual functions cannot be instantiated.


Is it mandatory for an abstract class to have a virtual function? Why?

It depends on the definition you use. The standard use

A class is abstract if it has at least one pure virtual function.

so yes it is mandatory. Informally you may use another definition but then you risk confusion in a C++ context.

What is the difference between pure virtual function and virtual function and what is the need of them?

A pure virtual member:

  • must be overridden in all non abstract derived class

  • can be left without definition (but giving a definition is possible, it is even mandatory in the case of a pure virtual destructor).


A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract) derived class. This is indicated in the declaration with the syntax " = 0" in the member function's declaration.

Example:

class AbstractClass {
public:
  virtual void AbstractMemberFunction() = 0; // Pure virtual function makes
                                             // this class Abstract class.
  virtual void NonAbstractMemberFunction1(); // Virtual function.

  void NonAbstractMemberFunction2();
};

In general an abstract class is used to define an implementation and is intended to be inherited from by concrete classes. It's a way of forcing a contract between the class designer and the users of that class. If we wish to create a concrete class (a class that can be instantiated) from an abstract class we must declare and define a matching member function for each abstract member function of the base class. Otherwise, if any member function of the base class is left undefined, we will create a new abstract class (this could be useful sometimes).

0

精彩评论

暂无评论...
验证码 换一张
取 消