C + + Important knowledge point pick up memory

Source: Internet
Author: User
Tags export class

One: inline function
函数调用也会带来降低效率的问题,因为调用函数实际上将程序执行顺序转移到函数所存放在内存中某个地址,将函数的程序内容执行完后,再返回到转去执行该函数前的地方。这种转移操作要求在转去前要保护现场并记忆执行的地址,转回后先要恢复现场,并按原来保存地址继续执行。 因此,函数调用要有一定的时间和空间方面的开销,于是将影响其效率。特别是对于一些函数体代码不是很大,但又频繁地被调用的函数来讲,解决其效率问题更为重要。引入内联函数实际上就是为了解决这一问题。 优点:可以加快代码的执行速度,当程序中调用内联函数时,该函数直接嵌入到每个调用语句处,每次函数调用时都用相对应的一段代码代替。可见它是以目标代码的增加为代价来换取时间的节省 主要解决功能相对简单、规模不大但使用相当频繁的程序运行效率问题。

When using inline functions, the following rules apply:
1. The inline function body cannot contain any static variables, cannot use circular statements, switch, or recursive.
2. The definition of an inline function must appear before the first call.
3. If the function return type is void, you cannot have a return statement.

Two: Hands
Referencing array elements by pointers
int a[10];
int *p;
p++ is legal, and a++ is wrong. A is the name of the array, which is the first address of the array and is a constant;

Pointer variable to function: stores the function entry address, which points to the program code store.
1. A function call can be called by a function name or by a pointer variable pointing to a function.
2, (*p) () means defining a pointer to a function variable, in the program which function's address is assigned to it, it points to which function.
3. When assigning a value to a function pointer variable, simply give the function name without having to give the parameter. p = max
4. When invoking a function with a function pointer variable, simply replace (*p) with the function name, and in parentheses after (*p), use the realistic argument as needed. C= (*p) (A, B)
5. Pointer variables to pointers to functions cannot be calculated

Const Pointer

A pointer involves two variables, one is the pointer itself, the other is a pointer to a variable

1.指向常量的指针         const放在指针类型前,在程序中不能通过指针来间接修改指针所指向的内存空间的值,但可以改变指向的空间    int a = 10;    const int b = 20;    const int* pa = &a;    const int* pb = &b;    a = 100;                    // ok    b = 200;                   // error   *pa = 100;               // error   *pb = 200;              // error    pb = &a;             // ok    pa = &b;            // ok2.指针常量      const放在”*”和指针名之间,不能改地址能改所指向的值。 int b =28;int* const pb = &b;*pb = 100;                   // okpb++;                       // error在定义指针常量时,必须将其初始化。3.指向常量的指针常量     const在上述两个地方都加。既不允许修改指针值,也不允许修改指针变量所指向的值const int a = 10;const int * const pa = &a;a = 100;    // error*pa = 100;  // errorpa ++;  // error
Three: reference title
引用与指针的区别  引用是 C++ 语言引进的概念,在 C 语言中没有。

1), pointer is variable, reference is alias
The reference is an alias, and the reference itself has no address.
2), the pointer can count the group element, the reference is not
Example: int *pa[5];//pointer array
int a[5];
int &rea[5]=a;//not
...
3), can have a null pointer, not a null reference
Example: int *p=null;//Legal
int &re=null;//no meaning

Four: Classes (Class)
私有(private)的数据和函数,只允许本类的成员函数访问或调用;保护(protected:)的数据和函数,允许本类和本类派生类的成员函数访问或调用;公有(public:)的数据和函数,允许本类和其它类的函数访问或调用。

Static data members:
1. Static data members of a class are shared by all objects of the class.
2. You must assign an initial value to a static data member somewhere in the scope of the out-of-class file (because the constructor is called multiple times and the static data member is initialized only 1 times):
< type > < class name >::< static data member > = < initial value >

The Const member function ensures that no data members of any such object are modified.

class constfun{  private:     int  a;  public:    voidnonconstFunc( )    {       a=18;         //ok    }     void  const    {      a=18         //error    }}
this指针:    this指针是指向对象的指针,隐含在类的成员函数中,用来指向成员函数所属类的正在被操作对象。this指针可以看作是类自身的一个引用。

Call Order of constructors
For constructors, the base class is executed first, then the object member is executed, and the derived class is finally executed.
For a destructor, a derived class is executed before the object member is executed, and the base class is finally executed.

导出类构造函数和析构函数的构建基类的构造函数和析构函数不能被派生类继承。如果基类没有定义构造函数,派生类也可以不定义构造函数,全都采用缺省的构造函数,此时,派生类新增成员的初始化工作可用其他公有函数来完成。如果基类定义了带有形参表的构造函数,派生类就必须定义构造函数,提供一个将参数传递给基类构造函数的途径,以便保证在基类进行初始化时能获得必需的数据。如果派生类的基类也是派生类,则每个派生类只需负责其直接基类的构造,不负责自己的间接基类的构造。派生类是否要定义析构函数与所属的基类无关,如果派生类对象在撤销时需要做清理善后工作,就需要定义新的析构函数。
V: Multiple Inheritance
解决二义性问题解决方法一:用类名来限定(主要解决方法)   为避免二义性,可在调用时加上基类的名称,如 A::print() 或 B::print() 。解决方法二:同名覆盖在C 中声明一个同名成员函数print(),f()再根据需要调用  A:: print()    或    B:: print()解决方法三:使用虚函数

Three mechanisms of object-oriented design: Data encapsulation, inheritance, polymorphism.

  继承:研究的是类与类之间的层次关系。  多态性:指不同的对象接收到相同的消息时产生不同的响应动作,即对相同的函数名,却执行不同的函数体。  函数重载和运算符重载实现类的一种多态性。

Static and dynamic linking
Union (binding): is the process of associating a function call with the corresponding function body code.
Static binding: If the binder process is completed before the program starts running, the compilation phase is complete.
For example: overloaded functions:
void Fun (int a,int b)
void Fun (float x,float y)
void Fun (char c)
Function names are the same, but the respective parameters are different, the compiler can automatically select the corresponding function body compilation according to the type and number of function parameters.

Dynamic Union (dynamically binding)
The way the program is run when it is running.
For example: virtual function
Virtual functions in C + +, because of their function name, return value, function parameters are identical, but the function body is different, so the compilation phase can not determine which function call and which body Association, can only be determined by the system when the program runs.

VI: virtual function
虚函数(virtual function)----运行时多态  在定义某一基类(或其派生类)时,若将其中的某一函数成员的属性说明为virtual,则称该函数为虚函数。       若基类中某函数被说明为虚函数,则意味着其派生类中也要用到与该函数同名、同参数表、同返回类型、但函数体不同。     虚函数存在继承环境中。虚函数成员的定义语法: virtual 函数类型 函数名(形参表)   {    函数体    }
program Example Class baseclass{ Public:Virtual void Show() {cout<<"Base class"<<endl;}//If you do not add the keyword virtual, the result of the operation is "Base class";};class Derived1: Publicbaseclass{ Public:void Show() {cout<<"Derived Class1"<<endl;}}; Class Derived2: Publicbaseclass{ Public:void Show() {cout<<"Derived Class2"<<endl;}};intMain () {BaseClass obj;     BaseClass *p;     Derived1 obj1;     Derived2 Obj2;     p=&obj;     P->show ();     p=&obj1;     P->show ();     p=&obj2; P->show ();return 0;} The purpose of accessing the derived class object member function with the base class pointer is achieved by virtual function, so that if the base class pointer is declared, it can make different function calls to different derived class objects, and realize the program run-time polymorphism. Run polymorphic should use virtual functions and call virtual functions via pointers, references, or member functions
          纯虚函数和抽象类

Pure virtual function:
Declare the virtual function in the base class without giving a specific definition, put its definition in each export class, this kind of function is pure virtual function
Virtual functions of all derived classes can be called through a base-class pointer or reference.

Abstract class: (Abstract Class)
A class that declares a virtual function, which is used only for inheritance, as an interface, and the specific functionality is implemented in a derived class.
Note: You can derive a specific or abstract class from an abstract class, but you cannot derive an abstract class from a concrete class.

Virtual base class
Introduction of Virtual base class
For occasions where there is a common base class
Statement
Example of a base class with virtual modification: class B1:virtual public B
Role
It is mainly used to solve the two semantic problems that can occur when multiple inheritance is inherited multiple times for the same base class.
Provides a unique base class member for the farthest derived class without repeating multiple copies

Class template
Another way to implement polymorphism in C + + is a class template
A class template enables a user to define a pattern for a class so that the parameters of some data members and member functions in the class, as well as the return values of member functions, can be married to any type.

#include <iostream.h>Template<classT>classTestClass { Public: T buffer[Ten];//t type data member buffer array size fixed to 10 (poor flexibility!) T GetData (intj);//Gets the first J component of the type T buffer (array)};Template<classT> T Testclass<t>::getdata (intj) {return* (BUFFER+J); };voidMain () {testclass<Char> Classinsta;//char replaces T, thus instantiating a specific class    Charcarr[6]="ABCDE"; for(intI=0; i<5; i++) Classinsta.buffer[i]=carr[i]; for(i=0; i<5; i++) {CharRes=classinsta.getdata (i);cout<<res<<"  "; }cout<<endl; After the execution of the program, the following results are displayed: A b c D E2.1  13.2  24.3  35.4  46.5  57.6 

Example of a class template that uses both type parameters and common parameters

#include <iostream.h>#include "string.h"Template<classTintI>classTestClass { Public: T Buffer[i];buffer of type//t, whose size varies with the value of normal parameter I (flexibility!)T GetData (intj); };Template<classTintI> T Testclass<t,i>::getdata (intj) {return* (BUFFER+J); }; testclass<Double,6> classinstf;Doublefarr[6]={12.1,23.2,34.3,45.4,56.5,67.6}; for(i=0; i<6; i++) classinstf.buffer[i]=farr[i]-Ten; for(i=0; i<6; i++) {DoubleRes=classinstf.getdata (i);cout<<res<<"  "; }cout<<endl;} After the execution of the program, the following results are displayed: A b c D E2.1  13.2  24.3  35.4  46.5  57.6

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Important knowledge point pick up memory

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.