下面代码是否有错误?若是有错,错在哪里?
struct Test { Test() {}; Test(int i){} void func(){} }; int main() { Test t1(1); Test t2(); t1.func(); t2.func(); }
答案:ios
1. Test t2(); ==> Test t2; warning: empty parentheses interpreted as a function declaration 2. t2.func(); ==> error: base of member reference is a function; perhaps you meant to call it with no arguments? 3. int main() 缺乏返回值 C++ 编译器须要兼容 C 编译器,编译过现有的 C 代码,所以没有报错
下面的代码输出什么?为何?
class Test { private: int m_i; int m_j; public: Test(int v) : m_j(v), m_i(m_j) { } int getI() { return m_i; } int getJ() { return m_j; } }; int main() { Test t1(1); Test t2(2); cout << t1.getI() << " " << t1.getJ() << endl; cout << t2.getI() << " " << t2.getJ() << endl; }
输出:编程
随机数, 1 随机数, 2
说明:segmentfault
成员的初始化顺序与成员的声明顺序相同 成员的初始化顺序与初始化列表中的位置无关 本题中成员变量在未初始化前存储的是随机数
参考:
初始化列表的使用数组
下面的代码输出什么?为何?
#include <iostream> using namespace std; class Test { private: int m_i; int m_j; public: Test() { cout << "Test()" << endl; } Test(int v) { cout << "Test(int v)" << endl; } Test(const Test& v) { cout << "Test(const Test& v)" << endl; } ~Test() { cout << "~Test()" << endl; } }; Test Play(Test t) { return t; } int main() { Test m_t = Play(5); }
输出:ide
g++ test.cpp -fno-elide-constructors 禁止编译器作任何优化 Test(int v) Test(const Test& v) Test(const Test& v) Test(const Test& v) ~Test() ~Test() ~Test() ~Test()
Which virtual fucntion re-declaration of the Derived class are correct ?
A. Base* Base::copy(Base*); Base* Derived::copy(Derived*); B. Base* Base::copy(Base*); Derived* Derived::copy(Base*); C. int Base::count(); int Derived::count(); D. void Base::func(Base*)const; void Derived::func(Base*);
答案:【C】
说明:函数
多态发生的条件:发生在派生类与基类之间;函数签名必须彻底一致;
下面的程序输出什么?为何?
class Base { public: virtual void func() { cout << "Base::func()" << endl; } }; class Child:public Base { public: void func() { cout << "Child::func()" << endl; } }; int main() { Base* pb = new Base(); pb->func(); Child* pc = (Child*)pb; // 注意这里!!! pc->func(); delete pc; pb = new Child(); pb->func(); pc = (Child*)pb; pc->func(); }
输出:优化
cout << "Base::func()" << endl; // 正常调用 cout << "Base::func()" << endl; cout << "Child::func()" << endl; // 多态调用 cout << "Child::func()" << endl; // 正常调用
说明:spa
Child* pc = (Child*)pb; ==> 子类对象指针指向父类对象,这是及其危险的!! 但是为何没有发生程序错误呢? 由于有虚函数表的存在,不管是子类对象指针仍是父类对象指针指向父类对象,都会发生相同的查找过程
当 func 不是虚函数时,就不会发生虚函数表的查找过程,此时必定会发生程序崩溃
参考:
C++对象模型分析指针
A C++ develoer wants to handle a static_cast<char*>() operation for the String class shown below. Which of the following options are valid declarations that will accomplish task ?
class String { public: // ... // declaration goes here } A. char* operator char*(); B. operator char*(); C. char* operator(); D. char* operator String();
答案:【B】code
如下两种状况:(1)new 一个 10 个整型变量的数组 (2)分 10 次 new 一个整型变量。 哪一个占用的空间更大?
A. 1 B. 2 C. 同样多 D. 没法肯定
答案:【B】
说明:
下面程序输出什么?
int main() { int v[2][10] = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18, 19, 20} } int (*a)[8] = (int(*)[8])v; cout << **a << endl; cout << **(a + 1) << endl; cout << *(*a + 1) << endl; cout << *(a[0] + 1) << endl; cout << *a[1] << endl; }
输出:
1 9 2 2 9
参考:
多维数组和多维指针
下面的程序输出什么?为何?
class Base { public: int a; Base() { a = 1; } void println() { cout << a << endl; } }; class Child : public Base { public: int a; Child() { a = 2; } }; int main() { Child c; c.println(); cout << c.a << endl; }
输出:
1 2
说明:
c.println(); ==> 父类中的 printlin 被调用,因为做用域, 父类中的 a 被访问 c.a ==> 发生同名覆盖
参考:
父子间的冲突
用 C/C++ 语言实现一个存储整型数据的栈结构
要求实现如下功能: 1. 入栈操做 push 2. 出栈操做 pop 3. 栈大小操做 size 4. 栈中最小元素 min
class IntStack { private: list<int> m_stack; list<int> m_cmin; public: void push(int v); int pop(); int top(); int size(); int min(); }; void IntStack::push(int v) { m_stack.push_front(v); if( m_cmin.size() != 0 ) { if( v < m_cmin.front() ) { m_cmin.push_front(v); } else { m_cmin.push_front(m_cmin.front()); } } else { m_cmin.push_front(v); } } int IntStack::pop() { int ret = m_stack.front(); m_stack.pop_front(); m_cmin.pop_front(); return ret; } int IntStack::top() { return m_stack.front(); } int IntStack::size() { return m_stack.size(); } int IntStack::min() { return m_cmin.front(); }
编程实现二叉树的相等比较,当二叉树每一个节点的值对应相等时,二叉树相等,不然不相等
struct BTreeNode { int v; BTreeNode* left; BTreeNode* right; }; 函数原型: bool BTreeCompare(BTreeNode* b1, BTreeNode* b2);
深度优先:
bool BTreeCompare(BTreeNode* b1, BTreeNode* b2) { bool ret = false; if( (b1 != NULL) && (b2 != NULL) ) { ret = (b1->v == b2->v) && BTreeCompare(b1->left, b2->left) && BTreeCompare(b1->right, b2->right); } if( (b1 == NULL) && (b2 == NULL) ) { ret = true; } return ret; }
广度优先:
bool BTreeCompareEx(BTreeNode* b1, BTreeNode* b2) { bool ret = true; list<BTreeNode*> l1; list<BTreeNode*> l2; l1.push_back(b1); l2.push_back(b2); while( ret && l1.size() && l2.size() ) { BTreeNode* n1 = l1.front(); BTreeNode* n2 = l2.front(); l1.pop_front(); l2.pop_front(); if( (n1 != NULL) && (n2 != NULL) ) { ret = (n1->v == n2->v); l1.push_back(n1->left); l1.push_back(n1->right); l2.push_back(n1->left); l2.push_back(n2->right); } else if( (n1 == NULL) && (n2 != NULL) ) { ret = false; } if( (n1 != NULL) && (n2 == NULL) ) { ret = false; } } return ret && (l1.size() == 0) && (l2.size() == 0); }