C + + abstract classes and interfaces

Source: Internet
Author: User
Tags function prototype

1. Abstract class

? In object-oriented programming, an abstract class is a class that can only define a type and cannot generate an object, and it is an abstraction of a series of concrete concepts that look different but are essentially the same. The most typical abstract class is "graphics", triangles, rectangles, trapezoidal are graphics, they all have edge length, area properties, "Graphics" class is the abstract of this series.

In programming, we can define a class that characterizes the graph, and then the concrete graph inherits the class. The graphical class, the base class, is defined as follows:

class Shape{public:    double area() //求面积 { //如何求?? }};

In fact, as long as the specific figure has an area of this amount, but the "graph" for the area is meaningless, only after knowing the specific type of graphic to find the area. So for "graphics", the area () function cannot be implemented, and shape cannot generate objects (meaningless), which requires that shape be defined as an abstract class: the area () function is declared as pure virtual function:

virtual double area() = 0;

Summarize what is abstract class: In fact, in C + + there is no concept of abstract class, to implement abstract classes need to be implemented by pure virtual function. A pure virtual function refers to a member function that defines only a function prototype. In C + + classes, as long as there is a pure virtual function, the class becomes an abstract class.

? (1) Each specific graph of the area of the algorithm is not the same, so with the virtual keyword, indicating that the function is a virtual function, when overridden in a subclass can occur polymorphic;
? (2) In order to find the Shape class area is meaningless, so adding "= 0" indicates that the function is declared as pure virtual function, do not need to define the function body.
? (3) Abstract class can not generate objects, can only be used as the parent class is inherited, the subclass must implement the specific function of pure virtual function, in the subclass, the pure virtual function of the parent class is implemented into a virtual function, of course, if the subclass does not implement a pure virtual function of the parent class, then the subclass is also an abstract class.

Class shape{Public://pure virtual functionVirtualDoubleArea () =0;};Define a circular class circle:Public shape{Privateint R;PublicCircleint a):R (a) {}Double Area () {Return R * R *3.14; }};Define the Rectangle class rectangle:Public shape{private: int x; int y; Public : rectangle (int A, int. B): x (a), y (b) {} double area () { return x * y;}}; int main (void) {Rectangle R (5, 6); Circle C (5); printf ("Circle area:%.2lf\n", C.area ()); printf ("Rectangular area:%.2lf\n", R.area ()); return 0;}                  

? Compile run:

2. Interface

When you understand what an abstract class is (a class contains pure virtual functions), it is easy to understand what an interface is. The interface in C + + is also a special class that needs to be met:
? (1) No member variables are defined in the class
? (2) All member functions in a class are public and are pure virtual functions

Example:

class deviceOp{public:    virtual bool OpenDev() = 0; virtual void CloseDev() = 0; virtual bool WriteDev(const char* buf, int len) = 0; virtual int ReadDev(char* buf, int len) = 0;};

? An interface is a special abstract class, so an abstract class has a feature that can only be inherited without creating an object.

Original Digest from: 77346934

C + + abstract classes and interfaces

Related Article

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.