Preface: C + + was originally born, only called C with class. This shows that C and C + + are narrow strip relationships, and then C + + expands a lot of features into an object-oriented language that really becomes the handle. So what are the advantages of C + +? For example, a C code project is like a freezer, where everything is packed in a piece and doesn't say "channeling" and looks disorganized. and C + + is like a refrigerator, a freezer freezer, cold storage room and place to put eggs, place vegetables ... Of course, this is just one of the first benefits: encapsulation. However, C cannot write C + + features, which depends on the other features of C + + described below.
I. C + + beyond C
C + + extends some of the new features, the most important of which include the following:
- Encapsulation (Class)
- Inherited
- Polymorphic
- Stl
The next step is to introduce each feature briefly, and finally look at the main container of the STL.
Two. Feature Description 2.1 Package (Class)
By abstraction, classes encapsulate the properties and methods of a class of things together. This is the concept of "object oriented" in the name of the United States. Is that the organization of the Code is no longer the process control, but the organization of the object. The process Control of C language is carried out by judging the conditions, while the organization of C + + objects is organized by inheritance, polymorphism and so on.
class Test { public: int x_; void init(intint y,int z); void display() { cout<< x_ << endl; }; private: int y_; protected: int z_; };
The above is a typical definition of a class, from which you can see that the data and functions are encapsulated in a block, and the access rights are restricted by the keyword public,private,protected.
From the above definition, and the struct in C can also achieve similar functions, but C language is not able to set access rights, there are some people like to write C + + style.
2.2 Inheritance
As we mentioned above, the C language is the organization process, so the code is full of conditional judgment and process processing. However, in C + +, you want to organize a group of objects. The first is to abstract, to define the embryonic shape of the object. However, there is a problem must be solved, C + + is the object, can the world object thousands, the universe is the most not missing is the difference, then thousands different kinds and the object of the instantiation how to manage it? The answer is inheritance and polymorphism. Inheritance solves the same part, polymorphism solves the difference. This section introduces inheritance first.
Inheritance makes the new class unnecessary to be completely redefined, but with the help of existing class definitions, which greatly reduces the effort to create management classes. Inheritance enables new classes to get properties and methods from the base class (when public inherits).
class base{ public: void cacl(int n) {printf("good");} private: int i,j;};classpublic base{ public: void mix(int m) {printf("well");}};
If you need to take an inherited approach when creating a new class, you need to be aware that if a new class is one of the base classes, such as a husky is a dog, Husky has other properties and methods, such as selling Moe; At this time, Huskies can inherit the dog class. Another case is that the eye is part of the head, but it cannot be said that the eye is the base class, the head is derived from the class, because the head obviously does not see this function. This time, when creating the head of this class, should be in the head this class contains the eyes of this class, rather than inherit.
class head{ public: void cacl(void); class eye eyes; private: int len;};
2.3 polymorphic
Well, to create a new class, the first abstraction to find commonality, through the inheritance of new classes to build up, however, will soon encounter a problem: class behavior is very different, even for the same method will have a variety of performance, such as for eating, Indians are hand-grasping to eat, the Chinese are using chopsticks, European and American people are using knives and forks In this case, if you define a base class, do you want to define the method of eating? If not defined, then each derived class will have to define its own meal method, and, more importantly, a class and another class may not only eat different, almost all the methods are implemented differently ... For example, sleep, leisure, sports are not the same. So each derived class to define their own set, inheritance is not become a device? Well, what about code reuse?
This is the usefulness of polymorphism, which can be implemented for each derived class, when invoking the same method, implementing a different behavior, that is, calling to its own method implementation. The polymorphism of C + + can be divided into two types:
- Static polymorphism
- Dynamic polymorphism
The so-called static polymorphism is implemented by means of function overloading, which can be used against the operator as well as the function. With overloading, the function has the same name, and the parameters and return values are different, in effect determining which function to use based on the type of the parameter during the compilation phase.
Dynamic polymorphism is achieved through virtual functions, that is, defining virtual functions in the base class, then defining your own methods in the derived class, and overriding the methods of the base class.
#include<iostream>usingnamespace std;class Base{ public: virtual Base* FunTest() { "victory" << endl; returnthis; }};class Derived :public Base{ public: virtual Derived* FunTest() { "yeah" << endl; returnthis; }};int main(){ Base b; Derived d; b.FunTest(); d.FunTest(); return0;}
2.4 STL Template Library
In order to improve the development efficiency of C + +, the code can be reused, STL was born, it is not a part of C + + at first, STL actually provides the implementation of generic programming on C + +. The thing that the STL considers is that for a sort function, no matter which data structure, array or list is used, the sort function should be able to be implemented regardless of the datatype, int or float. For this purpose, the STL is divided into three parts when it is implemented.
- Container
- Algorithm
- Iterators
The container provides the common data structure, set,map,list,vector and so on, the algorithm provides dozens of kinds of algorithms such as sorting, and the iterator? Iterators are actually matching containers and algorithms, depending on the type of container element, with the corresponding sort implementation. For example, array sorting and list sorting.
Then, looking back, STL is shorthand for the standard Template Library. What is a template? We talked about STL in the first order to provide generic support, and recall that the function overload solves some of the problems too? (function overloading can also provide partial generics support by implementing different prototypes of multiple parameters.) However, it is not an effective way to solve generic support problems, so the template was born. Of course, there has been no mention of the class template, only referred to the function of the template. The typical definition of a template is as follows:
template<typename(或class) T>T fuc(T x, T y){ T x; //……}
The following is a brief introduction to several types of containers provided by STL:
2.4.1 Several common containers
There are two types of containers in C + +: Sequential containers and associative containers, and sequential containers include: vectors, lists, deque, etc. Where vector represents a contiguous memory address, based on the implementation of the array, list represents the non-contiguous memory, based on the linked list implementation. Deque is similar to vectors, but provides two-way support for deletion and insertion for the first element. The associated containers are mainly map and set. The map is in key-value form, and set is a single value.
Vector-need to contain#include<vector>
The main operations provided by Vector are:
- Vec1.push_back (100);
- Vec1.size ();
- Vec1.empty ();
- Vec1.pop_back ();
- Vec1.insert (Vec1.end (), 5, 3);
- Vector
List-need to include#include<list>
List is a doubly linked list that provides the main operations:
- Lst1.push_back (10);
- Lst1.pop_back ();
- Lst1.sort ();
- Lst1.reverse ();
- Lst1.remove (2);
Map-need to include header file#include<map>
Map provides a <key,value>
structure for storing pairs of children, and automatically sorts them. The main operations are:
- Map1.insert (Make_pair
- Map1.erase (3);
- Map1.size ();
Three. Summary
C + + provides an object-oriented approach to design, and many features revolve around the creation, management, and use of classes and objects. This article simply introduces some of the key things in C + +, as well as more details, such as constructors, destructors, smart pointers, etc. temporarily omitted from the table. But as long as the core content of C + + is mastered, the details use with the accumulation of experience, nature will have heaven and earth in mind.
You know C, so C + + is a cinch.