錯題集
我真是艹了C艹了
選擇
某類中有一個無參且無返回值的常成員函數 Show,則正確的 Show 函數原型是
-
const void Show();
-
void const Show();
-
void Show() const;
-
void Show(const);
用來修飾函數本身,表示函數不能修改類變量,否則語法報錯。
填空
1.下面程序的輸出結果是:
0
5
#include <iostream>
using namespace std;
class A {
public:
int val;
public:
A(第一空)
{
val = n;
}
(第二空) GetObj() {
return (第三空);
}
};
int main(int argc, const char * argv[]) {
A a;
cout << a.val << endl;
a.GetObj() = 5;
cout << a.val << endl;
}
訂正:
- int n = 0(√)
- int&(×) ——>A&
- this->val(×)——>*this
2.下面程序的輸出結果是:
5,5
5,5
#include <iostream>
using namespace std;
class Base
{
public:
int k;
Base(int n):k(n) {}//類型轉換構造函數
};
class Big
{
public:
int v;
Base b;
Big(第一空)
Big(第二空)
};
int main()
{
Big a1(5);
Big a2 = a1;
cout << a1.v << "," << a1.b.k << endl;
cout << a2.v << "," << a2.b.k << endl;
return 0;
}
訂正:
-
(int n):b(n){this->v = n;}(×)——>(int n):v(n),b(n) {}【初始化賦值都拿出來】
-
():b(0){this->v=0;}(×)——>(const Big& a):v(a.v),b(a.b.k) {}【拷貝構造,需要學習!】