After "= 0" is added to the parameter list declared by the virtual function, the function becomes a pure virtual function.

Source: Internet
Author: User

After "= 0" is added to the parameter list declared by the virtual function, the function becomes a pure virtual function.
Class base
{
Virtual void function () = 0;
}
We do not need to provide any definition for the pure virtual function base: function (). classes that declare pure virtual functions are abstract classes. Any attempt to create an abstract class object will cause a compiler error.
If a base is derived and the base: function () function is rewritten, it becomes a specific class.
Class derived: Public basae
{
Void function ();
}
An abstract class is usually used as an interface declaration. In this case, you do not need to declare a complete implementation for this interface. This interface describes the operations that all objects derived from this class should support, the derived class must implement these abstract operations.
Class base
{
Public:
Virtual void print () = 0;
};
Class derived: public Base
{
Public:
Virtual void print ()
{
Cout <"derived: derived () \ n ";
}
~ Derived ()
{
Print ();
}
};
Int main ()
{
Derived D;
Base * PTR = & D;
D. Print ();
}
Running result:
Derived: derived ()

Derived: derived ()


The first derived: derived () is the output generated when print () is called directly through D, and the second is called at the end of the program ~ Output generated when derived () is used.

Although we cannot directly create abstract class objects, we can still use pointers and references pointing to base.

For example, base * PTR = & D;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.