Difference between revisions of "Interview Questions"
(→Inheritance) |
|||
Line 11: | Line 11: | ||
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. | 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=== | ||
+ | Inheritance is a way to reuse code of existing objects, establish a subtype from an existing object.<br> | ||
+ | Allow replace the implementation of an inherited method or data.<br> | ||
+ | ===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<br> | ||
+ | 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<br> | ||
+ | A member (either data member or member function) declared in a public section of a class can be accessed by anyone<br> | ||
+ | ===Private Inheritance=== | ||
+ | Private inheritance is like making a composition.<br> | ||
+ | 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. | ||
+ | <pre> | ||
+ | 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; | ||
+ | }; | ||
+ | </pre> |
Revision as of 23:09, 16 February 2012
Contents
Some Translation
n! Multiplicative
Object-oriented programming
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; };