Interview Questions
Contents
Some Translation
n! Multiplicative
Object-oriented programming
- REDIRECT title:Interview_Questions(OOP)
Encapsulation
restricting access to some of the object's components
bundling of data with the methods
public and private data and methods.
A benefit of encapsulation is that it can reduce system complexity, and thus increases robustness, reduce risk of conflict between components.
Polymorphism
In C++, that type of polymorphism is called overloading. If a class has inherited a parent class, it can redefine a method and thus each class has a method with the same name but different functionality.
Inheritance
Inheritance is a way to reuse code of existing objects, establish a subtype from an existing object.
Allow replace the implementation of an inherited method or data.
Public Private Protected
A member (either data member or member function) declared in a private section of a class can only be accessed by member functions and friends of that class
A member (either data member or member function) declared in a protected section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes
A member (either data member or member function) declared in a public section of a class can be accessed by anyone
Private Inheritance
Private inheritance is like making a composition.
One advantage to using protected/private inheritance instead of composition is that the derived class has access to protected members in the parent class. However this is kind of a double-edged sword, as it becomes easier to misuse the class.
class Motherboard {}; // this makes a "has a" relationship class Computer : private Motherboard { }; // this makes a similar "has a" relationship // this approach is aka "composition" class Computer { private: Motherboard mobo; };
Virtual Functions
If the function is designated virtual in the base class then the derived class' function would be called (if it exists). If it is not virtual, the base class' function would be called. To be short, virtual functions will have lowest priority to be called.
Virtual inheritance
To avoid ambiguous call of the same 2 levels higher base class. Declare virtual to say that the 2 levels higher base class is the same shared one.
class Animal { public: virtual void eat(); }; // Two classes virtually inheriting Animal: class Mammal : public virtual Animal { public: virtual void breathe(); }; class WingedAnimal : public virtual Animal { public: virtual void flap(); }; // A bat is still a winged mammal class Bat : public Mammal, public WingedAnimal { };
To avoid ambiguous between inherited WingedAnimal::Animal and Mammal:Animal From the code above.Animal instance is unambiguous,and we can call Bat::eat().
Virtual const
class Base { public: virtual void eval() const { std::cout<<"Base Const Eval\n"; } }; class Derived:public Base { public: void eval() { std::cout<<"Derived Non-Const Eval\n"; } }; int main() { Derived d; Base* pB=&d; pB->eval(); //This will call the Base eval() return 0; }
The example above. based class evel() will be called. In your Derived class, the prototype for eval doesn't match the one for the virtual function in Base. So it won't override the virtual function. If you add the const for Derived::eval(), you should get virtual behavior.