How unit 1 is examined
OOP versus procedural programming, OOP features, the object model, C++ I/O, data types, control statements, loops and arrays; the marks sit in features/merits/demerits, comparison, object model and I/O.
Comparison with Procedural Programming
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>Procedural programming organises a program as a sequence of procedures (functions) that operate on shared data, whereas object-oriented programming organises it as objects that bundle data and the functions acting on that data.</mark>
Key points.
- Procedural programming is top-down: the problem is split into functions that
maincalls in a fixed order. - OOP is bottom-up: classes come first, and objects pass messages, with dynamic binding choosing the method.
- Procedural code emphasises the algorithm, while OOP emphasises data and models real entities such as Student.
- Procedural data is usually global, so any function can change it; OOP hides data inside objects behind methods.
- Procedural code is reused by calling functions, OOP by inheritance and polymorphism.
- A change in shared procedural data affects many functions, while OOP keeps it inside one class.
Answer frame. Open with both definitions; draw the table in the next section (most marks); add points 4 and 6; close with "OOP suits large, changing systems, procedural suits small programs".
Pitfall: Writing only OOP features and no side-by-side table; the question says compare.
Asked: [7 marks] (Jun 2023, Dec 2024, Dec 2025) Explain the characteristics of Object-Oriented Programming and Procedural Programming in brief. How does OOP differ from procedural programming, and what are the key features of the object-oriented paradigm? Asked: [7 marks] (Jun 2023, Dec 2023, Dec 2025) Compare procedural programming with object oriented programming.
Features of the OO paradigm, merits and demerits
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>Object-oriented programming is a paradigm in which software is built from objects, instances of classes, that combine data and the operations on it, and which interact by passing messages.</mark>
Key points (features).
- A class is a blueprint of data and methods; an object is its runtime instance.
- Encapsulation binds data and methods into one unit, and data hiding blocks direct access through private members.
- Abstraction shows only the essential interface and hides implementation.
- Inheritance lets a derived class acquire a base class's members, giving reuse.
- Polymorphism means one name, many forms: overloading (compile time) and overriding through virtual functions (run time).
- Dynamic binding links a call to its method at run time by the actual object type: with a virtual
speak(),Animal* p = new Dog; p->speak();callsDog::speak. - Message passing is how objects communicate: a message names the receiver object, the method and the arguments, as in
acc.deposit(500).
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u1-01" viewBox="0 0 596 209" width="596" height="209" role="img" aria-label="Features of OOP: Cls = class and object, Enc = encapsulation, Abs = abstraction, Inh = inheritance, Pol = polymorphism"><style>#dsfig-u1-01 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u1-01 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u1-01 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u1-01 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u1-01 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u1-01 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u1-01 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u1-01 .t{fill:#16181D;font-weight:500}#dsfig-u1-01 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u1-01 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u1-01 .dot{fill:#16181D}#dsfig-u1-01 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u1-01 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u1-01 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u1-01 .ah{fill:#454C5A}#dsfig-u1-01 .ah.hi{fill:#2340B8}#dsfig-u1-01 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u1-01 .wl .t{font-size:12px;font-weight:700}#dsfig-u1-01 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u1-01 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u1-01 .e{stroke:#B1B7C3}html.dark #dsfig-u1-01 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u1-01 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u1-01 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u1-01 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u1-01 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u1-01 .t{fill:#E6E8ED}html.dark #dsfig-u1-01 .t.inv{fill:#0F1115}html.dark #dsfig-u1-01 .kd{stroke:#E6E8ED}html.dark #dsfig-u1-01 .dot{fill:#E6E8ED}html.dark #dsfig-u1-01 .ann{fill:#8FA3FF}html.dark #dsfig-u1-01 .lbl{fill:#858D9C}html.dark #dsfig-u1-01 .ptr{fill:#8FA3FF}html.dark #dsfig-u1-01 .ah{fill:#B1B7C3}html.dark #dsfig-u1-01 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u1-01 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u1-01 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u1-01 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah1" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh1" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M238.7,49.8 L58,158.2" marker-end="url(#ah1)"/><path class="e" d="M244.5,55.8 L180.6,151.5" marker-end="url(#ah1)"/><path class="e" d="M261,58 L291.4,149.1" marker-end="url(#ah1)"/><path class="e" d="M270.2,51.4 L410.2,156.4" marker-end="url(#ah1)"/><path class="e" d="M272.5,47.5 L536.7,160.7" marker-end="url(#ah1)"/><circle class="n" cx="255" cy="40" r="18"/><text class="t" x="255" y="40" dy=".35em" text-anchor="middle">OOP</text><circle class="n" cx="40" cy="169" r="18"/><text class="t" x="40" y="169" dy=".35em" text-anchor="middle">Cls</text><circle class="n" cx="169" cy="169" r="18"/><text class="t" x="169" y="169" dy=".35em" text-anchor="middle">Enc</text><circle class="n" cx="298" cy="169" r="18"/><text class="t" x="298" y="169" dy=".35em" text-anchor="middle">Abs</text><circle class="n" cx="427" cy="169" r="18"/><text class="t" x="427" y="169" dy=".35em" text-anchor="middle">Inh</text><circle class="n" cx="556" cy="169" r="18"/><text class="t" x="556" y="169" dy=".35em" text-anchor="middle">Pol</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Features of OOP: Cls = class and object, Enc = encapsulation, Abs = abstraction, Inh = inheritance, Pol = polymorphism</figcaption></figure>
Characteristics of procedural vs OOP.
| Basis | Procedural | Object oriented |
|---|---|---|
| Approach | Top-down | Bottom-up |
| Unit of modularity | Function | Class |
| Data vs functions | Separate; functions share global data | Bundled in one object |
| Emphasis | Algorithm and steps | Data and its behaviour |
| Data hiding | None, data is global | private, protected members |
| Code reuse | Calling or copying functions | Inheritance, polymorphism, composition |
| Inheritance | Not supported | Supported |
| Control flow | main calls functions in a fixed order |
Objects pass messages; dynamic binding chooses the method |
| Extensibility | Hard | Easy, new classes are added |
| Examples | C, Pascal, FORTRAN | C++, Java, Python, Smalltalk |
OOP languages. Object-based languages lack inheritance and polymorphism (Ada 83); object-oriented ones have all features. Pure OO languages make every value an object (Smalltalk, Ruby); hybrids add OOP to a procedural base (C++, with multiple inheritance, operator overloading and templates). Java keeps all code in classes, with single inheritance, interfaces and garbage collection; Python is dynamically typed.
Merits (advantages).
- Inheritance gives reusability of tested code.
- Modularity: each class is built and tested alone, so maintenance is easy.
- Data hiding gives security, as outside code cannot corrupt private data.
- Objects mirror real entities, and new classes extend software without changing old code.
Demerits (disadvantages).
- Dynamic binding adds runtime overhead: objects of polymorphic classes carry a hidden vptr, and virtual calls go through the vtable instead of a direct call.
- Execution is slower than procedural code because of this indirection.
- Programs are larger and use more memory, since every object stores its own data.
- Deep hierarchies add complexity, and simple problems get over-engineered.
- The many concepts give a steep learning curve, and OOP does not suit every domain, such as small scripts.
Features to merits/demerits. Inheritance gives reuse, encapsulation gives security, modularity gives easy maintenance, and dynamic binding gives flexibility at the cost of runtime overhead.
Answer frame. Open with the definition; draw the feature diagram; define the seven features; then merits, demerits and the mapping line (both lists even for "advantages" questions); for "languages" add the classification; close with the trade-off.
Trade-off. OOP trades speed for reuse, security and maintainability, so it suits large software; unlike functional (pure functions) and logic programming (rules, Prolog), it models state and behaviour together.
Asked: [7 marks] (Nov 2022, Jun 2023) Explain object oriented programming languages with its features. Describe in detail the features of object-oriented paradigm. Asked: [7 marks] (Dec 2023, Jun 2025, Dec 2025) What do you understand by object-oriented programming? What are the advantages of programming using object oriented? Explain and define various OOP concepts in brief. What are the main features of the paradigm and how do they contribute to its advantages and disadvantages? Asked: [7 marks] (Dec 2023, Dec 2024) Briefly describe merits and demerits of object-oriented methodology. Discuss the merits and demerits of OO methodology compared to other paradigms. Asked: [7 marks] (Nov 2022, Jun 2023, Dec 2023, Dec 2024, Dec 2025) Explain features, merits and demerits of Object Oriented paradigm.
Object model
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>The object model is the conceptual framework of object-oriented programming, defined by four major elements (abstraction, encapsulation, modularity, hierarchy) and three minor elements (typing, concurrency, persistence).</mark> A model lacking a major element is not object oriented (Booch).
Key points.
- Abstraction keeps the essential characteristics that distinguish an object and ignores irrelevant detail.
- Encapsulation hides internal detail by binding data and methods into a class, exposing only a public interface.
- Modularity splits a system into cohesive, loosely coupled modules built separately.
- Hierarchy ranks abstractions: an "is-a" hierarchy by inheritance and a "part-of" hierarchy by aggregation.
- Typing (minor) stops types being interchanged wrongly: strong typing rejects a mismatch (
int n = "abc";), weak typing converts silently; static typing checks at compile time (C++, Java), dynamic at run time (Python). - Concurrency (minor) lets objects work at once: an active object owns a thread (
thread t1(f);), an inactive object acts only when called. - Persistence (minor) lets an object's state outlive the program, stored in a file or database.
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u1-02" viewBox="0 0 844 194" width="844" height="194" role="img" aria-label="Elements of the object model"><style>#dsfig-u1-02 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u1-02 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u1-02 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u1-02 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u1-02 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u1-02 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u1-02 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u1-02 .t{fill:#16181D;font-weight:500}#dsfig-u1-02 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u1-02 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u1-02 .dot{fill:#16181D}#dsfig-u1-02 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u1-02 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u1-02 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u1-02 .ah{fill:#454C5A}#dsfig-u1-02 .ah.hi{fill:#2340B8}#dsfig-u1-02 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u1-02 .wl .t{font-size:12px;font-weight:700}#dsfig-u1-02 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u1-02 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u1-02 .e{stroke:#B1B7C3}html.dark #dsfig-u1-02 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u1-02 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u1-02 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u1-02 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u1-02 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u1-02 .t{fill:#E6E8ED}html.dark #dsfig-u1-02 .t.inv{fill:#0F1115}html.dark #dsfig-u1-02 .kd{stroke:#E6E8ED}html.dark #dsfig-u1-02 .dot{fill:#E6E8ED}html.dark #dsfig-u1-02 .ann{fill:#8FA3FF}html.dark #dsfig-u1-02 .lbl{fill:#858D9C}html.dark #dsfig-u1-02 .ptr{fill:#8FA3FF}html.dark #dsfig-u1-02 .ah{fill:#B1B7C3}html.dark #dsfig-u1-02 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u1-02 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u1-02 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u1-02 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><line class="e" x1="445.5" y1="37" x2="250.3" y2="101"/><line class="e" x1="445.5" y1="37" x2="640.8" y2="101"/><line class="e" x1="250.3" y1="101" x2="67" y2="165"/><line class="e" x1="250.3" y1="101" x2="197" y2="165"/><line class="e" x1="250.3" y1="101" x2="323" y2="165"/><line class="e" x1="250.3" y1="101" x2="433.5" y2="165"/><line class="e" x1="640.8" y1="101" x2="528.5" y2="165"/><line class="e" x1="640.8" y1="101" x2="631" y2="165"/><line class="e" x1="640.8" y1="101" x2="753" y2="165"/><rect class="n" x="388.5" y="22" width="114" height="30" rx="8"/><text class="t" x="445.5" y="37" dy=".35em" text-anchor="middle">Object model</text><rect class="n" x="220.8" y="86" width="59" height="30" rx="8"/><text class="t" x="250.3" y="101" dy=".35em" text-anchor="middle">Major</text><rect class="n" x="14" y="150" width="106" height="30" rx="8"/><text class="t" x="67" y="165" dy=".35em" text-anchor="middle">Abstraction</text><rect class="n" x="136" y="150" width="122" height="30" rx="8"/><text class="t" x="197" y="165" dy=".35em" text-anchor="middle">Encapsulation</text><rect class="n" x="274" y="150" width="98" height="30" rx="8"/><text class="t" x="323" y="165" dy=".35em" text-anchor="middle">Modularity</text><rect class="n" x="388" y="150" width="91" height="30" rx="8"/><text class="t" x="433.5" y="165" dy=".35em" text-anchor="middle">Hierarchy</text><rect class="n" x="611.3" y="86" width="59" height="30" rx="8"/><text class="t" x="640.8" y="101" dy=".35em" text-anchor="middle">Minor</text><rect class="n" x="495" y="150" width="67" height="30" rx="8"/><text class="t" x="528.5" y="165" dy=".35em" text-anchor="middle">Typing</text><rect class="n" x="578" y="150" width="106" height="30" rx="8"/><text class="t" x="631" y="165" dy=".35em" text-anchor="middle">Concurrency</text><rect class="n" x="700" y="150" width="106" height="30" rx="8"/><text class="t" x="753" y="165" dy=".35em" text-anchor="middle">Persistence</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Elements of the object model</figcaption></figure>
Relationships.
- Association is a "uses" link between independent objects: a Teacher uses a Course.
- Aggregation is a weak "has-a" link: a Car has an Engine, which can exist on its own.
- Composition is a strong "has-a" link: a Human owns a Heart, which cannot exist without the whole.
- Inheritance is an "is-a" link: a Dog is an Animal.
class Teacher { public: void teach(Course& c); }; // association
class Car { Engine* e; }; // aggregation
class Human { Heart h; }; // composition
class Dog : public Animal { // inheritance
string breed; // private
public: void speak() { cout << "Bark"; } };
Diagram. Boxes for classes: plain line for uses, hollow diamond at Car, filled diamond at Human, hollow-triangle arrow to Animal.
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u1-03" viewBox="0 0 259 389.6" width="259" height="389.6" role="img" aria-label="Relationships: Tch = Teacher, Crs = Course, Eng = Engine, Hum = Human, Hrt = Heart, Anml = Animal"><style>#dsfig-u1-03 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u1-03 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u1-03 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u1-03 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u1-03 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u1-03 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u1-03 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u1-03 .t{fill:#16181D;font-weight:500}#dsfig-u1-03 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u1-03 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u1-03 .dot{fill:#16181D}#dsfig-u1-03 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u1-03 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u1-03 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u1-03 .ah{fill:#454C5A}#dsfig-u1-03 .ah.hi{fill:#2340B8}#dsfig-u1-03 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u1-03 .wl .t{font-size:12px;font-weight:700}#dsfig-u1-03 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u1-03 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u1-03 .e{stroke:#B1B7C3}html.dark #dsfig-u1-03 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u1-03 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u1-03 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u1-03 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u1-03 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u1-03 .t{fill:#E6E8ED}html.dark #dsfig-u1-03 .t.inv{fill:#0F1115}html.dark #dsfig-u1-03 .kd{stroke:#E6E8ED}html.dark #dsfig-u1-03 .dot{fill:#E6E8ED}html.dark #dsfig-u1-03 .ann{fill:#8FA3FF}html.dark #dsfig-u1-03 .lbl{fill:#858D9C}html.dark #dsfig-u1-03 .ptr{fill:#8FA3FF}html.dark #dsfig-u1-03 .ah{fill:#B1B7C3}html.dark #dsfig-u1-03 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u1-03 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u1-03 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u1-03 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah3" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh3" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M59,40 L191,40" marker-end="url(#ah3)"/><path class="e" d="M59,143.2 L191,143.2" marker-end="url(#ah3)"/><path class="e" d="M59,246.4 L191,246.4" marker-end="url(#ah3)"/><path class="e" d="M59,349.6 L184,349.6" marker-end="url(#ah3)"/><g class="wl"><rect x="105.6" y="31" width="40.8" height="18" rx="9"/><text class="t" x="126" y="40" dy=".35em" text-anchor="middle">uses</text></g><g class="wl"><rect x="102.5" y="134.2" width="47.1" height="18" rx="9"/><text class="t" x="126" y="143.2" dy=".35em" text-anchor="middle">has-a</text></g><g class="wl"><rect x="105.6" y="237.4" width="40.8" height="18" rx="9"/><text class="t" x="126" y="246.4" dy=".35em" text-anchor="middle">owns</text></g><g class="wl"><rect x="105.6" y="340.6" width="40.8" height="18" rx="9"/><text class="t" x="126" y="349.6" dy=".35em" text-anchor="middle">is-a</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">Tch</text><circle class="n" cx="212" cy="40" r="18"/><text class="t" x="212" y="40" dy=".35em" text-anchor="middle">Crs</text><circle class="n" cx="40" cy="143.2" r="18"/><text class="t" x="40" y="143.2" dy=".35em" text-anchor="middle">Car</text><circle class="n" cx="212" cy="143.2" r="18"/><text class="t" x="212" y="143.2" dy=".35em" text-anchor="middle">Eng</text><circle class="n" cx="40" cy="246.4" r="18"/><text class="t" x="40" y="246.4" dy=".35em" text-anchor="middle">Hum</text><circle class="n" cx="212" cy="246.4" r="18"/><text class="t" x="212" y="246.4" dy=".35em" text-anchor="middle">Hrt</text><circle class="n" cx="40" cy="349.6" r="18"/><text class="t" x="40" y="349.6" dy=".35em" text-anchor="middle">Dog</text><rect class="n" x="187" y="334.6" width="50" height="30" rx="15"/><text class="t" x="212" y="349.6" dy=".35em" text-anchor="middle">Anml</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Relationships: Tch = Teacher, Crs = Course, Eng = Engine, Hum = Human, Hrt = Heart, Anml = Animal</figcaption></figure>
Answer frame. Open with the definition; draw the tree; major elements in two lines each, minor in one; for "basic elements" add relationships, code and diagram; close with "together these give reliable, reusable software".
Asked: [7 marks] (Jun 2025, Dec 2025) Define the object model. What are the core elements of an object model in OOP? Asked: [7 marks] (Dec 2023, Dec 2025) Explain object model and basic elements of OOPS.
Elements of OOPS
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. The elements of OOPS are the basic building blocks used to write object-oriented programs.
Key points.
- An object has state, behaviour and identity; a class is the template for objects.
- The four pillars are encapsulation, abstraction, inheritance and polymorphism.
- Relationships are association (uses), aggregation and composition (has-a) and inheritance (is-a).
- Message passing and dynamic binding let objects interact.
IO processing
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>A stream is a sequence of bytes flowing between a program and a device or file; C++ performs all I/O through stream objects, an input stream carrying data into the program and an output stream carrying data out.</mark>
Key points.
coutis the standard output stream, anostreamobject that writes to the screen with the insertion operator<<.cinis the standard input stream, anistreamobject that reads the keyboard with the extraction operator>>.- Both are predefined objects in
<iostream>inside namespacestd(std::cout, orusing namespace std;);cerrandclogalso exist. coutis buffered, soendlorflushforces output, andcinis tied tocout, so a prompt is flushed before input.- They are type safe (no
%d) and extensible by overloading<<and>>. - Operators chain (
cout << a << b;);<iomanip>manipulators format output:setw(n)width,setprecision(n)digits,fixedfixed-point,showpointtrailing zeros. cin >>stops at a space, sogetline(cin, s)reads a whole line; it converts text to the target type (5into afloatis 5.0).- Java reads
System.inthrough aScanner(nextInt(),nextLine()) and writes withSystem.out.println(x);.
cout << setw(6) << 42 << endl; // " 42"
cout << fixed << setprecision(2) << 3.14159; // 3.14
string s; getline(cin, s); // "Ravi Kumar" read whole
int x, sum = 0; while (cin >> x) sum += x; // EOF-controlled input loop
Scanner sc = new Scanner(System.in); int x, sum = 0;
while ((x = sc.nextInt()) != -1) sum += x; // sentinel -1 ends input
System.out.println("Sum = " + sum);
Stream classes. ios_base holds format flags and state; ios derives from it and uses a streambuf (device buffer, filebuf for files); istream and ostream derive from ios, iostream from both. Members: get()/put() (a character), getline() (a line), read()/write() (bytes). ifstream, ofstream, fstream are in <fstream>.
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u1-04" viewBox="0 0 583 262" width="583" height="262" role="img" aria-label="Stream class hierarchy (iostream derives from both istream and ostream; ios uses a streambuf)"><style>#dsfig-u1-04 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u1-04 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u1-04 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u1-04 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u1-04 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u1-04 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u1-04 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u1-04 .t{fill:#16181D;font-weight:500}#dsfig-u1-04 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u1-04 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u1-04 .dot{fill:#16181D}#dsfig-u1-04 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u1-04 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u1-04 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u1-04 .ah{fill:#454C5A}#dsfig-u1-04 .ah.hi{fill:#2340B8}#dsfig-u1-04 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u1-04 .wl .t{font-size:12px;font-weight:700}#dsfig-u1-04 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u1-04 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u1-04 .e{stroke:#B1B7C3}html.dark #dsfig-u1-04 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u1-04 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u1-04 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u1-04 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u1-04 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u1-04 .t{fill:#E6E8ED}html.dark #dsfig-u1-04 .t.inv{fill:#0F1115}html.dark #dsfig-u1-04 .kd{stroke:#E6E8ED}html.dark #dsfig-u1-04 .dot{fill:#E6E8ED}html.dark #dsfig-u1-04 .ann{fill:#8FA3FF}html.dark #dsfig-u1-04 .lbl{fill:#858D9C}html.dark #dsfig-u1-04 .ptr{fill:#8FA3FF}html.dark #dsfig-u1-04 .ah{fill:#B1B7C3}html.dark #dsfig-u1-04 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u1-04 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u1-04 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u1-04 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah4" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh4" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><line class="e" x1="154.5" y1="39" x2="154.5" y2="103"/><line class="e" x1="154.5" y1="103" x2="55.5" y2="167"/><line class="e" x1="154.5" y1="103" x2="154.5" y2="167"/><line class="e" x1="154.5" y1="103" x2="253.5" y2="167"/><line class="e" x1="55.5" y1="167" x2="55.5" y2="231"/><line class="e" x1="154.5" y1="167" x2="154.5" y2="231"/><line class="e" x1="253.5" y1="167" x2="253.5" y2="231"/><rect class="n" x="113" y="24" width="83" height="30" rx="8"/><text class="t" x="154.5" y="39" dy=".35em" text-anchor="middle">ios_base</text><circle class="n" cx="154.5" cy="103" r="17"/><text class="t" x="154.5" y="103" dy=".35em" text-anchor="middle">ios</text><rect class="n" x="18" y="152" width="75" height="30" rx="8"/><text class="t" x="55.5" y="167" dy=".35em" text-anchor="middle">istream</text><rect class="n" x="14" y="216" width="83" height="30" rx="8"/><text class="t" x="55.5" y="231" dy=".35em" text-anchor="middle">ifstream</text><rect class="n" x="117" y="152" width="75" height="30" rx="8"/><text class="t" x="154.5" y="167" dy=".35em" text-anchor="middle">ostream</text><rect class="n" x="113" y="216" width="83" height="30" rx="8"/><text class="t" x="154.5" y="231" dy=".35em" text-anchor="middle">ofstream</text><rect class="n" x="212" y="152" width="83" height="30" rx="8"/><text class="t" x="253.5" y="167" dy=".35em" text-anchor="middle">iostream</text><rect class="n" x="216" y="216" width="75" height="30" rx="8"/><text class="t" x="253.5" y="231" dy=".35em" text-anchor="middle">fstream</text><line class="e" x1="494.5" y1="37" x2="393.5" y2="101"/><rect class="n" x="449" y="22" width="91" height="30" rx="8"/><text class="t" x="494.5" y="37" dy=".35em" text-anchor="middle">streambuf</text><rect class="n" x="356" y="86" width="75" height="30" rx="8"/><text class="t" x="393.5" y="101" dy=".35em" text-anchor="middle">filebuf</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Stream class hierarchy (iostream derives from both istream and ostream; ios uses a streambuf)</figcaption></figure>
Stream errors. Each stream keeps state bits: goodbit (no error), eofbit (end of input), failbit (a logical failure, such as letters typed for an int) and badbit (a serious, unrecoverable error). They are tested with good(), eof(), fail() (true if failbit or badbit is set) and bad(), read with rdstate(), reset with clear(). A stream tests as a condition: if (!cin), while (cin >> x). A read hitting end of input sets eofbit and failbit together.
int x;
while (!(cin >> x)) { // input validation loop
cin.clear(); // reset failbit
cin.ignore(1000, '\n'); // discard bad input
}
Overloading << and >>. Friend functions let cout << p work for a user-defined class.
class P { int x; public:
friend ostream& operator<<(ostream& o, const P& p) { return o << p.x; }
friend istream& operator>>(istream& i, P& p) { return i >> p.x; } };
File stream objects. A stream is a sequence of bytes, and a file stream object is the bridge between the program and a physical file (ofstream writes, ifstream reads). Open with the constructor or f.open("o.txt", mode), check is_open(), release with close(). Modes (combined with |): ios::in read, ios::out write (truncates), ios::app append at end, ios::ate start at end but allow seeking, ios::binary binary I/O, ios::trunc discard old contents. For random access, seekg()/tellg() move and report the get (read) pointer, seekp()/tellp() the put pointer. Java uses FileInputStream/FileOutputStream and FileReader/FileWriter.
ofstream f("o.txt", ios::out); f << "Hello 42\nBye 7\n"; f.close();
ifstream g("o.txt"); string line;
while (getline(g, line)) cout << line << '\n'; // reads to EOF
g.clear(); g.seekg(0, ios::beg); // rewind to start
cout << g.tellg(); // 0
g.close();
Answer frame. Open with the stream definition; for cout/cin give points 1-8; for streams draw the hierarchy, then error flags, modes, the file example and seeking; close with "streams make C++ I/O type safe and extensible".
Pitfall: Writing that
cinandcoutare classes; they are objects ofistreamandostream.
Asked: [7 marks] (Nov 2022) Discuss the role of cout and cin in detail. Why they are used in C++ and from which class they belong to? Asked: [7 marks] (Nov 2022) What are the various stream classes and stream error? Explain them in brief. What are input and output streams? Give examples. Asked: [7 marks] (Dec 2023) What is meant by file stream objects? Give example. Asked: [7 marks] (Dec 2024) Describe the process of Input/Output (I/O) processing in object-oriented programming, including the handling of data types, type conversion and control statements.
Data Type
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>
Definition. A data type tells the compiler the kind of value a variable holds, its size and the operations allowed on it.
Key points.
- Built-in types are
int,float,double,charandbool; user-defined types areclass,structandenum. - A variable is declared as
type name = value;, and several together:int a = 1, b, c = 3;. - A constant is declared with
constin C++ (static final double PI = 3.14;in Java) and must be initialised because it cannot change. - Other constants are
#define MAX 100(macro),constexpr int N = 10;and aconstdata member set in the initialiser list. - Multiple objects are declared like variables:
Student s1, s2;in C++ andStudent s1 = new Student(), s2 = new Student();in Java; an array of objects isStudent st[3];. - A C++ reference is an alias (
int &r = a; r = 9;makesa9), and a Java reference variable holds an object's address.
Asked: [7 marks] (Jun 2023) Give example of multiple object declaration, variable declarations and constant declaration.
Type Conversion
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. Type conversion changes a value from one data type to another, either automatically or explicitly.
Key points.
- Implicit conversion is done by the compiler, promoting
inttodoubleorchartoint. - Explicit conversion (casting) is forced by the programmer: with
int a = 7,a / 2gives 3 but(double)a / 2orstatic_cast<double>(a) / 2gives 3.5. - Narrowing loses data:
int i = 3.9;stores 3. - Basic to class uses a one-argument constructor, class to basic a conversion function
operator int(), and class A to B a constructorB(A)oroperator B()in A.
class Dist { int m; public:
Dist(int x) : m(x) {} // basic -> class
operator int() { return m; } }; // class -> basic
Dist d = 5; int k = d; // k = 5
Control Statement
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Medium weight</span>
Definition. <mark>Control statements decide the order in which statements execute; they are of three kinds: selection, iteration and jump.</mark>
Key points.
- Selection statements choose a path:
ifruns a block when true,if-elsepicks one of two blocks, and anelse ifladder runs the first true branch. switchcompares one integer or char expression withcaselabels;breakstops fall-through anddefaulthandles no match.- Iteration statements repeat a block:
forandwhileare entry controlled (test first),do-whileis exit controlled (runs at least once). - In a
forloop the init runs once, then the condition is tested, then the body runs, then the update, and the condition is tested again. - Jump statements transfer control:
breakleaves a loop or switch,continueskips to the next iteration,gotojumps to a label, andreturnleaves a function. - Java has the same selection and loop statements, but
gotois only a reserved word, so Java uses labelledbreak/continue; Java'sswitchalso acceptsStringandenum.
if (cond) s; if (m >= 40) cout << "pass";
if (cond) s1; else s2; if (n % 2 == 0) cout << "even"; else cout << "odd";
for (init; cond; update) body; for (i = 1; i <= 3; i++) cout << i; // 123
while (cond) { body; update; } i = 1; while (i <= 3) { cout << i; i++; } // 123
do { body; update; } while (cond); i = 1; do { cout << i; i++; } while (i <= 3); // 123
if-else: [cond?] -T-> s1 -> next; -F-> s2 -> next
switch: [expr] -> matching case -> statements -> break -> next; no match -> default
entry: [cond?] -T-> body -> back to cond; -F-> exit (for, while)
exit: body -> [cond?] -T-> back to body; -F-> exit (do-while)
int m = 55, x = 2;
if (m >= 60) cout << "first"; else if (m >= 40) cout << "pass"; else cout << "fail";
switch (x) { case 2: cout << "two"; break; default: cout << "other"; } // two
for (int i = 0; i < 5; i++) { if (i == 3) break; cout << i; } // break: 012
for (int i = 0; i < 5; i++) { if (i == 2) continue; cout << i; } // continue: 0134
int k = 0; top: k++; if (k < 3) goto top; // goto: k = 3
int sq(int n) { return n * n; } // return: sq(4) = 16
outer:
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (i * j == 2) break outer; // leaves both loops at i = 1, j = 2
Answer frame. Open with the definition; draw the flowcharts; give syntax and an example per statement, then Java differences; close with "these give a program decision and repetition".
Asked: [7 marks] (Nov 2022, Jun 2024) Describe the various control statement with example. Asked: [7 marks] (Dec 2025) Discuss control statements, loops and arrays used in OOPS languages.
Loops
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>
Definition. A loop repeats a block of statements while a condition is true.
Key points.
- The
forloop is entry controlled, with init, condition and update in one line. - The
whileloop is entry controlled: it tests first and may run zero times. - The
do-whileloop is exit controlled: it tests after the body and always runs at least once. - The range-based
for(C++11),for (int v : a), visits every element, and Java's enhancedforhas the same form. - In a nested loop the inner loop finishes all its passes for each outer pass, so a 3 by 4 nest runs its body 12 times.
- Examples:
for (j = 1; j <= 5; j++) s += j;gives s = 15, and the while and do-while forms are in the Control Statement syntax block.
Asked: [7 marks] (Jun 2025) Explain different types of loops in OOP. Give examples of for, while and do-while loops.
Arrays
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. An array is a fixed-size collection of elements of the same type stored in contiguous memory and accessed by an index starting at 0.
Key points.
- A one-dimensional array is
int a[5] = {1,2,3,4,5};, and a 2D array is a table:int m[2][3] = {{1,2,3},{4,5,6}};givesm[1][2] = 6. - Arrays are traversed with an index loop and passed to functions with their size, since C++ does not check bounds.
- In Java arrays are objects:
int[] a = new int[5];holds five zeros,a.lengthgives 5, and a bad index throwsArrayIndexOutOfBoundsExceptionat run time. - In OOP, loops process arrays of objects and run inside member functions.
int sum(int a[], int n) { int s = 0; for (int i = 0; i < n; i++) s += a[i]; return s; }
int a[5] = {1, 2, 3, 4, 5}; cout << sum(a, 5); // 15
Student s[3]; for (int i = 0; i < 3; i++) s[i].input(); // array of objects
Last-minute revision
- OOP is bottom-up and data centred; procedural is top-down and function centred.
- The four pillars are encapsulation, abstraction, inheritance and polymorphism.
- Merits: reuse, modularity, security; demerits: vtable overhead, speed, memory, complexity.
- Object model: four major elements (abstraction, encapsulation, modularity, hierarchy) and three minor (typing, concurrency, persistence).
coutis anostreamobject using<<,cinanistreamobject using>>, both in<iostream>and namespacestd.- Stream state bits are goodbit, eofbit, failbit and badbit;
clear()resets them. - File streams
ifstream,ofstream,fstreamare in<fstream>; modes combine with|;seekg/seekpgive random access. (double)7 / 2is 3.5 but7 / 2is 3.- Array indices run from 0 to size-1.
Memory hooks
- A PIE: Abstraction, Polymorphism, Inheritance, Encapsulation are the four pillars.
- Object model: "A MEH" major (Abstraction, Modularity, Encapsulation, Hierarchy), "TCP" minor (Typing, Concurrency, Persistence).
- Insertion goes out (
<<points at cout); extraction comes in (>>points at the variable). - Do-while "does first, while checks later".
Coverage checklist
- Comparison with Procedural Programming: Q1, Q2 (also the two untagged Dec 2024 questions on differences).
- features of Object oriented paradigm– Merits and demerits of OO methodology: Q13, Q14, Q15, Q16.
- Object model: Q11, Q12.
- Elements of OOPS: covered by the topic section and Q12.
- IO processing: Q6, Q7, Q8, Q9.
- Data Type: Q5.
- Type Conversion: covered by the topic section and Q9.
- Control Statement: Q3, Q4.
- Loops: Q10.
- Arrays: covered by the topic section and Q4.