Skip to content
BT-205 · Basic Computer Engineering/Quick Revision Short Notes

Basic Computer Engineering (BT-205) - Unit 3 Short Notes

How unit 3 is examined

This unit covers C++ classes, constructors, friend functions, overloading, inheritance and an introduction to data structures; overloading, types of inheritance, classes, constructors, friend functions and the stack program carry the marks.

Object & 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">Medium weight</span>

Definition. <mark>A class is a user-defined blueprint that binds data members and member functions together, and an object is an instance of a class that occupies memory.</mark>

Key points.

  1. A class describes the data and behaviour once, and takes no memory for data until an object is created.
  2. An object has its own copy of the data members, and all objects of a class share the same member functions.
  3. Members are declared private, protected or public; private data is hidden and reached only through public functions, which is encapsulation.
  4. Objects interact by calling each other's public member functions, which is message passing.
  5. Grouping data and functions in one class gives modularity: each class can be written, tested and changed on its own.
  6. Classes give code organisation and maintainability, since a change inside a class does not disturb the code that uses it.
  7. A class can have several constructors with the same name but different parameter lists (constructor overloading); the compiler picks one by matching the arguments in the call.
Class Object
Blueprint, a user-defined data type Instance of the class
Logical, no memory for data Physical, takes memory
Declared once Created many times
class Student {...}; Student s1, s2;

Example.

class Student {
    int roll;                                   // private
public:
    void set(int r) { roll = r; }
    void show() { cout << roll; }
};
Student s1; s1.set(5); s1.show();               // prints 5

Answer frame. Open with the class and object definitions; give the comparison table; develop points 2-6 with the example; for the constructor question, show the three constructors from the next topic and state that the compiler resolves the call by argument types; close by saying classes give modular, maintainable code.

Asked: [7 marks] (Dec 2023, Dec 2024) Describe objects and classes; how do they interact and affect modularity? What is a class and how does it differ from an object? Give an example. Asked: [7 marks] (Jun 2023) Define objects and classes. Can a class in C++ have more than one constructor with the same name? Justify with an example.

Scope Resolution Operator

<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 scope resolution operator :: names the class or scope to which an identifier belongs.</mark>

Key points.

  1. It defines a member function outside its class, as void Student::show() { ... }.
  2. It accesses a global variable hidden by a local variable of the same name, as ::x.
  3. It names a base-class member explicitly, as A::show(), which also removes ambiguity in multiple inheritance.

Constructors & Destructors

<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>A constructor is a special member function with the class name and no return type that runs automatically when an object is created; a destructor, written ~ClassName(), runs automatically when the object is destroyed.</mark>

Key points.

  1. A constructor has the same name as the class, has no return type, and is called automatically at object creation to initialise the data members.
  2. A default constructor takes no arguments and gives fixed initial values.
  3. A parameterized constructor takes arguments so each object starts with its own values, as Student s(5);.
  4. A copy constructor, Student(const Student &s), builds a new object from an existing one, as in Student c = b;.
  5. Constructors can be overloaded, and a dynamic constructor allocates memory with new while initialising.
  6. A destructor has the tilde prefix, takes no arguments, has no return type, cannot be overloaded, and releases resources; objects are destroyed in reverse order of creation.

Example.

class Student {
    int roll;
public:
    Student() { roll = 0; cout << "Default\n"; }
    Student(int r) { roll = r; cout << "Param " << r << "\n"; }
    Student(const Student &s) { roll = s.roll; cout << "Copy\n"; }
    ~Student() { cout << "Destroy " << roll << "\n"; }
};
// main: Student a; Student b(5); Student c = b;
// Default, Param 5, Copy, then Destroy 5, Destroy 5, Destroy 0

Answer frame. Open with the constructor definition; list the types in order default, parameterized, copy with one line of syntax each; then the destructor properties; show the program with its output; close with the reverse-order destruction.

Asked: [7 marks] (Jun 2022, Dec 2024) What is a constructor and destructor? Explain them with a simple program. Asked: [7 marks] (Jun 2025) How many types of constructors are present in C++? Explain in detail.

Friend Functions

<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>A friend function is a non-member function declared inside a class with the friend keyword, which lets it access the private and protected members of that class.</mark>

Key points.

  1. It is declared inside the class with friend but defined outside, without the ClassName:: prefix and without the friend keyword.
  2. It is not a member function, so it is called like an ordinary function, not with the dot operator on an object.
  3. It has no this pointer, so it takes the object as an argument and reads members as b.w.
  4. It can be declared anywhere in the class, in the private or public part, with the same effect.
  5. Friendship is not inherited and is not mutual.
  6. Advantage: one function can work on the private data of two different classes. Disadvantage: it breaks encapsulation.

Example.

class Box {
    int w;
public:
    Box(int x) { w = x; }
    friend void show(Box b);
};
void show(Box b) { cout << "Width = " << b.w << endl; }
// main: Box b(7); show(b);   Output: Width = 7

Answer frame. Open with the definition and the friend keyword; state the characteristics in order; give the program; close with the pros and cons for encapsulation.

Asked: [7 marks] (Jun 2022, Dec 2023, Jun 2025) What is a friend function? Explain the purpose, declaration and definition of friend functions with an example program.

Inheritance

<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>Inheritance is the mechanism by which a derived class acquires the data members and member functions of a base class.</mark>

Key points.

  1. It gives reusability: existing code is used again, not rewritten.
  2. It gives extensibility, since the derived class adds its own members to the inherited ones.
  3. Syntax: class Derived : public Base { ... };
  4. Private members of the base are never accessible in the derived class; protected and public ones are.

Polymorphism

<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>Polymorphism means one name with many forms: the same function name or operator behaves differently in different contexts.</mark>

Key points.

  1. Compile-time (static) polymorphism is resolved by the compiler, through function overloading and operator overloading.
  2. Run-time (dynamic) polymorphism is resolved during execution, through virtual functions and base-class pointers.
  3. Dynamic binding means the function to call is chosen by the type of the object pointed to, not by the pointer type.

Overloading Functions & Operators

<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>Function overloading uses one function name for several functions that differ in the number or types of parameters; operator overloading gives an existing operator a special meaning for user-defined types.</mark>

Key points.

  1. Both are compile-time polymorphism: the compiler decides which version to call.
  2. Overloaded functions must differ in parameter number, type or order; a different return type alone is not enough.
  3. The compiler matches the call's arguments to a signature, so area(4), area(3,5) and area(2.0) call three different functions.
  4. Operator overloading uses the operator keyword. Syntax: return_type operator op (arguments).
  5. For a member operator function the left operand is the calling object, so a binary operator takes one argument and a unary operator takes none.
  6. It is useful because user-defined types then behave like built-in types with readable syntax, for complex numbers, vectors, matrices and string concatenation.
  7. Rules: a new operator cannot be created, precedence and associativity cannot change, and the number of operands stays fixed.
  8. These operators cannot be overloaded: ., ::, ?:, sizeof and .*.

Example.

int area(int s) { return s * s; }
int area(int l, int b) { return l * b; }
double area(double r) { return 3.14 * r * r; }
class Complex {
    int re, im;
public:
    Complex(int r = 0, int i = 0) { re = r; im = i; }
    Complex operator+(Complex c) { return Complex(re + c.re, im + c.im); }
    void show() { cout << re << "+" << im << "i\n"; }
};
// area(4)=16, area(3,5)=15, area(2.0)=12.56
// Complex a(2,3), b(4,5); (a+b).show() prints 6+8i

Answer frame. For the two-part question, open with both definitions, show the area overloads, then the Complex program with a+b read as a.operator+(b); close with compile-time polymorphism. For the operator-only question, open with the definition, give syntax, the program, then rules 6-8 and the closing line on readability.

Pitfall: Writing that . or :: can be overloaded, or that functions differing only in return type are overloads, loses marks.

Asked: [7 marks] (Nov 2022, Jun 2023) How is function overloading and operator overloading done? Explain them with a simple program. Asked: [7 marks] (Jun 2024) Explain the concept of operator overloading with an example. What is operator overloading and why is it useful in C++?

Types of Inheritance

<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 lets a derived class reuse and extend the members of one or more base classes; the forms are single, multiple, multilevel, hierarchical and hybrid.</mark>

Key points.

  1. Advantages: code reusability, extensibility, and a natural class hierarchy that also supports polymorphism.
  2. Syntax: class Derived : public Base1, public Base2 { ... }; where the access specifier can be public, protected or private.
  3. Single: one derived class from one base class.
  4. Multiple: one derived class from two or more base classes.
  5. Multilevel: a class derived from a derived class, forming a chain.
  6. Hierarchical: several classes derived from one base class.
  7. Hybrid: a combination of two or more forms; it can cause the diamond problem, where a class reaches the same base through two paths and gets two copies of it. Virtual base classes fix this.
  8. In multiple inheritance, a member name repeated in both bases is ambiguous and is resolved with Base::member.

Diagram. <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 596 209" width="596" height="209" role="img" aria-label="Left: single (A base, B derived). Right: multiple (C, D bases, E derived)"><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="M40,59 L40,148" marker-end="url(#ah5)"/><path class="e" d="M188,40 L406,40" marker-end="url(#ah5)"/><path class="e" d="M317,40 L406,40" 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="40" cy="169" r="18"/><text class="t" x="40" y="169" dy=".35em" text-anchor="middle">B</text><circle class="n" cx="169" cy="40" r="18"/><text class="t" x="169" y="40" dy=".35em" text-anchor="middle">C</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">D</text><circle class="n" cx="427" cy="40" r="18"/><text class="t" x="427" y="40" dy=".35em" text-anchor="middle">E</text><circle class="n" cx="556" cy="40" r="18"/><text class="t" x="556" y="40" dy=".35em" text-anchor="middle">F</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Left: single (A base, B derived). Right: multiple (C, D bases, E derived)</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 331 194" width="331" height="194" role="img" aria-label="tree diagram"><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="246.5" y1="37" x2="153.5" y2="101"/><line class="e" x1="153.5" y1="101" x2="60.5" y2="165"/><rect class="n" x="220.5" y="22" width="52" height="30" rx="8"/><text class="t" x="246.5" y="37" dy=".35em" text-anchor="middle">Base</text><rect class="n" x="112" y="86" width="83" height="30" rx="8"/><text class="t" x="153.5" y="101" dy=".35em" text-anchor="middle">Derived1</text><rect class="n" x="19" y="150" width="83" height="30" rx="8"/><text class="t" x="60.5" y="165" dy=".35em" text-anchor="middle">Derived2</text></svg></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 333 130" width="333" height="130" role="img" aria-label="tree diagram"><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="154.5" y1="37" x2="55.5" y2="101"/><line class="e" x1="154.5" y1="37" x2="154.5" y2="101"/><line class="e" x1="154.5" y1="37" x2="253.5" y2="101"/><rect class="n" x="128.5" y="22" width="52" height="30" rx="8"/><text class="t" x="154.5" y="37" dy=".35em" text-anchor="middle">Base</text><rect class="n" x="14" y="86" width="83" height="30" rx="8"/><text class="t" x="55.5" y="101" dy=".35em" text-anchor="middle">Derived1</text><rect class="n" x="113" y="86" width="83" height="30" rx="8"/><text class="t" x="154.5" y="101" dy=".35em" text-anchor="middle">Derived2</text><rect class="n" x="212" y="86" width="83" height="30" rx="8"/><text class="t" x="253.5" y="101" dy=".35em" text-anchor="middle">Derived3</text></svg></figure> Captions: the first tree is multilevel, the second is hierarchical.

Point Single inheritance Multiple inheritance
Base classes Exactly one Two or more
Syntax class D : public B class D : public B1, public B2
Complexity Simple More complex
Ambiguity None Possible, and the diamond problem
Resolution Not needed B1::f() or virtual base class
Example Student from Person Employee from Person and Job

Example.

class Person { protected: char name[20]; int age;
public: void getP() { cin >> name >> age; } };
class Job { protected: char desig[20]; float salary;
public: void getJ() { cin >> desig >> salary; } };
class Employee : public Person, public Job {
public: void show() { cout << name << " " << age << " " << desig << " " << salary; }
};
// main: Employee e; e.getP(); e.getJ(); e.show();
// Input: Ravi 30 Manager 50000   Output: Ravi 30 Manager 50000

Answer frame. For "define and explain types": open with the definition and advantages, give the syntax, draw single, multiple and multilevel diagrams, describe each in one line, add the program, close with the diamond problem. For the comparison: draw both diagrams, give the table, close with ambiguity. For the employee program: define multiple inheritance, declare the two bases, the derived class, and read and print in main().

Asked: [7 marks] (Jun 2022, Dec 2023) Define inheritance. Explain the types of inheritance, with implications for reuse and extensibility, with a suitable program. Asked: [7 marks] (Jun 2024) What is the difference between single inheritance and multiple inheritance? Asked: [7 marks] (Jun 2025) Write a program to read and print employee information using multiple inheritance.

Virtual functions

<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>A virtual function is a base-class member function declared with virtual so that a call through a base-class pointer runs the derived-class version.</mark>

Key points.

  1. It gives run-time polymorphism, as in A *p = &b; p->show(); printing "Derived" when show is virtual in A.
  2. Without virtual, the base version runs, because the call is bound at compile time.
  3. A pure virtual function has the form virtual void f() = 0;, and a class containing one is abstract and cannot be instantiated.

Introduction to Data Structures

<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>A data structure is a way of organising and storing data in memory so that it can be accessed and processed efficiently.</mark>

Key points.

  1. The choice of structure decides how fast the operations run and how much memory is used, which matters for efficient computation.
  2. Linear structures store elements in sequence: arrays, linked lists, stacks and queues.
  3. Non-linear structures store elements in hierarchy or network form: trees and graphs.
  4. Common operations are traversing, searching, inserting, deleting and sorting.
  5. Uses: a stack for undo and function calls, a queue for printer jobs, a tree for file folders, a graph for road maps.
  6. A stack follows LIFO: push inserts at the top, pop deletes from the top; push on a full stack is overflow and pop on an empty stack is underflow.

Diagram. <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 526 194" width="526" height="194" role="img" aria-label="tree diagram"><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><line class="e" x1="301.1" y1="37" x2="179.5" y2="101"/><line class="e" x1="301.1" y1="37" x2="422.8" y2="101"/><line class="e" x1="179.5" y1="101" x2="43.5" y2="165"/><line class="e" x1="179.5" y1="101" x2="142" y2="165"/><line class="e" x1="179.5" y1="101" x2="240.5" y2="165"/><line class="e" x1="179.5" y1="101" x2="315.5" y2="165"/><line class="e" x1="422.8" y1="101" x2="387" y2="165"/><line class="e" x1="422.8" y1="101" x2="458.5" y2="165"/><rect class="n" x="232.6" y="22" width="137" height="30" rx="8"/><text class="t" x="301.1" y="37" dy=".35em" text-anchor="middle">Data structures</text><rect class="n" x="146" y="86" width="67" height="30" rx="8"/><text class="t" x="179.5" y="101" dy=".35em" text-anchor="middle">Linear</text><rect class="n" x="14" y="150" width="59" height="30" rx="8"/><text class="t" x="43.5" y="165" dy=".35em" text-anchor="middle">Array</text><rect class="n" x="89" y="150" width="106" height="30" rx="8"/><text class="t" x="142" y="165" dy=".35em" text-anchor="middle">Linked list</text><rect class="n" x="211" y="150" width="59" height="30" rx="8"/><text class="t" x="240.5" y="165" dy=".35em" text-anchor="middle">Stack</text><rect class="n" x="286" y="150" width="59" height="30" rx="8"/><text class="t" x="315.5" y="165" dy=".35em" text-anchor="middle">Queue</text><rect class="n" x="373.8" y="86" width="98" height="30" rx="8"/><text class="t" x="422.8" y="101" dy=".35em" text-anchor="middle">Non-linear</text><rect class="n" x="361" y="150" width="52" height="30" rx="8"/><text class="t" x="387" y="165" dy=".35em" text-anchor="middle">Tree</text><rect class="n" x="429" y="150" width="59" height="30" rx="8"/><text class="t" x="458.5" y="165" dy=".35em" text-anchor="middle">Graph</text></svg></figure>

Example.

#define MAX 5
class Stack {
    int a[MAX], top;
public:
    Stack() { top = -1; }
    void push(int x) { if (top == MAX - 1) cout << "Overflow\n"; else a[++top] = x; }
    void pop() { if (top == -1) cout << "Underflow\n"; else cout << a[top--] << " deleted\n"; }
};
// main: push(10); push(20); pop(); pop(); pop();
// Output: 20 deleted, 10 deleted, Underflow

Answer frame. For "explain data structures": open with the definition, draw the classification, give each type with its use, list the operations, close with the choice of structure. For the stack program: state that top = -1 means empty, show push with the overflow check, pop with the underflow check, then main.

Asked: [7 marks] (Nov 2022) Write a C++ program to insert and delete an element from stack. Asked: [7 marks] (Jun 2023) Explain data structures in detail.

Last-minute revision

  • A class is a blueprint with no data memory; an object is an instance that takes memory.
  • A constructor has the class name, no return type, and runs automatically at creation; the types are default, parameterized and copy.
  • A destructor is ~Name(), has no arguments, cannot be overloaded, and runs in reverse order of creation.
  • A friend function is a non-member declared with friend that reads private members; it is called without the dot operator.
  • :: defines members outside the class and reaches hidden globals.
  • Function overloading needs different parameter lists; return type alone does not count.
  • Operator syntax: return_type operator op(args); . :: ?: sizeof .* cannot be overloaded.
  • The five inheritance types are single, multiple, multilevel, hierarchical and hybrid.
  • Diamond problem: two copies of one base through two paths; the fix is a virtual base class.
  • A virtual function gives run-time polymorphism through a base pointer; = 0 makes it pure and the class abstract.
  • Stack is LIFO: overflow when top == MAX-1, underflow when top == -1.

Memory hooks

  • CPD: Constructor types are Default, Parameterized, Copy.
  • SMMHH: the inheritance types are Single, Multiple, Multilevel, Hierarchical, Hybrid.
  • Friend is a guest with a key to the private room, but not a family member of the class.
  • Overloading is compile-time (early); virtual is run-time (late).
  • Stack is a plate pile: last in, first out.

Coverage checklist

  • Object & Classes: Dec 2023 / Dec 2024 class and object question; Jun 2023 objects, classes and multiple constructors.
  • Scope Resolution Operator: no past question; definition and uses covered.
  • Constructors & Destructors: Jun 2022 / Dec 2024 constructor and destructor; Jun 2025 types of constructors.
  • Friend Functions: Jun 2022 / Dec 2023 / Jun 2025 friend function.
  • Inheritance: no past question; definition and syntax covered.
  • Polymorphism: no past question; compile-time and run-time covered.
  • Overloading Functions & Operators: Nov 2022 / Jun 2023 both overloads; Jun 2024 operator overloading.
  • Types of Inheritance: Jun 2022 / Dec 2023 types; Jun 2024 single vs multiple; Jun 2025 employee program.
  • Virtual functions: no past question; virtual and pure virtual covered.
  • Introduction to Data Structures: Nov 2022 stack program; Jun 2023 data structures.
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in