Home > Back-end >  Design and use of virtual functions
Design and use of virtual functions

Time:02-28

1. The task description

To establish a system of inheritance, the List is the base class, ArrayList, and LinkedList is a derived class,

Check List provides five functions, namely, add or delete to change significantly, among them, the first four is pure virtual function, the fifth is the virtual functions,

Users need to write in the ArrayList, and LinkedList implementation corresponding implementation,

Note: no need to realize in the ArrayList display function,
2. Knowledge

Virtual functions is the key to achieve dynamic binding in c + +, dynamic binding, as follows:

The List * pList;
PList=new ArrayList.
PList - & gt; Insert (pList - & gt; GetSize () - 1, someValue);//ArrayList do end insert faster
The delete pList.
PList=new LinkedList.
PList - & gt; Insert (0, someValue);//a LinkedList down into faster
The delete pList.

From the point of view of syntax, pList is a pointer to the type of List, but in 2, 3 lines of code, pList actually point to an ArrayList object, at that moment, pList invoking the insert member function, so it is called List insert function of a class or insert function of the ArrayList class? It depends on whether for virtual functions,

If the insert member function is a virtual function, is a member of the ArrayList is call function, if the insert member functions are not virtual functions, is now called the List member function,

Therefore, if the insert function is set to the virtual function, can achieve such an effect, as the above code written by a line 5, 6, through the pList pointer, can call the ArrayList insert function, also can call the LinkedList insert function,

What's good about doing so? Describe A situation, we need is to develop A use industrial camera image processing program, then bought A camera products of the company, and use the collection function, graphic programming code is as follows:

A, A.//camera object from A company
Anderson nitialize ();//initialize
While (1) {//death cycle
Atul gawande rab ();//acquisition a frame
/* here for image processing */
}

After a period of time, because of various reasons, to switch to B company camera, of course with B company also will provide its own function with the product, so we need to modify the code:

A, B;//B company's camera object
A.s tart ();//initialization, why company B function name is not A same as A company?
While (1) {//death cycle
Amy polumbo rocess ();//acquisition a frame, here not to expect any B company's name
/* here for image processing */
}

You will find that if the code involved in the more specific camera function, when you switch to another product, the more you need to modify the code, which means that it takes more manpower, material resources, time, and are more prone to errors,
Can do it, is still the first use of A company, but design A Camera base class, and then derived A class used to represent A company's Camera:

The class Camera {
Public:
Virtual void init ();
Virtual void getFrame ();
};
The class CA: public Camera {
Private:
A, A.//camera object from A company
Public:
Void init () {Anderson nitialize (); }
Atul gawande at the void getFrame () {rab (); }
}
Camera * pCamera=new CA;
PCamera - & gt; The init ();
While (1) {
PCamera - & gt; GetFrame ();
/* here for image processing */
}

Now need to change the product, how to do? Can add a CB class:

The class CB: public Camera {
Private:
A, B;//B company's camera object
Public:
Void init () {a.s tart (); }
Void getFrame () {Amy polumbo rocess (); }
};

For our main program, you just need to change a:

//Camera * pCamera=new CA;
//need to change this sentence can
Camera * pCamera=new CB.
PCamera - & gt; The init ();
While (1) {
PCamera - & gt; GetFrame ();
/* here for image processing */
}

The behind this programming method and compared with the former writing, it is more easy to extend, easier to upgrade, in order to get the benefits, of course, must be done at the same time 2 PM: authors must provide virtual function of a class, class of users must use Pointers or references to base classes,

Therefore, when you as the writer to write class, especially write inheritance system, must provide the appropriate virtual functions, when you use the class as a class of users, must first define a base class pointer or reference, if there is no special reason, never use a subclass type,

Also, note that the public inheritance base class destructor must be virtual,

So-called pure virtual functions, c + + allows a class to declare a pure virtual functions, that is, the virtual function unrealized, no function body, at this point, the class is an abstract class, it must be used as a base class, the effect of pure virtual functions, is derived class for its regulation function prototype, this is also called interface, c + + does not specifically describe the syntax of the interface and keywords, using pure virtual function in c + + to complete the function of the interface (the interface can refer to the content of the 3 off), whereas in Java interface as a keyword,

Incidentally, in an inheritance system, if a function is decorated virtual base class, the derived class and derived class derived classes, and so on are no longer need to explicitly write the virtual keyword, the function in all its derived classes are virtual,
3. Programming requirements

Level 4 files in total, of which the List. The h class provides a List, the List class provides five virtual functions, including four is a pure virtual function, require users to implementation in the subclass, ArrayList. Provided the ArrayList class h, LinkedList. Provided the LinkedList class h, these two classes are derived class List,
List. H content is as follows:

# # ifndef _LIST_H_
# define _LIST_H_
#include
Using STD: : ostream;
Using STD: : endl;
The class List {
Protected:
Int size;
Public:
//the default constructor and function constructor
The List (int s=0) : the size (s) {}
//copy constructor
The List (const List& RHS) : size (RHS. Size) {}
/* the following is a virtual function */
//as inherited base class destructor must be virtual
Virtual ~ List () {}
/* the following is a pure virtual functions, that is, do not implement */
Virtual void insert (int pos, int value)=0.
Virtual void remove (int pos)=0;
Virtual int the at (int pos) const=0;
Virtual void the modify (int pos, int newValue)=0.
/* the following is a common virtual functions, implementation */
Virtual void disp (ostream& OS) const {
For (int I=0, n=getSize (); iOs//but not hinder wrote
}
Os}
};
# endif//_LIST_H_

ArrayList. H content is as follows:

# # ifndef _ARRAYLIST_H_
# define _ARRAYLIST_H_
# include "List. H"
The class ArrayList: public List {
Private:
Int * data;//save data real place
Int capacity;//capacity
Public:
//the default constructor, to construct a logical order of empty table
ArrayList ();
//copy constructor, to construct a logical table with the order of parameters of the same content
ArrayList (const ArrayList& RHS);
//native array constructor, to construct a content table with the order of the given array
ArrayList (int const a [], int n);
//fill the constructor, to construct a content for the order of the n value table
ArrayList (int n, int value);
//the destructor, must achieve on its own, or have a memory leak
~ ArrayList ();
//subclasses must override of pure virtual function and realize the superclass
Void insert (int pos, int value);
Void the remove (int pos);
Int the at (int pos) const;
Void the modify (int pos, int newValue);
//for virtual functions have been implemented in the parent class, you can choose covered or not covered
//void disp (ostream& OS) const;//this function can be used directly in the parent class to implement
};
# endif//_ARRAYLIST_H_

LinkedList. H content is as follows:

# # ifndef _LINKEDLIST_H_
# define _LINKEDLIST_H_
# include "List. H"
The class LinkedList: public List {
Public:
//this is singly linked list node structure
Struct Node {
int data;
Node * next;
The Node (int a=0, Node * b=nullptr), data (a), next (b) {}
};
Private:
Node * head;//the head of the linked list node
Public:
//the default constructor, to construct a logical is empty list
LinkedList ();
//copy constructor, to construct a logically with the parameters of the same content list
LinkedList (const LinkedList& RHS); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related