Download presentation
Presentation is loading. Please wait.
Published byBaby Fortune Modified over 10 years ago
1
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar), m_uiRating(iR) {} Here the derived class takes 2 variables one is used to initialise the derived class attribute the other is passed on to the base class constructor. 1
2
Constructors: Access Considerations DerivedClass x = new DerivedClass( 10, aPointer, true); Destroying an object occurs in the opposite order used to construct an object. The body of the derived-class destructor is executed first, and then the base-class destructor is called. When creating an object of a derived class, the program first calls the base-class constructor and then calls the derived-class constructor. The base-class constructor is responsible for initialising the inherited data members. 2
3
Constructors: Access Considerations Two important relationships are a base-class pointer can point to a derived-class object without an explicit type cast a base-class reference can refer to a derived-class object without an explicit type cast: Ordinarily, C++ requires that references and pointer types match the assigned types, but this rule is relaxed for inheritance. However, the rule relaxation is just in one direction. You can’t assign base-class objects and addresses to derived-class references and pointers: 3
4
Constructors: Access Considerations We create an array of BaseClass pointers and assign them to null. Arrays must be of the same type. We create a mixture of DerivedClass pointers and instantiate objects of the DerivedClass. We now have a group of different DerivedClass objects and their pointers. Next we assign the BaseClass pointers in the array to the derived class pointers. We end up with an array of pointers pointing to various DerivedClass objects. 4
5
Constructors: Access Considerations Inheritance is a ‘is a’ relationship Containment is where one class has another class as a member of that class is ‘has a’ relationship. Beware only use inheritance when there is a ‘is a’ relationship. Lawyers are often referred to as sharks. However you should not derive Lawyer from shark, it is not a true ‘is a’ relationship or is it? 5
6
Polymorphic Public Inheritance Objects of the derived class use the base-class functions. However there are times where you want a function to behave differently for the derived class than it does for the base class. You want a particular function to behave depending on the object that invokes it. Polymorphic behaviour You can have multiple behaviours for a function, depending on its context. 6
7
Polymorphic Public Inheritance There are two key mechanisms for implementing polymorphic public inheritance: Redefining base-class functions in a derived class Using virtual functions Virtual functions Inheritance can involve a derived class having the same function as the base class. Declaring the base class function as virtual allows your program to choose at runtime which function to run. 7
8
Polymorphic Public Inheritance A Base Class has a member function (its name is not important) the class designer knows the Base Class won’t use the member function for itself. However Derived Class’s will have a member function of the same name (signature/ID) that will do something (remember functions are doing things). If you declare the Base function as virtual when that member function name is used it will be used in the context of the DerivedClass not the BaseClass; this is polymorphic behaviour. 8
9
Polymorphic Public Inheritance We declare a virtual member function by preceding the function's prototype with the keyword virtual in the base class. virtual void FunctionName(); Polymorphism for( int i = 0; i < arraySize; i++) { pBaseClassArray[i]->FunctionName(); cout << endl; } The FunctionName in the derived class is called 9
10
Polymorphic Public Inheritance An overridden member function in a derived class has the same signature and return type (i.e., prototype) as the member function it overrides in its base class. If we declare the base class member function as virtual, we can override that function by the derived class to enable polymorphic behaviour. Declaring the base member function as virtual makes the program choose the member function version based on object type. 10
11
Polymorphic Public Inheritance The fact we may have a Base Class pointer pointing to the Derived Class is not relevant; the Derived Class member function will be invoked, that is at execution time based on the object type; not the pointer type. Note: The pointer has a type, the object has a type what we are saying is they can be different for the same instance of an object when using inheritance. This is because every instance of a Derived Class has a Base Class as a part of it. 11
12
Polymorphic Public Inheritance Even though certain functions are implicitly virtual because of a declaration made higher in the class hierarchy, explicitly declare these functions virtual at every level of the hierarchy to promote program clarity. Note: that the keyword virtual is used just in the function prototypes in the class declaration (h file), not in the function definitions (cpp file). virtual functions are another example of dynamic binding. 12
13
Polymorphic Public Inheritance When using virtual functions you also need to create a virtual destructor. If the destructors are virtual, the destructor corresponding to the object type is called. 13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.