[ Pobierz całość w formacie PDF ]
.Line 9 of the constructor represents the call to Speak() on the firstobject, the Dog.The virtual Speak() method is called, and thecorrect version of Speak() is invoked.The Clone() function isthen called, and as this is also virtual, Dog's Clone() methodis invoked, causing the Mammal constructor and the Dog copy constructorto be called.The same is repeated for Cat on lines 12-14, and then for Mammalon lines 15 and 16.Finally, the new array is iterated, and each of the new objectshas Speak() invoked.The Cost of VirtualMethodsBecause objects with virtual methods must maintain a v-table, there is some overheadin having virtual methods.If you have a very small class from which you do not expectto derive other classes, there may be no reason to have any virtual methods at all.Once you declare any methods virtual, you've paid most of the price of the v-table(although each entry does add a small memory overhead).At that point, you'll wantthe destructor to be virtual, and the assumption will be that all other methods probablywill be virtual as well.Take a long hard look at any non-virtual methods, and becertain you understand why they are not virtual.DO use virtual methods when you expect to derive from a class.DO usea virtual destructor if any methods are virtual.DON'T mark the constructoras virtual.SummaryToday you learned how derived classes inherit from base classes.This chapterdiscussed public inheritance and virtual functions.Classes inherit all the publicand protected data and functions from their base classes.Protected access is public to derived classes and private to all other objects.Even derived classes cannot access private data or functions in their base classes.Constructors can be initialized before the body of the constructor.It is at thistime that base constructors are invoked and parameters can be passed to the baseclass.Functions in the base class can be overridden in the derived class.If the baseclass functions are virtual, and if the object is accessed by pointer or reference,the derived class's functions will be invoked, based on the run-time type of theobject pointed to.Methods in the base class can be invoked by explicitly naming the function withthe prefix of the base class name and two colons.For example, if Dog inheritsfrom Mammal, Mammal's walk() method can be called withMammal::walk().In classes with virtual methods, the destructor should almost always be made virtual.A virtual destructor ensures that the derived part of the object will be freed whendelete is called on the pointer.Constructors cannot be virtual.Virtualcopy constructors can be effectively created by making a virtual member functionthat calls the copy constructor.Q&AQ.Are inherited members and functions passed along to subsequent generations?If Dog derives from Mammal, and Mammal derives from Animal, does Dog inherit Animal'sfunctions and data?A.Yes.As derivation continues, derived classes inherit the sum of all thefunctions and data in all their base classes.Q.If, in the example above, Mammal overrides a function in Animal, which doesDog get, the original or the overridden function?A.If Dog inherits from Mammal, it gets the function inthe state Mammal has it: the overridden function.Q.Can a derived class make a public base function private?A.Yes, and it remains private for all subsequent derivation.Q.Why not make all class functions virtual?A.There is overhead with the first virtual function in the creation of av-table.After that, the overhead is trivial.Many C++ programmers feel that if onefunction is virtual, all others should be.Other programmers disagree, feeling thatthere should always be a reason for what you do.Q.If a function (SomeFunc()) is virtual in a base class and is also overloaded,so as to take either an integer or two integers, and the derived class overridesthe form taking one integer, what is called when a pointer to a derived object callsthe two-integer form?A.The overriding of the one-int form hides the entire base classfunction, and thus you will get a compile error complaining that that function requiresonly one int.WorkshopThe Workshop provides quiz questions to help you solidify your understanding ofthe material that was covered, and exercises to provide you with experience in usingwhat you've learned.Try to answer the quiz and exercise questions before checkingthe answers in Appendix D, and make sure you understand the answers before continuingto the next chapter.Quiz1.What is a v-table?2.What is a virtual destructor?3.How do you show the declaration of a virtual constructor?4.How can you create a virtual copy constructor?5
[ Pobierz całość w formacie PDF ]