类与结构: 唯一的区别是 class 中变量默认是 private 的, 而 struct 是 public 的
例题1:
class A {
const int size = 0;
};
这道程序题存在成员变量的初始化问题. 常量必须在构造函数的初始化列表里面初始化或者将其设置为 static
class A {
static const int size = 0;
};
class A {
A() {
const int size = 0;
}
};
例题2:
Which virtual function re-declarations of the Derived class are correct?
Base* Base::copy(Base*); // 重载
Base* Derived::copy(Derived*);
Base* Base::copy(Base*);//编译错误, 不能根据返回值重载或重定义
Derived* Derived::copy(Base*);
ostream& Base::print(int, ostream&=cout);// OK, 是多态
ostream& Derived::print(int, ostream&);
void Base::eval() const; // 重载
void Derived::eval();
注意, const 修饰的函数算是重载, const 修饰的参数也算是重载