H-SC COMS 262 - Lecture 22 - Inheritance Fundamental Functions

Unformatted text preview:

Inheritance of ConstructorsInheritance of DestructorsInheritance of the Assignment OperatorAssignmentInheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance: The Fundamental FunctionsLecture 22Chapter 1Robb T. KoetherHampden-Sydney CollegeFri, Mar 6, 2009Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentOutline1Inheritance of Constructors2Inheritance of Destructors3Inheritance of the Assignment Operator4AssignmentInheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of ConstructorsConstructor Rules1A derived-class constructor will automatically invoke thebase-class default constructor, unless instructedotherwise.2We may instruct the derived-class constructor to invokea specific base-class constructor.3The base-class constructor is invoked before thederived-class constructor is executed.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of ConstructorsExample (A List Hierarchy)Suppose we create a List base class and then derivean ArrayList class and a LinkedList class from it.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of ConstructorsExample (A List Hierarchy)The ArrayList and LinkedList classes havemSize in common.Therefore, we could put mSize in the List base class.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of ConstructorsExample (A List Hierarchy)When we construct an ArrayList usingArrayList list(5, 123);what would happen?The ArrayList constructor will first call a Listconstructor which will initialize mSize.Then it will initialize element.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of ConstructorsExample (A List Hierarchy)When we construct an ArrayList usingArrayList list(5, 123);what would happen?The ArrayList constructor will first call a Listconstructor which will initialize mSize.Then it will initialize element.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of ConstructorsExample (A List Hierarchy)When we construct an ArrayList usingArrayList list(5, 123);what would happen?The ArrayList constructor will first call a Listconstructor which will initialize mSize.Then it will initialize element.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of ConstructorsExample (A List Hierarchy)Which List constructor will it call?Unless we specify otherwise, it will call the default Listconstructor.Uh-oh.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of ConstructorsExample (A List Hierarchy)Which List constructor will it call?Unless we specify otherwise, it will call the default Listconstructor.Uh-oh.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of ConstructorsExample (A List Hierarchy)Which List constructor will it call?Unless we specify otherwise, it will call the default Listconstructor.Uh-oh.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInvoking the Base Class ConstructorBase Class ConstructorArrayList(parameters) : List(parameters){body of the ArrayList constructor}We may specify other constructors through aninitializer.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInvoking the Base Class ConstructorBase Class ConstructorArrayList(int sz, int value) : List(sz){if (mSize == 0)element = NULL;else{element = new int[mSize];for (int i = 0; i < mSize; i++)element[i] = value;}return;}Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of DestructorsDestructor Rules1The derived-class destructor automatically invokes thebase-class destructor.2The base-class destructor is invoked after thederived-class destructor is executed.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of the Assignment OperatorAssignment Operator Rules1The automatic assignment operator invokes theassignment operator for the base class.2A programmer-defined assignment operator must copythe base-class members. This is a problem if thebase-class members are private.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of the Assignment OperatorInheritance of the Assignment Operatorclass A {private: int aMem;};class B : public A {private: int bMem;};B& B::operator=(const B& b){aMem = b.aMem // IllegalbMem = b.bMem;}One can use the scope operator :: to invoke theassignment operator of the base class.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of the Assignment OperatorInheritance of the Assignment Operatorclass A {private: int aMem;};class B : public A {private: int bMem;};B& B::operator=(const B& b){A::operator=(b); // LegalbMem = b.bMem;}One can use the scope operator :: to invoke theassignment operator of the base class.Inheritance:TheFundamentalFunctionsRobb T.KoetherInheritance ofConstructorsInheritance ofDestructorsInheritance oftheAssignmentOperatorAssignmentInheritance of the Assignment OperatorInheritance of the Assignment Operatorclass A {protected: int aMem;};class B : public A {private: int bMem;};B& B::operator=(const B& b){aMem =


View Full Document

H-SC COMS 262 - Lecture 22 - Inheritance Fundamental Functions

Download Lecture 22 - Inheritance Fundamental Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 22 - Inheritance Fundamental Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 22 - Inheritance Fundamental Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?