假设变量 veh 是类 Car 的一个实例,我们可以调用 veh.move(),是因为面向对象编程有( )性质。
class Vehicle {
private:
string brand;
public:
Vehicle(string b) : brand(b) {}
void setBrand(const string& b) { brand = b; }
string getBrand() const { return brand; }
void move() const {
cout << brand << " is moving..." << endl;
}
};
class Car : public Vehicle {
private:
int seatCount;
public:
Car(string b, int seats) : Vehicle(b), seatCount(seats) {}
void showInfo() const {
cout << "This car is a " << getBrand()
<< " with " << seatCount << " seats." << endl;
}
};分析时间复杂度时关注循环层数和每层迭代次数,递归算法则分析递归树。注意最坏/最好/平均情况的区别。
分析复杂度时注意外层循环和内层循环的关联。例如内外两层都到n的循环不一定是O(n²),要看内层是否每次都在缩小。
【考点】继承|类成员访问控制|面向对象编程基础
本题精准卡在 GESP 六级「面向对象编程」模块的继承机制考点。命题人专挑这个点发难——六级大纲明确要求考生理解「派生类继承基类公有成员」,但 80% 的考生只会死记「继承」这个词,完全搞不清访问控制才是关键:私有成员被继承但不可直接访问,公有成员既继承又可调用。