C++ Addendum: Inheritance of Special Member Functions Constructors Destructor Construction and Destruction Order Assignment Operator
What s s Not Inherited? The following methods are not inherited: Constructors Destructor Assignment operator We will see that Definition of these methods is in a sense recursive Overriding these methods is always a refinement
Reminder: The Default Constructor Constructor with no arguments Called automatically when an object is created without an explicit constructor call Recursion base: Empty for: Basic types: int, char, double,... Pointers to: basic types, classes, structures Generated automatically by the compiler if no constructor is defined by programmer Recursive step (compiler generated default constructor): Call default constructors of all base class subobjects Call default constructors of all data member subobjects Use the order they were declared in.
Constructors and Inheritance Constructor methods are not inherited Initialization List: The stuff between the constructor header and body Preceded with a colon : class Stack { int *buff, *sp; public: Stack(int n) : buff(new int[n]), sp(buff + n) { //... Defining constructors in a derived class: Strive to make constructor body empty Use initialization list Base class subobjects can only be initialized in initialization list If not initialized there, then default constructor is called const data members can only be initialized in initialization list Order of application: Same as that of default constructor
Construction Order Whenever a constructor is invoked: All base class subobjects must be constructed If in initialization list: invoke appropriate constructor Otherwise, apply default constructor All data member subobjects must be constructed If in initialization list: invoke appropriate constructor Otherwise, apply default constructor Body of user defined constructor is called Fixed construction order: Order of items in initialization list is irrelevant Construction order: Base class subobjects (in the order of their definition) Data member subobjects (in the order of their definition) Body of constructor: It is empty for compiler-generated constructors
Quiz: Order of Construction struct A { A(void) { cout << A ; struct B { B(void) { cout << B ; struct C { C(void) { cout << C ; What will be printed? struct X: public A { B b; X(void): b() { cout << X ; struct Y: public X { C c; Y(void): X() { cout << Y ; struct Z: public Y { X x; B b; Z(void): b(), x(), Y() { cout << Z ; z; Solution
Reminder: Destructor Called automatically when object is destroyed Doesn t have any arguments Recursion base: Empty for: Basic types: int, char, double,... Pointers to: basic types, classes, structures Generated automatically by the compiler if not defined by the programmer Recursive step: Call body of destructor: user defined/compiler generated Call destructor of all data member subobjects Reverse order of declaration Call destructor of all base class subobjects Order: reverse order of construction
Quiz: Order of Destruction struct A { ~A(void) { cout << A ; struct B { ~B(void) { cout << B ; struct C { ~C(void) { cout << C ; What will be printed? struct X: public A { B b; ~X(void) { cout << X ; struct Y: public X { C c; ~Y(void) { cout << Y ; struct Z: public Y { X x; B b; ~Z(void){ cout << Z ; z;
The Copy Constructor Reminder: Constructor of form Class::Class(const Class &X) Called automatically in: Initialization Call by value Function returning value Recursion base: Member-wise copy for basic types Generated automatically by the compiler if no copy constructor is defined by programmer Recursive step (for compiler generated copy constructors): Call copy constructor of all base class subobjects Call copy constructor of all data members Use the order they were defined in. Q: What s the recursion of user defined copy constructors?
The Assignment Operator Reminder: Must be defined as member function: class X { public: void operator = (const X& right); Recursion base: Empty for basic types and pointers. Generated automatically by the compiler if no assignment operator defined by programmer Recursive step (compiler defined assignment operators): Call assignment operators of all base class subobjects Call assignment operators of all data member subobjects Use the order they were declared in. Call body of assignment operator: user defined/compiler generated Q: What s the recursion of user defined assignment operators?