How unit 3 is examined
This unit covers inheritance and its five types, the is-a link, association, aggregation and composition, and abstract classes versus interfaces; inheritance, association and interfaces carry the marks.
Inheritance: purpose and its types
<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>Inheritance is the mechanism by which a derived class acquires the data members and member functions of an existing base class, so that existing code is reused and extended instead of rewritten.</mark>
Key points.
- Inheritance gives reusability: common code is written once in the base class and every derived class uses it without copying.
- It gives extensibility, because a derived class can add new members or redefine inherited functions without touching the base class.
- It builds a class hierarchy that models the real world and establishes the is-a relationship (a Student is a Person).
- It supports runtime polymorphism, since a base class pointer can hold any derived object and call overridden virtual functions.
- It saves time and reduces errors, as tested base code is reused and a change made in the base reaches all derived classes.
- Syntax:
class Derived : public Base { ... };. Private members of the base stay inaccessible to the derived class; protected and public ones are inherited.
Diagram. The five types (A, B, C, D are classes):
| Type | Structure | Syntax |
|---|---|---|
| Single | A to B | class B : public A |
| Multiple | A and B to C | class C : public A, public B |
| Multilevel | A to B to C | class C : public B |
| Hierarchical | A to B, C, D | class B : public A, class C : public A |
| Hybrid | mix, e.g. diamond | class D : public B, public C |
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-01" viewBox="0 0 252 252" width="252" height="252" role="img" aria-label="Multiple inheritance, C derives from both A and B"><style>#dsfig-u3-01 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-01 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-01 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-01 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-01 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-01 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-01 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-01 .t{fill:#16181D;font-weight:500}#dsfig-u3-01 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-01 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-01 .dot{fill:#16181D}#dsfig-u3-01 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-01 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-01 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-01 .ah{fill:#454C5A}#dsfig-u3-01 .ah.hi{fill:#2340B8}#dsfig-u3-01 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-01 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-01 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-01 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-01 .e{stroke:#B1B7C3}html.dark #dsfig-u3-01 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-01 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-01 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-01 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-01 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-01 .t{fill:#E6E8ED}html.dark #dsfig-u3-01 .t.inv{fill:#0F1115}html.dark #dsfig-u3-01 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-01 .dot{fill:#E6E8ED}html.dark #dsfig-u3-01 .ann{fill:#8FA3FF}html.dark #dsfig-u3-01 .lbl{fill:#858D9C}html.dark #dsfig-u3-01 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-01 .ah{fill:#B1B7C3}html.dark #dsfig-u3-01 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-01 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-01 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-01 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah5" 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="ahh5" 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="M117.5,195 L49.4,58.8" marker-end="url(#ah5)"/><path class="e" d="M134.5,195 L202.6,58.8" marker-end="url(#ah5)"/><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">A</text><circle class="n" cx="212" cy="40" r="18"/><text class="t" x="212" y="40" dy=".35em" text-anchor="middle">B</text><circle class="n" cx="126" cy="212" r="18"/><text class="t" x="126" y="212" dy=".35em" text-anchor="middle">C</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Multiple inheritance, C derives from both A and B</figcaption></figure>
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-02" viewBox="0 0 184 198" width="184" height="198" role="img" aria-label="Multilevel inheritance, A is grandparent of C"><style>#dsfig-u3-02 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-02 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-02 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-02 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-02 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-02 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-02 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-02 .t{fill:#16181D;font-weight:500}#dsfig-u3-02 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-02 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-02 .dot{fill:#16181D}#dsfig-u3-02 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-02 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-02 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-02 .ah{fill:#454C5A}#dsfig-u3-02 .ah.hi{fill:#2340B8}#dsfig-u3-02 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-02 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-02 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-02 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-02 .e{stroke:#B1B7C3}html.dark #dsfig-u3-02 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-02 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-02 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-02 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-02 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-02 .t{fill:#E6E8ED}html.dark #dsfig-u3-02 .t.inv{fill:#0F1115}html.dark #dsfig-u3-02 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-02 .dot{fill:#E6E8ED}html.dark #dsfig-u3-02 .ann{fill:#8FA3FF}html.dark #dsfig-u3-02 .lbl{fill:#858D9C}html.dark #dsfig-u3-02 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-02 .ah{fill:#B1B7C3}html.dark #dsfig-u3-02 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-02 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-02 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-02 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah6" 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="ahh6" 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="124" y1="39" x2="80" y2="103"/><line class="e" x1="80" y1="103" x2="36" y2="167"/><circle class="n" cx="124" cy="39" r="17"/><text class="t" x="124" y="39" dy=".35em" text-anchor="middle">A</text><circle class="n" cx="80" cy="103" r="17"/><text class="t" x="80" y="103" dy=".35em" text-anchor="middle">B</text><circle class="n" cx="36" cy="167" r="17"/><text class="t" x="36" y="167" dy=".35em" text-anchor="middle">C</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Multilevel inheritance, A is grandparent of C</figcaption></figure>
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-03" viewBox="0 0 186 134" width="186" height="134" role="img" aria-label="Hierarchical inheritance, one base with many derived classes"><style>#dsfig-u3-03 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-03 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-03 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-03 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-03 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-03 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-03 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-03 .t{fill:#16181D;font-weight:500}#dsfig-u3-03 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-03 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-03 .dot{fill:#16181D}#dsfig-u3-03 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-03 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-03 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-03 .ah{fill:#454C5A}#dsfig-u3-03 .ah.hi{fill:#2340B8}#dsfig-u3-03 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-03 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-03 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-03 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-03 .e{stroke:#B1B7C3}html.dark #dsfig-u3-03 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-03 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-03 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-03 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-03 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-03 .t{fill:#E6E8ED}html.dark #dsfig-u3-03 .t.inv{fill:#0F1115}html.dark #dsfig-u3-03 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-03 .dot{fill:#E6E8ED}html.dark #dsfig-u3-03 .ann{fill:#8FA3FF}html.dark #dsfig-u3-03 .lbl{fill:#858D9C}html.dark #dsfig-u3-03 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-03 .ah{fill:#B1B7C3}html.dark #dsfig-u3-03 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-03 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-03 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-03 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah7" 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="ahh7" 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="81" y1="39" x2="31" y2="103"/><line class="e" x1="81" y1="39" x2="81" y2="103"/><line class="e" x1="81" y1="39" x2="131" y2="103"/><circle class="n" cx="81" cy="39" r="17"/><text class="t" x="81" y="39" dy=".35em" text-anchor="middle">A</text><circle class="n" cx="31" cy="103" r="17"/><text class="t" x="31" y="103" dy=".35em" text-anchor="middle">B</text><circle class="n" cx="81" cy="103" r="17"/><text class="t" x="81" y="103" dy=".35em" text-anchor="middle">C</text><circle class="n" cx="131" cy="103" r="17"/><text class="t" x="131" y="103" dy=".35em" text-anchor="middle">D</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Hierarchical inheritance, one base with many derived classes</figcaption></figure>
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-04" viewBox="0 0 252 303.6" width="252" height="303.6" role="img" aria-label="Hybrid (diamond) inheritance, P Person, S Student, E Employee, W WorkingStudent"><style>#dsfig-u3-04 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-04 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-04 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-04 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-04 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-04 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-04 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-04 .t{fill:#16181D;font-weight:500}#dsfig-u3-04 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-04 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-04 .dot{fill:#16181D}#dsfig-u3-04 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-04 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-04 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-04 .ah{fill:#454C5A}#dsfig-u3-04 .ah.hi{fill:#2340B8}#dsfig-u3-04 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-04 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-04 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-04 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-04 .e{stroke:#B1B7C3}html.dark #dsfig-u3-04 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-04 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-04 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-04 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-04 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-04 .t{fill:#E6E8ED}html.dark #dsfig-u3-04 .t.inv{fill:#0F1115}html.dark #dsfig-u3-04 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-04 .dot{fill:#E6E8ED}html.dark #dsfig-u3-04 .ann{fill:#8FA3FF}html.dark #dsfig-u3-04 .lbl{fill:#858D9C}html.dark #dsfig-u3-04 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-04 .ah{fill:#B1B7C3}html.dark #dsfig-u3-04 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-04 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-04 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-04 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah8" 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="ahh8" 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="M51.6,136.7 L113.2,56.6" marker-end="url(#ah8)"/><path class="e" d="M200.4,136.7 L138.8,56.6" marker-end="url(#ah8)"/><path class="e" d="M114.4,248.5 L52.8,168.4" marker-end="url(#ah8)"/><path class="e" d="M137.6,248.5 L199.2,168.4" marker-end="url(#ah8)"/><circle class="n" cx="126" cy="40" r="18"/><text class="t" x="126" y="40" dy=".35em" text-anchor="middle">P</text><circle class="n" cx="40" cy="151.8" r="18"/><text class="t" x="40" y="151.8" dy=".35em" text-anchor="middle">S</text><circle class="n" cx="212" cy="151.8" r="18"/><text class="t" x="212" y="151.8" dy=".35em" text-anchor="middle">E</text><circle class="n" cx="126" cy="263.6" r="18"/><text class="t" x="126" y="263.6" dy=".35em" text-anchor="middle">W</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Hybrid (diamond) inheritance, P Person, S Student, E Employee, W WorkingStudent</figcaption></figure>
Example (multiple inheritance).
class A { public: int a = 1; };
class B { public: int b = 2; };
class C : public A, public B { public: int sum() { return a + b; } };
int main() { C c; cout << c.sum(); } // 3
Ambiguity and its fix. In the diamond, WorkingStudent gets two copies of Person's members (through Student and through Employee), so w.name is ambiguous. Two fixes: use the scope operator (w.Student::name), or make the shared base virtual so only one copy exists: class Student : virtual public Person.
class Person { protected: string name; int age; public: void input(){cin>>name>>age;} void output(){cout<<name<<age;} };
class Student : virtual public Person { protected: int rollno, marks; };
class Employee : virtual public Person { protected: int empcode; string designation; };
class WorkingStudent : public Student, public Employee {
public: void input(){ Person::input(); cin>>rollno>>marks>>empcode>>designation; }
void output(){ Person::output(); cout<<rollno<<marks<<empcode<<designation; } };
// input "R 20 5 90 7 Clerk" prints: R 20 5 90 7 Clerk
Answer frame. Open with the definition and the reusability idea; list the role points 1-5; draw the five diagrams with one-line syntax each; for a program question write the base classes, the derived class with public A, public B, and main() with output; for the ambiguity question draw the diamond, state the duplicate-copy problem, and close with the virtual base class fix.
Pitfall: Forgetting
virtualon the shared base in the diamond leaves two copies ofnameandageand an ambiguity error.
Asked: [14 marks] (Nov 2018) What is Inheritance? What are the different forms of Inheritance? Write suitable codes to illustrate them. Asked: [7 marks] (Nov 2019) Describe the role of inheritance in object oriented programming. Asked: [7 marks] (May 2019) Write a C++ program for multiple inheritance. Asked: [7 marks] (Dec 2020) What is an idea of reusability? Discuss different forms of inheritance with example. Asked: [7 marks] (Jun 2020) Explain the fundamental concept behind inheritance by taking an example. Define multiple, multilevel and hybrid inheritance with diagram and syntax. Asked: [7 marks] (Nov 2022) When does ambiguity arise in multiple inheritance? How can one resolve it? Develop a C++ program with derived class having name, age, rollno, marks, empcode, designation; base classes student and employee both inherited from person; with input() and output(). Asked: [7 marks] (Jun 2023, Dec 2024, Dec 2025) Describe the role of Inheritance in object-oriented programming. Give different types of inheritance. / Explain the purpose of inheritance and discuss its types with examples. / Explain inheritance and its types.
'is a' relationship
<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. <mark>The is-a relationship means a derived class is a specialised kind of its base class, and it is implemented by public inheritance.</mark>
Key points.
- A Car is a Vehicle and a Student is a Person, so the derived object can be used wherever the base object is expected.
- It is drawn in UML as a solid line with a hollow triangle arrowhead pointing to the base class (generalisation).
- It differs from has-a (aggregation or composition), where an object contains another object instead of being a kind of it.
- Disinheritance means a derived class refuses or hides an inherited behaviour, which signals that is-a was misused.
Association
<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>Association is a structural relationship between two classes in which objects of one class are linked to, and communicate with, objects of the other while keeping independent lifetimes.</mark>
Key points.
- Association is the "uses-a" or "knows-a" link, for example a Teacher teaches Students; neither object owns the other.
- Direction: uni-directional (only one class knows the other), bi-directional (both know each other) and reflexive or recursive (a class linked to itself, like Employee manages Employee).
- Multiplicity (cardinality) says how many objects take part: one-to-one (Person–Passport), one-to-many (Teacher–Students), many-to-many (Student–Course).
- Roles are the names at each end of the line, such as "teacher" and "student", and are written beside the association line.
- Special forms are aggregation (weak has-a, parts live independently) and composition (strong part-of, parts die with the whole).
- It is implemented as a pointer, a reference or an object member in one or both classes.
- Dependency is the weakest link: one class only uses another as a function parameter or local variable.
Diagram. UML symbols: association is a plain line, inheritance a hollow triangle, aggregation a hollow diamond at the whole, composition a filled diamond at the whole, dependency a dashed arrow.
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-05" viewBox="0 0 295 80" width="295" height="80" role="img" aria-label="Association, T Teacher teaches S Student, one-to-many"><style>#dsfig-u3-05 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-05 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-05 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-05 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-05 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-05 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-05 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-05 .t{fill:#16181D;font-weight:500}#dsfig-u3-05 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-05 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-05 .dot{fill:#16181D}#dsfig-u3-05 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-05 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-05 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-05 .ah{fill:#454C5A}#dsfig-u3-05 .ah.hi{fill:#2340B8}#dsfig-u3-05 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-05 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-05 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-05 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-05 .e{stroke:#B1B7C3}html.dark #dsfig-u3-05 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-05 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-05 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-05 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-05 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-05 .t{fill:#E6E8ED}html.dark #dsfig-u3-05 .t.inv{fill:#0F1115}html.dark #dsfig-u3-05 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-05 .dot{fill:#E6E8ED}html.dark #dsfig-u3-05 .ann{fill:#8FA3FF}html.dark #dsfig-u3-05 .lbl{fill:#858D9C}html.dark #dsfig-u3-05 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-05 .ah{fill:#B1B7C3}html.dark #dsfig-u3-05 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-05 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-05 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-05 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah9" 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="ahh9" 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 L236,40"/><g class="wl"><rect x="127.1" y="31" width="40.8" height="18" rx="9"/><text class="t" x="147.5" y="40" dy=".35em" text-anchor="middle">1..*</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">T</text><circle class="n" cx="255" cy="40" r="18"/><text class="t" x="255" y="40" dy=".35em" text-anchor="middle">S</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Association, T Teacher teaches S Student, one-to-many</figcaption></figure>
Comparison.
| Basis | Association | Aggregation | Composition |
|---|---|---|---|
| Meaning | General uses-a link | Weak has-a, whole and part | Strong part-of |
| Lifetime | Independent | Part outlives whole | Part dies with whole |
| Ownership | None | Shared, not exclusive | Whole owns exclusively |
| UML | Plain line | Hollow diamond | Filled diamond |
| Example | Teacher and Student | Department and Teacher | House and Rooms |
| Strength | Weakest | Medium | Strongest |
Kinds of class relationships. Association, aggregation, composition, inheritance (is-a, generalisation) and dependency (uses-a) are the five relationships among classes.
Short notes. Recursive association: a class associated with itself, e.g. an Employee has a manager who is also an Employee. Disinheritance: a derived class rejects or suppresses inherited members it should not have. Memory management: objects created with new must be freed with delete, and composition frees its parts in the destructor of the whole, while aggregated parts are freed by their own owner.
Answer frame. Open with the definition of association as a relationship between objects of independent classes; draw the UML line with roles and multiplicity; develop directions, multiplicity, then aggregation and composition; close with the comparison table and a real example. For "association and aggregation", give both definitions then the has-a difference; then continue with the abstract class topic below.
Pitfall: Calling aggregation and composition the same; the test is whether the part can exist after the whole is destroyed.
Asked: [7 marks] (May 2019, Jun 2023, Dec 2023) What is Association? Explain the various forms / different types of association with example. Asked: [7 marks] (Jun 2020, Nov 2022) Explain the concept of association and aggregation in object oriented approach. What is an Abstract class? Why are abstract classes used? Asked: [7 marks] (Dec 2024) Differentiate between association, aggregation and composition with real-world examples. Asked: [7 marks] (Nov 2022) What are the different kinds of relationship among classes? Explain. Asked: [14 marks] (Nov 2019) Explain briefly: i) Disinheritance ii) Recursive Association iii) Memory Management
Aggregation
<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>Aggregation is a special form of association that represents a has-a (whole and part) relationship in which the part can exist independently of the whole.</mark>
Key points.
- The whole class holds a pointer or reference to a part object created elsewhere, so the whole does not create or destroy it.
- Deleting the whole leaves the part alive: a Department can be closed while its Teachers still exist.
- A part may be shared by several wholes, which is not allowed in composition.
- It is drawn with a hollow diamond at the whole's end.
- Composition is the strong version: the whole creates the part, owns it exclusively and destroys it.
Example.
class Address { public: string city; Address(string c) : city(c) {} };
class Employee { string name; Address* addr;
public: Employee(string n, Address* a) : name(n), addr(a) {}
void show() { cout << name << " lives in " << addr->city; } };
// Address a("Bhopal"); { Employee e("Ravi", &a); e.show(); } cout << a.city;
// output: Ravi lives in Bhopal | Bhopal (Address survives Employee)
Comparison (association vs aggregation).
| Basis | Association | Aggregation |
|---|---|---|
| Relation | General uses-a | Whole-part has-a |
| Lifetime | Independent | Independent, but whole-part |
| Strength | Weaker | Stronger than association |
| UML | Plain line | Hollow diamond |
| Example | Doctor and Patient | Library and Books |
Answer frame. Open with "Aggregation is a has-a relationship with independent lifetimes"; draw the hollow-diamond diagram; give points 1-4, then the Address and Employee code with output; close by contrasting composition. For the differentiate question, use the table.
Asked: [7 marks] (Nov 2019, Dec 2023) How can we implement aggregation in OOP? Explain with an example. Asked: [7 marks] (Dec 2025) Differentiate between association and aggregation.
Concept of interfaces and Abstract classes
<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. ==An abstract class is a class with at least one pure virtual function (virtual void f() = 0;) that cannot be instantiated and exists only to be a base class; an interface is a pure abstract class that has only pure virtual functions and no data.==
Key points.
- A pure virtual function has no body in the base and forces every concrete derived class to override it; a class that does not override it stays abstract.
- An abstract class cannot create objects, but pointers and references to it are allowed and hold derived objects.
- It may also contain data members, constructors and ordinary functions with code, so it shares implementation among related classes.
- An interface (C++ has no interface keyword) is a class of only pure virtual functions and a virtual destructor; it is a contract that states what to do, not how.
- Abstract classes and interfaces enable runtime polymorphism: code written against the base pointer works for every present and future derived class.
- They give loose coupling and extensibility; frameworks and design patterns depend on them, and a class can implement several interfaces.
- Use an abstract class for related classes sharing code and state (Shape family); use an interface for unrelated classes sharing behaviour (Printable Bill and Photo).
Example.
class Shape { public: virtual double area() = 0; virtual ~Shape() {} };
class Circle : public Shape { double r; public: Circle(double x) : r(x) {} double area() { return 3.14*r*r; } };
class Rect : public Shape { double l, b; public: Rect(double x, double y) : l(x), b(y) {} double area() { return l*b; } };
// Shape* p[] = { new Circle(2), new Rect(3,4) }; each p[i]->area()
// output: 12.56 12
Comparison.
| Basis | Abstract class | Interface |
|---|---|---|
| Functions | Pure virtual and normal | Only pure virtual |
| Data members | Allowed | None |
| Implementation | Partial | None, pure abstraction |
| Constructor | Allowed | Not needed |
| Use | Related classes sharing code | Unrelated classes sharing a contract |
| Purpose | Common base plus shared code | Loose coupling, multiple inheritance |
Answer frame. Open with the definition and = 0; draw the Shape, Circle, Rect hierarchy; give points 1-5, then the code and output; for "interfaces vs abstract classes" put the table before the use cases; close with role in design: abstraction, loose coupling, polymorphism. For "interfaces facilitate polymorphism", show two implementations called through one base pointer.
Pitfall: Leaving out the virtual destructor in an interface, so deleting through the base pointer skips the derived destructor.
Asked: [7 marks] (Nov 2018) What is meant by Abstract class? Asked: [7 marks] (May 2019) Explain abstract base classes by taking a suitable example. Asked: [7 marks] (Dec 2020) What are abstract classes? Explain with suitable example. Asked: [7 marks] (Jun 2020) Write short notes on any two: i) Virtual functions ii) Operator overloading iii) Interface iv) Inline function Asked: [7 marks] (Nov 2022, Dec 2024, Dec 2025) Explain the concept of interfaces and abstract class in detail. / How do interfaces differ from abstract classes? Discuss the use of both. / Explain interfaces and abstract classes. Asked: [7 marks] (Dec 2023) What is an abstract class in OOP and how do we implement it? Explain with an example. Asked: [7 marks] (Dec 2024) Explain how interfaces can facilitate polymorphism. Provide code examples in C++. Asked: [7 marks] (Dec 2025) Discuss their role in software design.
Last-minute revision
- Inheritance: derived class reuses base members; syntax
class D : public B. - Five types: single, multiple, multilevel, hierarchical, hybrid.
- Multiple inheritance:
class C : public A, public B; the diamond creates duplicate copies of the top base. - Fix ambiguity with the scope operator
::orvirtual public Base. - Is-a is inheritance; has-a is aggregation or composition; uses-a is association or dependency.
- Association: independent lifetimes; multiplicity one-to-one, one-to-many, many-to-many.
- Aggregation: hollow diamond, part survives whole; composition: filled diamond, part dies with whole.
- Abstract class: at least one pure virtual function
= 0, no objects, may have data. - Interface: only pure virtual functions plus virtual destructor, no data.
- Abstract class shares code among related classes; interface gives a contract to unrelated classes.
Memory hooks
- IS-A means inherit, HAS-A means contain, USES-A means associate.
- Hollow diamond: the part can walk away; filled diamond: the part is stuck.
- Diamond problem: two copies of the top,
virtualkeeps one. - Pure virtual
= 0: "zero body, must override". - Abstract class = partly built house; interface = blueprint only.
Coverage checklist
- Inheritance: purpose and its types: Q Nov 2018 (14), Nov 2019, May 2019, Dec 2020, Jun 2020, Nov 2022 ambiguity program, Jun 2023 / Dec 2024 / Dec 2025.
- 'is a' relationship: definition and key points (no recent questions).
- Association: May 2019, Jun 2023 / Dec 2023, Jun 2020 / Nov 2022, Dec 2024 comparison, Nov 2022 kinds of relationship, Nov 2019 short notes.
- Aggregation: Nov 2019 / Dec 2023 implementation, Dec 2025 differentiate.
- Concept of interfaces and Abstract classes: Nov 2018, May 2019, Dec 2020, Jun 2020, Nov 2022 / Dec 2024 / Dec 2025, Dec 2023, Dec 2024 polymorphism, Dec 2025 role in design.