oops · high
C++: Name Mangling, extern C, Inheritance Access Rules, Virtual Destructor
Name mangling: C++ encodes function parameter types into linker symbols to support overloading. GCC/Clang: Itanium ABI — _Z3addii = add(int,int). Demangle: c++filt _Z3addii → add(int,int); nm -C to view. extern 'C': disables mangling; required for C interop, JNI, Python ctypes, dlsym(). Header pattern: #ifdef __cplusplus extern 'C' { #endif ... #ifdef __cplusplus } #endif. Inheritance access: public = interface unchanged (IS-A), protected = public→protected (further subclassing), private = all→private (HAS-A via inheritance; prefer composition). Not inherited: constructors, destructors, operator=, friend declarations. Private members exist in memory layout but are inaccessible by name in derived. C++11: using Base::Base; imports all base constructors into derived. Virtual destructor rule: any virtual function → virtual destructor; without it, delete via base pointer = UB + leak.
Name mangling: C++ encodes param types in symbol names (_Z3addii = add(int,int)); extern 'C' disables mangling for C interop; inheritance access: public=unchanged, protected=all→protected, private=all→private; NOT inherited: ctors, dtor, operator=, friends.
The code
// C++ name mangling — encodes type signature into symbol name// Enables: function overloading, namespaces, templates
// C++ source:int add(int a, int b);int add(double a, double b);namespace math {int add(int a);}
// Linker symbols (GCC Itanium ABI mangling):// _Z3addii → add(int, int)// _Z3adddd → add(double, double)// _ZN4math3addEi → math::add(int)
// Demangle with: c++filt _Z3addii → add(int, int)// See symbols: nm -C libfoo.so// Or: objdump -t foo.o | grep add
// extern "C" — disables mangling for C interop:#ifdef __cplusplusextern "C" {#endifint c_function(int x); // C linkage: symbol = c_function#ifdef __cplusplus}#endif// Without extern "C": C++ .so exports _Z10c_functionii// C caller dlsym("c_function") fails — symbol not found
// OOP inheritance access rules:// class D : public B → public B members stay public in D// class D : protected B → public B members → protected in D// class D : private B → public B members → private in D// private base = same as composition, just with D IS-A B for internal code
// What derived class does NOT inherit:// - Constructors (called automatically but not inherited)// - Destructor// - operator=// - Friend functions// - Private members (exist in memory but inaccessible)What this lesson walks through
- 01Name mangling — how C++ enables function overloading
- 02extern 'C' — disabling mangling for C interop
- 03Inheritance access rules — the 3 modes
- 04What derived classes do NOT inherit
- 05OOP pillars — interview answer template
- 06Virtual destructor — the most asked OOP trap
C++ supports function overloading — multiple functions with the same name but different parameters. The linker needs unique symbol names. C++ 'mangles' each function's name by encoding the parameter types into the symbol. GCC/Clang use the Itanium ABI mangling scheme. This is why C++ symbols in .so files look like _Z3addii. nm -C or c++filt demangles them back to human-readable form.
See it animated — step by step, at your own pace
Unlock the full interactive walkthrough of C++: Name Mangling, extern C, Inheritance Access Rules, Virtual Destructor and 100+ animated C++ interview lessons.