Object-Oriented Programming Concepts
Object-Oriented Programming Concepts
The exam checks whether you can distinguish the four pillars and match each to C++ mechanisms.
Core ideas
- Class is a blueprint; object is an instance with its own data members.
- Encapsulation bundles data and methods and restricts direct access, usually with
privatedata andpublicmethods. - Data hiding is the result of private/protected access; encapsulation is the broader design principle.
- Abstraction exposes only essential behaviour through an interface, hiding implementation detail.
- Inheritance lets a derived class reuse and extend a base class (
class B : public A). - Polymorphism means one interface, many forms: function overloading/operator overloading at compile time; virtual functions at run time.
- Message passing is calling an object's method.
Worked example
A class Shape has virtual void draw(). Circle and Square override it. With Shape *s = new Circle; s->draw();, the Circle version runs because the base function is virtual and the call is through a base pointer. Without virtual, Shape::draw() would be selected at compile time. This is run-time polymorphism.
Traps
- Saying encapsulation and abstraction are identical; one hides data, the other hides complexity.
- Expecting run-time polymorphism without
virtual. - Confusing overloading (same name, different parameter lists) with overriding (same signature in derived class).
- Assuming private members are directly accessible in derived classes; they are not.
- Forgetting that a virtual function is inherited as virtual.