2017.4.4 复习

来源:互联网 发布:古文知乎 编辑:程序博客网 时间:2024/05/21 08:54

虚析构函数

#include <iostream>using namespace std;class A{public:    A(){        cout<<"A"<<endl;    }    virtual ~A(){ //如果不把析构函数声明为虚函数,有时候就调用不到,因此产生内存泄漏         cout<<"~A"<<endl;    }};class B:public A{public:    B(){        cout<<"B"<<endl;    }    ~B(){        cout <<"~B"<<endl;    }};int main(){    A *b = new B();    delete b;}
#include <iostream>using namespace std;struct A{    double a;    virtual void foo(){        cout<<"foo in a"<<endl;    }    A(){    }    A(A&&a){    }    ~A(){        cout<<"des A"<<endl;    }};struct B:public A{    int b;    virtual void foo(){        cout<<"foo in b"<<endl;    }    B(A&&a){    }    B(B&&a){    }}; int main(){//1============//  A b;//  A a(move(b)); //move的意思是把左值转化为右值,但是这里的b,在move以后没有发生改变,并没有失效. //  a.foo();//  b.foo();//2============    A a;    B b(move(a));}

打印元素的所有出栈顺序

#include <iostream>#include <vector>#include <stack>using namespace std;void process(int *arr, int index, int n, vector<int>path,stack<int>&seq){    if(index == n && path.size() == n){         for(auto i : path){            cout<<i<<" ";        }        cout<<endl;        return;    }    if(index < n){        seq.push(arr[index]);        process(arr,index+1,n,path,seq);        seq.pop();    }    if(!seq.empty()){        int k = seq.top();        seq.pop();        path.push_back(k);        process(arr,index,n,path,seq); //注意这里并没有处理元素,所以是index(在原地)         path.pop_back();        seq.push(k);    }}int main(){    int arr[] = {1,2,3,4,5};     vector<int>path;    stack<int>seq;    process(arr,0,5,path,seq);}

关于纯虚函数的一些思考

#include <iostream>#include <vector>#include <stack>using namespace std;class A{public:    virtual void fun() = 0;};void A::fun(){    cout << "really fun" << endl;}class B :public A{public:    void fun(){        A::fun();        cout << "B's function" << endl;    }};int main(){    B a;    a.fun();}

关于截断和数字转换

#include <iostream>#include <vector>#include <stack>using namespace std;int main(){    cout << " unsigned int to unsigned char" << endl;    unsigned int sa = 0xFFFFFFF7;    unsigned char ii = (unsigned char)sa;    printf("%08x\n", ii); //000000f7,因为发生了截断    char *bb = (char *)&sa;    printf("%08x\n", *bb);}

关于虚函数的使用

#include <iostream>#include <string.h>#include <vector>#include <algorithm>#include <iterator>using namespace std;class A{public:    virtual void func(int val = 11)    {        std::cout << "A->" << val << std::endl;    }    virtual void test()    {        func();    }};class B :public A{public:    void func(int val = 0)    {        std::cout << "B->" << val << std::endl;    }};int main(int argc, char* argv[]){    B*p = new B;    p->test();    return 0;}

运算符优先级,在算的时候要注意
1+2 | 3,可以看出算术运算优先级高于移位运算,高于位运算,高于逻辑运算

main函数之前执行的代码,有全局变量的构造函数,以及规定的函数
可以看这里:

这里可以理解成为before & after exit()函数…负责分配内存,以及清理内存

http://blog.csdn.net/haoxingfeng/article/details/9165657

atexit()函数
http://blog.csdn.net/huang_xw/article/details/8542105

#include <stdio.h>  #include <stdlib.h>  #include <malloc.h>  class A  {  public:      A()      {          printf("A()\n");      }      ~A()      {          printf("~A()\n");      }  };  A a;  bool b = false;  int foo()  {      if (!b)      {          printf("init now!\n");          b = true;      }      else      {          printf("uninit now\n");      }      return 0;  }  typedef int (__cdecl *_PVFV)();  #pragma section(".CRT$XIU", long, read)  #pragma section(".CRT$XPU", long, read)  __declspec(allocate(".CRT$XIU")) _PVFV mgt_startup[] = { foo };  __declspec(allocate(".CRT$XPU")) _PVFV mgt_exit[] = { foo };  int main()  {      printf("main.!\n");      //exit(1);      return 0;  }  

VS2012使用条件断点和内存断点

http://blog.csdn.net/k346k346/article/details/51170635

#include <stdio.h>  #include <stdlib.h>  #include <malloc.h>  #include <iostream>using namespace std;int test;int main(){    /*    int a = 0;    for (int i = 0; i<1000; ++i){        cout << "before a++" << endl;        ++a;        cout << "after a++" << endl;    }*/    //在debug模式下,新建数据断点,也可以不在F5的情况下,创建函数断点.    cout << "before change test" << endl;    test = 1;    cout << "after change test" << endl;    return 0;}

void printInChinese(int num);
这个函数输入一个小于100000000(一亿)的正整数,并在屏幕上打印这个数字的中文写法。
例如:
17 -> 一十七
120 -> 一百二十
201 -> 二百零一
1074 -> 一千零七十四
65536 -> 六万五千五百三十六
1010101 -> 一百零一万零一百零一
提示:请注意‘零’的处理。

#include <stdio.h>  #include <stdlib.h>  #include <malloc.h>  #include <iostream>using namespace std;void printInChinese(int num){    char* wei[] = { ",", "十", "百", "千", "万", "十万", "百万", "千万" };    char* num1[] = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };    int a[9];    int i = 0;    int tmp, flag = 0;    while (num)    {        a[i] = num % 10;        num /= 10;        i++;    }    bool occur = false;    int pos = 0;    for (int j = i - 1; j >= 0; j--)    {        if(a[j]){            if(occur)                cout<<"零";            occur = false;            cout<<num1[a[j]];            if(j>0)                cout<<wei[j];        }else{            occur = true;        }       }    cout<<endl;}int main(){    printInChinese(120);         return 0;}

g++编译器的编译过程

一个工程由如下文件组成:head1.h head2.h src1.cpp src2.cpp main.cpp最终编译结果为xxx.exe(或者xxx,如果在Linux下的话)请写出你熟悉的某种编译器将这5个文件转换为最终结果xxx.exe(或xxx)的详细过程。写出每一个步骤,并作相关的解释,如果你熟悉某编译器的话,请写出这个编译器每一步用到的命令行。先将head1.h和src1.cpp文件生成1.o文件将head2.h和src2.cpp文件生成2.o文件再将main.cpp文件生成main.o文件最后将这些.o文件生成xxx.exeg++ -o 1.o  head1.h src1.cppg++ -o 2.o  head2.h src2.cppg++ -o main.o main.cppg++ -o xxx.exe main.o 1.o 2.o

C++如何禁止对一个对象的拷贝?

拷贝构造函数,和赋值函数都设置为私有,且不定义!!!

#include <iostream>using namespace std;class Noncopyable{private:    Noncopyable& operator=(const Noncopyable &rhs);    Noncopyable(const Noncopyable &rhs);//拷贝构造函数,和赋值函数都设置为私有,且不定义protected:    Noncopyable(){}    virtual ~Noncopyable(){}};class C : private Noncopyable{public:    C()    {        cout << "C constructor" << endl;    }    virtual ~C()    {        cout << "C destructor" << endl;    }};int main(void){    C c1;    C c2 = c1;    C c3;    c3 = c1;    return 0;}

堆排序的算法

#include <iostream>#include<algorithm>using namespace std;void makeHeap(int arr[],int i, int size){    int left = 2*i;    int right = 2*i+1;    int max = i;    if(i<=size/2){        if(left <= size && arr[max] < arr[left]){            max = left;        }        if(right <= size && arr[max] < arr[right]){            max = right;        }        if(max != i){            swap(arr[i],arr[max]);            makeHeap(arr,max,size);        }    }}void BuildHeap(int arr[], int size){    for(int i = size/2;i>=1;--i){ //只是调整非叶子结点,从下往上         makeHeap(arr,i,size);    }}void HeapSort(int arr[], int size){    BuildHeap(arr,size);    for(int i = size;i>=1;--i){ //调整完了以后开始输出 第i个         swap(arr[1],arr[i]);        makeHeap(arr,1,i-1);//之后重新调整非叶子结点, 从上往下     }}int main(int argc, char *argv[]){    int arr[] = {0,9,12,17,30,50,20,60,65,4,49};    int size = 10;    HeapSort(arr,size);     for(int i = 1;i<= size;i++)        cout<<arr[i]<<" ";    cout<<endl;    return 0;}

数据库的四个隔离级别,以及分别会遇到的问题有哪些?

HTTP状态码的分配,

数据库的三个范式,BC范式

#include <iostream>using namespace std;int i = 1;int main(void){    //cout << i << ++i << endl; //一定要记住这个函数的入栈顺序是从右往左,因此先执行++操作,不要想当然的以为是1,2!!!    printf("%d,%d\n",i,++i);    return 0;}

C 是不支持函数重载的!

sizeof一个class的时候还是会犯错误。

#include <iostream>using namespace std;class X{    bool b;    int a;    bool c;};int main(){    cout<<sizeof(X)<<endl;    return 0;}

如果类中加入了虚函数,又会怎样?
有一个虚表指针
如果有虚继承,则会产生两个虚表指针,每一个指针大小为8,(在64位操作系统上)

面试题

珠海金山C/C++开发工程师面试金山WPS C++研发面试。面试的时候是有好几个大教室,然后念到你名字就告诉你去哪个房间。每个教室大概有十个面试官吧,进去之后就坐下来开问了,先是一个简单的自我介绍,然后开始问专业相关的题目。需要注意的是,面试官面前就有你的笔试卷子,上面也会有评分,我当时的是B+,一般来说笔试达到A以上的,一定会有二面,A以下的就看你表现了。面试题全是C++的,一点其它的都没有,首先问了Const和define的区别,然后问了malloc和new的区别,接下来是类的部分,比如怎么重载,要你写一个继承类,然后是虚函数,问如何调用,用基类的指针调用派生类对象的函数(分虚函数和非虚函数)是什么情况。接下来是STL里的东西, 要你删除一个容器中值为5的元素,考的就是个迭代器。还有map和set怎么用,怎么添加和删除元素。问的还是挺多的,全部都是C++,数据结构、操作系统什么的问的都比较少。
#include <iostream>#include <functional>#include <map>#include <vector>#include<string>#include<numeric>#include <algorithm>#include <cstring>#include<sstream>using namespace std;int main() {    int a = 15, b = 8;    int mx[2] = {a,b};    cout<<mx[unsigned(a-b)>>31&1]<<endl; //比较两个数的大小,找较大的那个数.     cout<<sizeof(malloc(a*sizeof(int)))<<endl; // 永远只返回一个指针    int *c = &a;    cout<<sizeof(c)<<endl;//指针的大小在64位机器上是8,在32位的机器上是4    cout<<sizeof(int)<<endl;//int 大小都是统一的4.    return 0;}

c++内存分配

0 0