Unit 3: Inheritance and overloading
OOPs using C++ notes · PTU syllabus (BSIT203/BSBC203)
On this page
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)
public inheritance
public
protected
protected inheritance
protected
protected
private inheritance
private
private
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 member | Public mode | Protected mode | Private mode |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | not inherited (not accessible) | not accessible | not accessible |
Exam tip
Private members of a base class are never directly accessible in the derived class, whatever the mode.
Topic 2
Single, multilevel and other types of 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.
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 member | Public inheritance | Protected inheritance | Private inheritance |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | not accessible | not accessible | not accessible |
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; }
};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));
}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
- Q1.What is a derived class?
- Q2.How are protected members inherited under public inheritance?
- Q3.Distinguish single and multilevel inheritance.
- Q4.What is containership?
- Q5.What is function overloading?
- Q6.Name two operators that cannot be overloaded.
Long-answer questions
- Q1.Explain base and derived classes and visibility modes.
- Q2.Explain single and multilevel inheritance with programs.
- Q3.Explain nesting of classes and protected member inheritance.
- 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.
