Abstract classes and interfaces in C + + (44)

Source: Internet
Author: User

In the study of C + +, when we conduct object-oriented analysis, we will find someAbstractThe concept. So how are they described in object-oriented languages? How is the area of the specific shape calculated? Graphs can be divided into rectangles, circles, and so on. What are the properties of animals? such as tigers, lions and so on. Then in the real need to know the specific graphics type in order to find the area, so the concept of "graphics" for the area is meaningless! Such as

Class Shape{public:double area () {return 0; }};

The Shape at this point is just a conceptual type, without its specific object! So is there any need for the Shape class to exist? Come down here, let's talk about abstract classes in object-oriented: A> can be used to represent abstract concepts in the real world;b> is a class that can only define types, and cannot produce objects;c> can only be inherited and override related functions;d> the direct feature is that the correlation function does not have a complete implementation.

Shape is the abstract concept of a variety of graphics in the real world, so you must be able to reflect abstract graphics in your program, and the concept of graphics in your program by means of abstract classes; Abstract classes cannot create objects, they can only be used for inheritance . There is no concept of abstract classes in the C + + language, and abstract classes can be implemented by pure virtual functions . A pure virtual function is a member function that defines only the prototype, and if a pure virtual function exists in a C + + class, it becomes an abstract class . Let's take a look at the syntax rules for pure virtual functions, as shown below

Class Shape{public:virtual Double area () = 0;};

where "= 0" is used to tell the compiler that a pure virtual function is currently declared, so there is no need to define the function body . Let's take the code as an example to illustrate

#include  <iostream>using namespace std;class Shape{public:     Virtual double area ()  = 0;}; Class rect : public shape{    int ma;    int  mb;public:    rect (int a, int b)     {         ma = a;        mb =  b;    }        double area ()      {        return ma * mb;     }};class circle : public shape{    int mr;public:     circle (Int r)     {         mr = r;    }      &nBsp; double area ()     {        return  3.14 * mr * mr;    }};void area (Shape* p) {     double r = p->area ();        cout  <<  "r = " &NBSP;&LT;&LT;&NBSP;R&NBSP;&LT;&LT;&NBSP;ENDL;} Int main () {    rect rect (1, 5);     circle circle (         area (&rect);     area (&circle);         return 0;}

We saw that two subclasses were defined below the Shape class and rewritten the area function. The global function area is then passed to the object to achieve polymorphism. Let's take a look at the compilation results

We see that it has been done correctly. Let's talk about the relationship between abstract class and pure virtual function:a> abstract class can only be used as the parent class is inherited;b> subclasses must implement the specific function of pure virtual function;c> The pure virtual function is implemented and becomes the virtual function;d> if the subclass does not implement a pure virtual function, the subclass becomes an abstract class.

Let's talk about interfaces, C + + classes that meet these conditions can be called interfaces:1, no member variables are defined in the class, 2, all the member functions are common, 3, all the member functions are pure virtual functions, 4, the interface is a special kind of abstract class . For example, we say in the Bluetooth interface, it is to send and receive data through a short distance to achieve the Bluetooth function, and then the network interface, it is through the Ethernet to achieve data transmission. The interface is capable of opening and shutting down devices, receiving and transmitting data.

Let's see how the interface is defined in the program.

#include <iostream>using namespace Std;class channel{public:virtual bool Open () = 0;    virtual void close () = 0;    virtual bool Send (char* buf, int len) = 0; virtual int recive (char* buf, int len) = 0;}; int main () {return 0;}

Through the abstract class and interface learning, summarized as follows:1, abstract class used to describe the real world of abstract concepts, abstract classes can only be inherited and cannot create objects, 2, in C + + No abstract class concept, is through pure virtual function to achieve abstract class, 3, the class only exist pure virtual function when the interface; 4 , an interface is a special kind of abstract class .


Welcome Everybody to learn C + + language together, can add me qq:243343083.

Abstract classes and interfaces in C + + (44)

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.