C++ 期末考试小总结

来源:互联网 发布:淘宝微淘粉丝数怎么看 编辑:程序博客网 时间:2024/06/05 11:20

完成了程刚出的期末试卷,感觉难度不小,后来又自己在底下研究了一下一些疑难问题,特总结如下:
1.有关基类、派生类指针相互调用的问题:
大体来说,就是因为sizeof(derived calss) >= sizeof(base class),所以用基类的指针指向派生类object或者pointer都是可以的(这里派生类可以当做基类看待),但是只要是派生类的指针,不管指向基类object还是pointer都是不行的。

# include <iostream>using std::cout;using std::endl;class A{    public:        display1(){            cout << "This is class A" << endl;        }};class B:public A{    public:        display2(){            cout << "This is class B" << endl;        }};int main(){    A *pa;    A a;    B *pb;    B b;    pa = pb; //A选项     pa ->display1();    pa ->display2(); //不能调用     pb = pa; //B选项 错误 invalid conversion from 'A*' to 'B*'     pa = &b; //C选项     pa -> display1();    pa -> display2(); //不能调用     pb = &a; //D选项  错误 invalid conversion from 'A*' to 'B*'     return 0;}

2.有关一些复杂的指针函数的表示:
类型1:函数返回类型+(*指针)+(函数形参列表)
short (*p[3]) (int): p是具有3个元素的指针数组,数组每个元素指针指向一个 short foo(int)类型的函数;

类型2:int (*p(int))[3]: p是具有三个元素的整数型指针数组,数组的每个元素指针,都是foo(int)的类型的函数的返回值(一个指针函数),见下:

How to understand the following declaration: void (*foo(char))(int);? In fact, foo is a function, the return type is a function pointer. We can divide this statement into two separate statements: typedef void (*T)(int); T foo(char);

因此,只有int (*p(int))()表示一个指针。

3.C++程序执行六步:
edit, preprocess, compile, link, load, execute.

4.reuse class是很常见的,也是很有用的,见教材section1.17:

Reuse of existing classes when building new classes and programs saves time, money and effort. Reuse also helps programmers build more reliable and effective systems, because existing classes and components often have gone through extensive testing, debugging and performance tuning.

5.virtual function 一般比memeber function 运行更慢些,这里有人在stackflow里做过实验:
Virtual functions and performance

6.有关运算符的优先顺序,参见教材附录A,注意这里”a+=b”、”a=a+b”运算符等级不完全等同,一切运算都是先高级再低级(跟数学中的规则一样)
C++ 操作符优先级
因此,以下两种表述是不相同的:
c += x>5?x+1:0;
c = c + x>5?x+1:0;

7.要注意static variable 在函数一次运行结束时不析构,而是保存起来等待下次再接着使用:

# include <iostream>using std::cout;int func(){    static int k = -1;    k++;    return k;}int main(){    int p = 2;    while(p < 5){        if(p%2){            p = func();        }        p ++;        cout << p << " ";    }    return 0;}

程序最后输出结果为:3 1 2 3 3 4 5

8.数组的初始化问题:

# include <iostream>using std::cout;using std::endl;template<typename T>void printArray(const T *array, int count){    for(int i=0; i<count; i++){        cout << array[i] <<" ";    }} int main(){    int array[7] = {1,2,3,4,5}; //array={};不管花括号里赋予几个值(当然不能多于7个),都算数组已初始化     printArray(array, 7);    return 0;}

根据C99的array initialization规则,默认初始化就是各元素都为0:
Initialization from brace-enclosed lists
因此,此题的输出为:1 2 3 4 5 0 0;
若改为int array[7] = { };则输出为:0 0 0 0 0 0 0;
若改为int array[7];则输出就是一些不可测的数字,比如:3 0 43 0 0 0 1.

9.有关string的搜索问题:
string::find 函数是字符串查找函数,具体用法可参见:
std::string::find reference
该函数可带三个参数,依次是查找关键字、查找地址(从什么地方开始找)、查找长度(关键词有效长),可见以下示例:

#include <iostream>       // std::cout#include <string>         // std::stringint main (){  std::string str ("There are two needles in this haystack with needles.");  std::string str2 ("needle");  // different member versions of find in the same order as above:  std::size_t found = str.find(str2);  if (found!=std::string::npos)    std::cout << "first 'needle' found at: " << found << '\n';    //first 'needle' found at: 14  found=str.find("needles are small",found+1,6);  if (found!=std::string::npos)    std::cout << "second 'needle' found at: " << found << '\n';    //second 'needle' found at: 44

find函数,如果找到,则返回(从左到右)第一个有效关键字的所在地址(注意这里地址是字符串从左向右计数,由0开始,1个字符算1个);如果找不到,则返回默认函数值std::string::npos(其实就是-1)

因此,我们借用以上性质,就可以编写以下程序来完成最后一题的要求:

#include <iostream>using std::cin;using std::cout;using std::endl;#include <string>using std::string;int main(){    int location = 0;    int count = 0;    string sentence;    string word;    cout << "Please input the whole sentence: " << endl;    getline(cin, sentence);     //getline(cin, xx)允许带空格输入,并将输入值存入xx变量     cout << "Please input the key word: " << endl;    cin >> word;    while(true){        location = sentence.find(word, location);        if (location!=string::npos){ //string::npos的值就是-1,find函数若未找到关键词则返回-1,否则则返回第一个关键词的地址             location ++;            count ++;        }        else{            break;        }    }    if(count!= 0){        cout << "The key word: "<<word<< " is " <<count <<" times in sentence"<<endl;    }    else{        cout << "Not found the key word in the whole sentence" <<endl;    }    return 0;}
原创粉丝点击