Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructor Operation tMyn1 Constructor Operation in a Derived Class So far the default base class constructor has been called automatically. We can arrange.

Similar presentations


Presentation on theme: "Constructor Operation tMyn1 Constructor Operation in a Derived Class So far the default base class constructor has been called automatically. We can arrange."— Presentation transcript:

1 Constructor Operation tMyn1 Constructor Operation in a Derived Class So far the default base class constructor has been called automatically. We can arrange to call a particular base class constructor from the derived class constructor. This will enable us to initialize the base class data members with a constructor other than the default. It also allows us to choose one or the other base class constructor, depending on the data supplied to the derived class constructor.

2 Constructor Operation tMyn2 Let’s start with a simple situation. There is defined a derived class constructor which has arguments and a default base class constructor.

3 Constructor Operation tMyn3 #include "stdafx.h" #include using namespace System; using namespace std; class Box { public: Box(); ~Box(); void baseDimensions(); protected: double length; double breadth; double height; };

4 Constructor Operation tMyn4 Box::Box() { cout<<"Base class constructor, object "<<this<<endl; cout<<"Input length: "; cin>>length; cin.get(); cout<<"Input breadth: "; cin>>breadth; cin.get(); cout<<"Input height: "; cin>>height; cin.get(); } Box::~Box() { cout<<"Base class destructor, object "<<this<<endl; }

5 Constructor Operation tMyn5 void Box::baseDimensions() { cout<<"length="<<length<<endl <<"breadth="<<breadth<<endl <<"height="<<height<<endl; } class Carton:public Box { public: Carton(double=75.5); ~Carton(); void derivedDimensions(); private: double weight; };

6 Constructor Operation tMyn6 Carton::Carton(double wv) { cout<<"Derived class constructor, object "<<this<<endl; weight=wv; } Carton::~Carton() { cout<<"Derived class destructor, object "<<this<<endl; } void Carton::derivedDimensions() { cout<<"length="<<length<<endl <<"breadth="<<breadth<<endl <<"height="<<height<<endl <<"weight="<<weight<<endl; }

7 Constructor Operation tMyn7 int main(array ^args) { Box first=Box(); first.baseDimensions(); Carton second=Carton(); second.derivedDimensions(); Carton* third=new Carton(55.5); third->derivedDimensions(); delete third; return 0; }

8 Constructor Operation tMyn8

9 Constructor Operation tMyn9 Again as it was seen: destroying a derived class object – either when it goes out of scope or, in the case of an object in the free store, when you delete it – involves both the derived class destructor and the base class destructor. We have also seen that the objects are destroyed in the reverse order from which they were created. This sequence is chosen to ensure that we never end up with an object in an illegal state.

10 Constructor Operation tMyn10 An object can only be used after it has been declared – this means that any given object can only contain pointers (or references) which point (or refer) to objects that have already been created. By destroying the given object before any objects that it might point (or refer) to, we ensure that the destructor execution cannot result in any invalid pointers or references. For a particular derived class object, the sequence of destructor calls is the reverse of the constructor call sequence for the object – the derived class destructor is called first, then the base class destructor.

11 Constructor Operation tMyn11 For an object with several levels of base class, this sequence of destructor calls runs through the hierarchy of classes, starting with the derived class destructor and ending with the destructor for the most general base class.

12 Constructor Operation tMyn12 A small change to the previous example: let’s add one data member in the derived class which is allocated dynamically – that means that the derived class destructor is really needed:

13 Constructor Operation tMyn13 … class Carton:public Box { public: Carton(string="Cardboard"); ~Carton(); void derivedDimensions(); private: string* text; }; Carton::Carton(string usage) { cout<<"Derived class constructor, object "<<this<<endl; text=new string; *text=usage; }

14 Constructor Operation tMyn14 Carton::~Carton() { cout<<"Derived class destructor, object "<<this<<endl; delete text; text=0; } void Carton::derivedDimensions() { cout<<"length="<<length<<endl <<"breadth="<<breadth<<endl <<"height="<<height<<endl <<*text<<endl; }

15 Constructor Operation tMyn15 int main(array ^args) { Box first=Box(); first.baseDimensions(); Carton second=Carton(); second.derivedDimensions(); Carton* third=new Carton("Exclusive"); third->derivedDimensions(); delete third; return 0; }

16 Constructor Operation tMyn16

17 Constructor Operation tMyn17 Next step: Let’s add a base class constructor with arguments:

18 Constructor Operation tMyn18 #include "stdafx.h" #include using namespace System; using namespace std; class Box { public: Box(); Box(double, double, double); ~Box(); void baseDimensions(); protected: double length; double breadth; double height; };

19 Constructor Operation tMyn19 Box::Box() { cout<<"Base class default constr., object ” <<this<<endl; cout<<"Input length: "; cin>>length; cin.get(); cout<<"Input breadth: "; cin>>breadth; cin.get(); cout<<"Input height: "; cin>>height; cin.get(); } Box::Box(double lValue, double bValue, double hValue) { cout<<"Base class constr. with params., object ” <<this<<endl; length=lValue; breadth=bValue; height=hValue; }

20 Constructor Operation tMyn20 class Carton:public Box { public: Carton(string="Cardboard"); ~Carton(); void derivedDimensions(); private: string* text; }; Carton::Carton(string usage):Box(1.2, 2.3, 3.4) { cout<<"Derived class constructor, object "<<this<<endl; text=new string; *text=usage; } This is not very satisfactory implementation!

21 Constructor Operation tMyn21 void Carton::derivedDimensions() { cout<<"length="<<length<<endl <<"breadth="<<breadth<<endl <<"height="<<height<<endl <<*text<<endl; } int main(array ^args) { Box first=Box(4.5, 5.6, 6.7); first.baseDimensions(); Carton second=Carton(); second.derivedDimensions(); Carton* third=new Carton("Exclusive"); third->derivedDimensions(); delete third; return 0; }

22 Constructor Operation tMyn22

23 Constructor Operation tMyn23 In the example above the data members inherited from the base class will always be initialized with the same values (unfortunately to some extent…).

24 Constructor Operation tMyn24 The next step: Let’s give the ability to be able to initialize independently the data members inherited from the base class:

25 Constructor Operation tMyn25 #include "stdafx.h" #include using namespace System; using namespace std; class Box { public: Box(); Box(double, double, double); ~Box(); void baseInfo(); protected: double length; double breadth; double height; };

26 Constructor Operation tMyn26 Box::Box() { cout<<"Base class default constr., object "<<this<<endl; cout<<"Input length: "; cin>>length; cin.get(); cout<<"Input breadth: "; cin>>breadth; cin.get(); cout<<"Input height: "; cin>>height; cin.get(); } Box::Box(double lValue, double bValue, double hValue) { cout<<"Base class constr. with params., object ” <<this<<endl; length=lValue; breadth=bValue; height=hValue; }

27 Constructor Operation tMyn27 Box::~Box() { cout<<"Base class destructor, object "<<this<<endl; } void Box::baseInfo() { cout<<"Basic information:"<<endl <<"length="<<length<<endl <<"breadth="<<breadth<<endl <<"height="<<height<<endl; }

28 Constructor Operation tMyn28 class Carton:public Box { public: Carton(); Carton(string); Carton(double, double, double, string); ~Carton(); void derivedInfo(); private: string* text; };

29 Constructor Operation tMyn29 Carton::Carton():Box() { cout<<"Derived class default constructor, object " <<this<<endl; text=new string; cout<<"Target usage is: "; getline(cin, *text); } Carton::Carton(string usage):Box() { cout<<"Derived class constructor with one param., object " <<this<<endl; text=new string; *text=usage; }

30 Constructor Operation tMyn30 Carton::Carton(double lVal, double bVal, double hVal, string uVal): Box(lVal, bVal, hVal) { cout<<"Derived class constructor with four params., object " <<this<<endl; text=new string; *text=uVal; } The explicit call of the constructor for the Box class appears after a colon in the header of the derived class constructor. The call appears only in the constructor definition, and not in the prototype. The parameters of the Carton constructor are passed to the base class constructor as arguments.

31 Constructor Operation tMyn31 Carton::~Carton() { cout<<"Derived class destructor, object " <<this<<endl; delete text; text=0; } void Carton::derivedInfo() { cout<<"Basic information:"<<endl <<"length="<<length<<endl <<"breadth="<<breadth<<endl <<"height="<<height<<endl <<*text<<endl; }

32 Constructor Operation tMyn32 int main(array ^args) { Box first=Box(); first.baseInfo(); Box second=Box(4.5, 5.6, 6.7); second.baseInfo(); Box* third=new Box(7.8, 8.9, 9.1); third->baseInfo(); delete third; return 0; }

33 Constructor Operation tMyn33

34 Constructor Operation tMyn34 Now to the derived class objects:

35 Constructor Operation tMyn35 int main(array ^args) { Carton second=Carton(); second.derivedInfo(); Carton* third=new Carton("Exclusive"); third->derivedInfo(); Carton fourth(7.4, 8.5, 9.6, "To daily usage"); fourth.derivedInfo(); delete third; return 0; }

36 Constructor Operation tMyn36

37 Constructor Operation tMyn37 So when a derived class object is created, the base class constructor must be called before the derived class constructor. This is a general rule. When several levels of inheritance are involved, the class constructor of the most general base class is called first; subsequent calls are made in order of derivation, until finally, the derived class constructor is called.


Download ppt "Constructor Operation tMyn1 Constructor Operation in a Derived Class So far the default base class constructor has been called automatically. We can arrange."

Similar presentations


Ads by Google