optional · CSS study notes
P-I.III. Object Oriented Paradigm
CSS Computer Science (Optional) — Paper-I, Section-A, Topic III. Index for Object Oriented Paradigm — matches the textbook's chapter structure exactly, one page per heading.
Diagramming Reference (visual notation for concepts covered above)
Exam Practice (worked class-design questions)
Deeper-Dive Reference Pages (broader groupings with extra worked examples)
These predate the 22-page split above and go into more depth on cross-cutting topics — kept as supplementary reading:
MCQ Traps (Topic-wide)
- A pure virtual function (
= 0) has no base-class implementation and forces overriding; a plain virtual function has a default implementation that CAN be overridden but doesn't have to be - C++ supports multiple inheritance; many other OOP languages (e.g., Java) restrict classes to single inheritance and use interfaces instead
staticmembers belong to the class (one shared copy); non-static (instance) members belong to each object separately- Encapsulation and Data Abstraction are related but distinct: abstraction hides complexity; encapsulation is the mechanism (bundling + access control) that achieves it
✅ Self-Test Questions (with Answers)
Q1. Define the four core OOP principles with one line each.
Ans:
- Encapsulation — bundling data and the functions that operate on it inside one class and restricting direct access to the data (private members + public interface).
- Abstraction — exposing only the essential behaviour of an object and hiding the implementation detail (what it does, not how).
- Inheritance — deriving a new class from an existing one so it reuses and extends the base class's members (an "is-a" relationship).
- Polymorphism — one interface, many forms: the same call behaves differently depending on the object (compile-time through overloading, run-time through virtual functions).
Q2. What is the difference between a pure virtual function and a regular virtual function?
Ans: A regular virtual function has a body in the base class; a derived class may override it, and if it does not, the base version is used. A pure virtual function is declared virtual void f() = 0;, has no base implementation (it may optionally have one but still counts as pure), and must be overridden by any concrete derived class. Any class containing a pure virtual function becomes an abstract class and cannot be instantiated. Both are resolved at run time through the vtable.
Q3. Explain the difference between composition ("has-a") and inheritance ("is-a") with an example each.
Ans: Inheritance (is-a) creates a subtype: class Car : public Vehicle — a Car is a Vehicle, so it inherits the Vehicle interface and can be used wherever a Vehicle is expected. Composition (has-a) builds an object from other objects as members: class Car { Engine e; Wheel w[4]; }; — a Car has an Engine. Composition gives looser coupling and can be changed at run time, so modern design guidance is to "favour composition over inheritance"; inheritance is right only when the subtype genuinely satisfies the Liskov substitution principle.
Q4. Why can't an abstract class be instantiated directly?
Ans: Because it is incomplete: it declares at least one pure virtual function with no implementation, so an object of that class would have a vtable slot with no valid function to call. The compiler therefore rejects AbstractClass obj;. An abstract class exists to define a common interface/contract for its derived classes. You can still have pointers and references to it (Shape* s = new Circle();) — which is exactly how run-time polymorphism is achieved — and it may have constructors, data members and ordinary functions used by its subclasses.
Q5. Distinguish sequential access files from random access files.
Ans: In sequential access, records are read or written in order from the beginning; to reach record 50 you must pass through the previous 49. Records may vary in length; it suits text files, logs and batch processing, and is simple but slow for lookups; updating a record usually means rewriting the file. In random (direct) access, the program jumps straight to any record by computing its position — seekg/seekp with an offset — which requires fixed-length records. It suits databases, reservation and banking systems where individual records must be read or updated instantly, at the cost of more complex code and some wasted space in fixed-size records.
Q6. Name all 5 forms of inheritance.
Ans: (1) Single — one base, one derived class; (2) Multiple — one derived class with two or more bases (supported in C++; source of the diamond problem, solved by virtual inheritance); (3) Multilevel — a chain, A → B → C; (4) Hierarchical — several derived classes from one base; (5) Hybrid (multipath) — a combination of two or more of the above, typically multilevel plus multiple.
Q7. What's the difference between protected and private in the context of a derived class?
Ans: A private member of the base class is inherited but not accessible inside the derived class — only base-class members and friends can touch it (a derived class must use public/protected accessor functions). A protected member is accessible inside the derived class (and further descendants) but remains hidden from outside code, just like private. So protected is the right choice when subclasses need direct access to a member while the outside world must not — with the trade-off that it weakens encapsulation, since every future subclass depends on that member.