Base Class to Derived Class Inheritance
Base Class to Derived Class Inheritance
BASE CLASS (Parent)
Vehiclespeed, fuelCapacity, startEngine()
── Inherited by (IS-A) ──▶
DERIVED CLASS (Child)
CarGets all Vehicle features automatically + adds: numberOfDoors, openSunroof()
Explanation
- The derived type obtains a base subobject and adds or adapts behaviour.
- The inheritance access mode determines how inherited public and protected members are exposed.
The 5 Forms of Inheritance
The 5 Forms of Inheritance
1. Single Inheritance
One child class inherits from one parent class.
Class A ──▶ Class B2. Multilevel Inheritance
A chain of inheritance (Grandfather ──▶ Father ──▶ Son).
Class A ──▶ Class B ──▶ Class C3. Multiple Inheritance
One child class inherits from two or more parent classes.
Class A + Class B ──▶ Class C4. Hierarchical Inheritance
One parent class is inherited by multiple child classes.
Class A ──▶ Class B & Class C5. Hybrid Inheritance
A mix of two or more inheritance types (e.g. Hierarchical + Multiple forming a diamond shape).
Explanation
- Single and multilevel inheritance describe one parent or a chain.
- Multiple, hierarchical and hybrid forms describe several parents, several children or combined patterns.
The Diamond Problem in Multiple Inheritance
The Diamond Problem in Multiple Inheritance
Top
Class A (Grandparent)
Defines base attribute:
int rollNoClass B (Parent 1)
Inherits rollNo from Class A.
Class C (Parent 2)
Inherits duplicate rollNo from Class A.
Ambiguity!
Class D (Child inherits both B & C)
Receives TWO copies of
rollNo! (Solved using virtual inheritance)Explanation
- Two paths from a derived class to one base can create two base subobjects.
- Virtual inheritance allows the most-derived object to share one base subobject instead.
The Two Branches of Polymorphism in C++
The Two Branches of Polymorphism in C++
1. Compile-Time (Early / Static Binding)
The compiler decides which function to call while compiling the code. Fast and fixed.
• Function Overloading
• Operator Overloading
• Function Overloading
• Operator Overloading
2. Run-Time (Late / Dynamic Binding)
The decision of which function to call is delayed until the program actually runs, based on the real object in RAM.
• Virtual Functions
• Virtual Functions
Explanation
- Overloading resolves among functions using information available at compile time.
- Virtual dispatch selects an override according to the object's runtime type.
Rules of Abstract Classes
Rules of Abstract Classes
No Direct Objects
You CANNOT create direct objects of an abstract class (
Shape s; will give an error!).Pointers are Allowed
You CAN create pointers to an abstract class (
Shape *ptr;) to achieve runtime polymorphism.Mandatory Overriding
Every derived child class MUST override and provide code for all pure virtual functions, or it becomes abstract too!
Acts as a Blueprint Contract
Ensures that every child shape will definitely implement
area().Explanation
- A class with an unimplemented pure virtual function is abstract.
- Pointers or references to it can still refer to suitable derived objects.
The 5 Non-Overloadable Operators in C++
The 5 Non-Overloadable Operators in C++
1. Dot Operator (.)
Class member access operator.
2. Member Pointer (.*)
Pointer-to-member dereference operator.
3. Scope Resolution (::)
Scope resolution operator.
4. Sizeof Operator
sizeof memory evaluator.5. Conditional Ternary Operator (?:)
Ternary conditional construct (
condition ? true : false).Explanation
- Some built-in operations have language-defined behaviour that user code cannot overload.
- Examples include scope resolution, direct member access and the conditional operator.
Pointer-Subscript Equivalence Rule
Pointer-Subscript Equivalence Rule
arr[i]
Standard array subscript notation. Accesses element at index i.
*(arr + i)
Pointer arithmetic offset and dereference. Identical internal CPU operation!
Explanation
- For a valid array index, indexing and dereferencing the corresponding pointer offset reach the same element.
- The offset advances in elements of the pointed-to type.
The Three Critical Roles of the 'this' Pointer
The Three Critical Roles of the 'this' Pointer
1. Resolving Name Conflicts
Separates class variables from function parameters when both have the exact same name (
this->age = age;).2. Method Chaining
Returning
*this allows you to chain multiple function calls in one single line: obj.setName("Aman").setAge(21);3. Self-Assignment Check
Used in operator overloading to make sure an object isn't being assigned to itself (
if (this != &obj)).Explanation
- The this pointer identifies the receiver of a non-static member call.
- It can disambiguate member names or let a method return the current object by reference.
C++ Input/Output Stream Architecture
C++ Input/Output Stream Architecture
01
Source Device
Keyboard (standard input) or disk file
➔
02
Input Stream (cin)
Flow of byte sequences into program memory
➔
03
C++ Program
Computes logic and prepares results
➔
04
Output Stream (cout)
Flow of formatted bytes delivered to screen
Explanation
- Extraction reads formatted values from an input stream.
- Insertion sends values to an output stream using its current formatting rules.
C++ Stream Class Inheritance Tree
C++ Stream Class Inheritance Tree
ios (Root Base Class)
The foundation class. Handles formatting flags and error states for all streams.
istream (Input Stream)
Inherits from
ios. Provides extraction operator (>>) and methods like get(), getline(). Defines cin.ostream (Output Stream)
Inherits from
ios. Provides insertion operator (<<) and methods like put(), write(). Defines cout, cerr, clog.iostream (Both Input & Output)
Inherits from both
istream and ostream (Multiple Inheritance) for two-way reading and writing.Explanation
- Input and output streams share common stream facilities.
- File and string streams specialise how characters reach or leave a stream.
The Three Pillars of Exception Handling
The Three Pillars of Exception Handling
1. try Block
Wrap the risky code here that might cause an error at runtime.
2. throw Statement
When an error occurs, you throw an error message or error code out of the
try block.3. catch Block
Catches the thrown error and displays a friendly message to the user instead of crashing!
Explanation
- Try encloses work that may fail and throw signals an exceptional condition.
- A matching catch handler decides how to respond.