Unit 3 of 4 · B.Sc IT Sem 2

Unit 3: Inheritance and overloading

OOPs using C++ notes · PTU syllabus (BSIT203/BSBC203)

3 min read6 topics10 exam questions
On this page
  1. Unit summary
  2. Base and derived classes and visibility modes
  3. Single, multilevel and other types of inheritance
  4. Protected member inheritance
  5. Nesting of classes
  6. Function overloading
  7. Operator overloading: unary and binary
  8. Key terms
  9. Quick revision
  10. Important questions

Unit summary

Inheritance reuses code and overloading makes operators and functions work naturally with new types. This unit covers base and derived classes, visibility modes, single and multilevel inheritance, protected member inheritance, nesting of classes, and function and operator overloading — unary and binary.

After this unit you can

  • Derive classes with different visibility modes
  • Implement single and multilevel inheritance
  • Use nested classes
  • Overload functions and unary and binary operators

PTU syllabus topics

  • Base and derived classes
  • visibility modes (private/public/protected)
  • single and multilevel inheritance
  • protected member inheritance
  • nesting of classes
  • function and operator overloading (unary and binary)
ComparisonInheritance visibility modes
Base public members become
Base protected members become

public inheritance

public

protected

protected inheritance

protected

protected

private inheritance

private

private

1

Topic 1

Base and derived classes and visibility modes

Inheritance is the mechanism by which a derived (child) class acquires the data and functions of a base (parent) class. Syntax: class Derived : mode Base { … };

Base memberPublic modeProtected modePrivate mode
publicpublicprotectedprivate
protectedprotectedprotectedprivate
privatenot inherited (not accessible)not accessiblenot accessible

Exam tip

Private members of a base class are never directly accessible in the derived class, whatever the mode.

2

Topic 2

Single, multilevel and other types of inheritance

ClassificationTypes of inheritance
Inheritance
  • Single

    One base → one derived

  • Multiple

    Two or more bases → one derived

  • Multilevel

    A → B → C (chain)

  • Hierarchical

    One base → many derived

  • Hybrid

    A combination of two or more types

cppclass Student {                       // base
protected:
    int roll;
public:
    void getRoll(int r) { roll = r; }
};
class Exam : public Student {         // multilevel: level 2
protected:
    int m1, m2;
public:
    void getMarks(int a, int b) { m1 = a; m2 = b; }
};
class Result : public Exam {          // level 3
public:
    void show() { cout << roll << " total = " << m1 + m2; }
};

Ambiguity in multiple and hybrid inheritance: if two base classes inherit from the same grandparent, the derived class gets two copies (the diamond problem). It is solved with a virtual base class: class B : virtual public A.

3

Topic 3

Protected member inheritance

  • protected members are accessible in the class and its derived classes but not outside — the middle ground between private and public.
Base memberPublic inheritanceProtected inheritancePrivate inheritance
publicpublicprotectedprivate
protectedprotectedprotectedprivate
privatenot accessiblenot accessiblenot accessible
4

Topic 4

Nesting of classes

  • Nested class: a class declared inside another class — used to hide helper classes; containership (composition): a class contains objects of other classes ("has-a" relationship), as opposed to inheritance ("is-a").
cppclass Car {
    class Engine { public: int hp = 120; };   // nested class
    Engine e;                                 // containership
public:
    int power() { return e.hp; }
};
5

Topic 5

Function overloading

Function overloading means several functions with the same name but different parameter lists (number or type of arguments). The compiler chooses the right one.

cppdouble area(double r)            { return 3.14159 * r * r; }   // circle
double area(double l, double b)  { return l * b; }             // rectangle
double area(double a, double b, double c) {                    // triangle (Heron)
    double s = (a + b + c) / 2;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}
6

Topic 6

Operator overloading: unary and binary

Operator overloading gives an operator a special meaning for user-defined types using an operator function: ReturnType operator+(Arg);

  • Unary operators (++, −−, −) as member functions take no argument.
  • Binary operators (+, −, ×) as member functions take one argument (the right operand).
cppclass Complex {
    float re, im;
public:
    Complex(float r = 0, float i = 0) { re = r; im = i; }
    Complex operator+(Complex c) { return Complex(re + c.re, im + c.im); }
};
Complex c3 = c1 + c2;   // calls c1.operator+(c2)

Rules: only existing operators can be overloaded; at least one operand must be a user-defined type; precedence and number of operands cannot change; the operators ., ::, ?:, sizeof and .* cannot be overloaded.

cppclass Counter {
    int c = 0;
public:
    Counter& operator++() { ++c; return *this; }           // unary prefix ++
    Counter operator+(const Counter &o) const {             // binary +
        Counter t; t.c = c + o.c; return t;
    }
};

Key terms

Derived class
Class inheriting from a base class
Visibility mode
Public, protected or private inheritance
Multilevel inheritance
Class derived from a derived class
Containership
A class containing objects of another class
Operator overloading
Giving operators meaning for user-defined types

Quick revision

  • Base and derived classes; public, protected, private modes and access table.
  • Single, multilevel, multiple, hierarchical, hybrid inheritance.
  • Constructors in inheritance.
  • Nested classes and containership.
  • Function overloading; unary and binary operator overloading; operators that cannot be overloaded.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.What is a derived class?
  2. Q2.How are protected members inherited under public inheritance?
  3. Q3.Distinguish single and multilevel inheritance.
  4. Q4.What is containership?
  5. Q5.What is function overloading?
  6. Q6.Name two operators that cannot be overloaded.

Long-answer questions

  1. Q1.Explain base and derived classes and visibility modes.
  2. Q2.Explain single and multilevel inheritance with programs.
  3. Q3.Explain nesting of classes and protected member inheritance.
  4. Q4.Explain function overloading and unary and binary operator overloading.

Stuck on this unit?

Message SBS on WhatsApp for help with OOPs using C++, or to ask about studying B.Sc IT at Synetic.

WhatsApp us