下面代码是否有错误?如果有错,错在哪里?
struct Test
{
Test() {};
Test(int i){}
void func(){}
};
int main()
{
Test t1(1);
Test t2();
t1.func();
t2.func();
}
答案:
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
说明:
成员的初始化顺序与成员的声明顺序相同
成员的初始化顺序与初始化列表中的位置无关
本题中成员变量在未初始化前存储的是随机数
参考:初始化列表的使用
下面的代码输出什么?为什么?
class Test
{
private:
int m_i;
int m_j;
public:
Test()
{
cout << “Test()” << endl;
}
Test(int v)
{
cout << “Test(int v)” << endl;
}
~Test()
{
cout << “~Test()” << endl;
}
};
Test Play(Test t)
{
return t;
}
int main()
{
Test m_t = Play(5); // Play(Test(5))
}
输出:
cout << “Test(int v)” << endl; // Play(5); ==> Play(Test(5))
cout << “~Test()” << endl; // t 析构
cout << “~Test()” << endl; // m_t 析构
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; // 正常调用
说明:
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】
以下两种情况:(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);
}