1 / 13

Constructors: Access Considerations

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 ) {}

odetta
Download Presentation

Constructors: Access Considerations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constructors: Access Considerations DerivedClass::DerivedClass(intiR, floatfVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(intiR, floatfVar) : 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.

  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.

  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:

  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.

  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?

  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.

  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 virtualallows your program to choose at runtime which function to run.

  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 virtualwhen that member function name is used it will be used in the context of the DerivedClass not the BaseClass; this is polymorphic behaviour.

  9. Polymorphic Public Inheritance We declare a virtualmember function by preceding the function's prototype with the keyword virtualin the base class. virtualvoidFunctionName(); Polymorphism for( inti = 0; i < arraySize; i++) { pBaseClassArray[i]->FunctionName(); cout << endl; } The FunctionName in the derived class is called

  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 virtualmakes the program choose the member function version based onobject type.

  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 timebased on the object type;notthe 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.

  12. Polymorphic Public Inheritance Even though certain functions are implicitlyvirtual 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.

  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.

More Related