Learn a C++ improvement tip every day: Item 01: Treat C++ as a language federation

Get yourself used to C++

This blog post and this series are my thoughts on watching "Effective C++", and I learn a little trick to improve my C++ every day.

Article 01: Treat C++ as a language federation

Today's C++ is already a multi-paradigm programming language, a language that supports procedural, object-oriented, functional, generic, and meta-programming forms at the same time.

In order to understand the C++ language you must know the main sub-language, there are four:

1.C。

C++ is based on C. Blocks, statements, preprocessors, built-in data types, arrays, and pointers all come from C. In many cases, C++'s solution to problems is just a higher-level solution. When you use the C components in C++ to deal with problems, you will find that the high-efficiency guidelines find the limitations of C: no templates, no overloads, no abnormal.

2. Object-oriented C++.

Class construction and destruction, encapsulation, inheritance, polymorphism, virtual (virtual) function (dynamic binding)... etc.

3. Template C++.

This is the generic programming part of C++, and template-related considerations and design permeate the entire C++. The power of templates is powerful, bringing a new programming paradigm, template metaprogramming (TMP, template metaprogramming).

4.STL。

STL is a template library. It has excellent close coordination and coordination with the conventions of containers, iterators, algorithms, and function objects.

//2.面向对象C++。类的构造和析构,封装、继承、多态、virtual函数(动态绑定).....等等。
class Base
{
    
    
public:
	Base()   //构造
	{
    
    
		//.....
	}
	~Base()   //析构
	{
    
    
		//..
	}

};

class Derive : public Base   //Dervie类继承于Base类
{
    
    
	//.....
};

//3.模板(template)C++。这是C++的泛型编程部分,模板相关的考虑与设计弥漫整个C++。模板的威力强大,带来了崭新的编程范型,template metaprogramming(TMP,模板元编程)。

//函数模板
template<typename T>
T swap01(T& a, T& b)      //提供了交换数据函数的模板,以后可以传入不同数据类型的函数参数
{
    
    
	T temp = a;
	a = b;
	b = a;
}


int swap02(int& a, int& b) //非函数模板,以后调用改函数只能传入int型的函数参数
{
    
    
	int temp = a;
	a = b;
	b = temp;
}

//类模板
template<class T>
class A
{
    
    
public:
	T m;
};

class B :public A<int>    //继承类模板必须给出一个确定的类型
{
    
    

};

Guess you like

Origin blog.csdn.net/weixin_50188452/article/details/111033327