Advanced Data Structures
Advanced Data Structures CSCI 3110
Popular in Course
Popular in ComputerScienence
This 6 page Class Notes was uploaded by Hester Ernser on Wednesday September 23, 2015. The Class Notes belongs to CSCI 3110 at Middle Tennessee State University taught by Judith Hankins in Fall. Since its upload, it has received 25 views. For similar materials see /class/213011/csci-3110-middle-tennessee-state-university in ComputerScienence at Middle Tennessee State University.
Reviews for Advanced Data Structures
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/23/15
CSCI 3110 Inheritance 2 An example at earl binding class Sale public Sale Saledouble thePrice double GetPriceO const void SetPricedouble neWPrice double Bill const double Savings const Sale amp other const protected double price SaleSale price 0 Sale Saledouble thePrice price thePrice double SaleBill return price double SaleGetPrice const return price void Sale SetPricedouble neWPrice 39ce neWPrice double SaleSavingsconst Sale amp other const return CBillO 7 otherBill DiscountSale Class class DiscountSale public Sale public DiscountSaleO DiscountSaledouble thePrice double theDisc ount double GetDiscountO const void GetDiscountdouble neWDiscount double Bill const private double discount DiscountSaleDiscountSaleO Sale0 discount 0 DiscountSale DiscountSaledouble thePrice double theDiscount SalethePrice discounttheDiscount double DiscountSale GetDiscountO const return discount void DiscountSale SetDiscountdouble neWDiscount discount neWDiscount double DiscountSale Bill const d uble fraction discount 100 return 1 fractiongetPrice client grogram bool operator lt const Saleamp first const Sale amp second int main Sale simplel000 DiscountSale discl l 00 10 if disc lt simple cout ltlt Discounted item is cheaper cout ltlt Saving is 39 ltlt simplesavingdiscount ltlt endl else cout ltlt Discounted item is not cheaper bool operator lt const Saleamp first const Saleamp second return firstbill lt secondbill For operator lt disc which is an object of DiscountedSale class is cast back to Sale class during compile time 7 early binding binding done at compile time As a result the bill method associated with rst is the bill method de ned for Sale class not the bill method for DiscountSale class How to make an object always use its own methods e g not being bound by methods defined in base classes Answer virtual functions If a method is de ned to be Virtual it is to tell the compiler I do not know how this function is implemented Wait until it is used in a program and then get the implementation from the object instance wait until run time to determine the implementation of a function late binding examples including overloaded function function passed as parameter Change the above program to class Sale public SaleO Saledouble thePrice double GetPriceO const void SetPricedouble newPrice virtual double Bill 0 const this is the only change necessary a if a method is defined to be virtual then all new definitions of the function in the derived class will automatically be virtual b virtual modifier is not necessary in the implementation file double Savings const Sale amp other const private double price output Discounted item is cheaper Savings is 010 include ltiostreamgt include quotmamhquot include quotdoghquot int main mammal Animal dog MyDog AnimalSpeak MyDogSpeak mammalquot mamptrampAnimal mammalquot dogptrampMyDog cout ltltquotMammal s pointer quot mamptr gtSpeak0 cout ltlt endl cout ltltquotDog s pointer quot dogptr gtSpeak0 cout ltlt endl legal since MyDog is a mammal return 0 Equot W Above examples are examples of static or early binding 7 appropriate version of a member function is determined at compilation time compiler binds a particular object to a given function MyDogSpeak will execute the Speak function found in the dog class Since dogptr is declared as a pointer to a mammal dogptr gtSpeak will execute the Speak function found in the mammal class Polymorphism a Many forms during run time an object pointer or an object reference may take on many different forms of a class method depending on which underlying object class is used b C extends polymorphism to allow pointers to base classes to be assigned to derived class objects c Polymorphism is supported using dynamic binding and virtual member functions d The reserve word virtual should be used when declaring selected methods in a base class if it is possible that these methods may or should be overridden in a derived class This noti es the compiler that these functions may be de ned differently in a derived class and that the appropriate method should be determined at execution time e Virtual function magic operates only on pointers and references ifndef MAMiH de ne MAMiH class mammal public mammal mammal int W mammal int RetumWeight int ReturnHeight Virtual void Speakconst protected int Age int Weight int Height endif include ltiostreamgt include quotmamhquot include quotdoghquot using namespace std int main mammal Animal dog MyDog AnimalSpeak MyDogSpeak mammal mamptrampAnimal mammal dogptrampMyDog cout ltltquotMammal s pointer quot mamptr SpeakO cout ltlt endl cout ltltquotDog s pointer quot dog ptr SpeakO cout ltlt endl return 0 Additional rules 0 constructors can not be virtual o destructor a can and should be Virtual guarantees that future descendants of the object can deallocate themselves correctly b If any function in a class is Virtual destructor should be Virtual as well A class destructor must be de ned when a class allocates dynamic memory 0 virtual function s return type can not be overridden 0 Virtual function table 0 CSCI 3110 Friends Function as a friend to a class Example ltlt gtgt class A public friend ostream amp operator ltltostream amp os const Aamp rhs l Class friendship Class A is a friend of class B does not automatically make class B a friend of class A Definition of class C class C public friend class B class B methods have access to all data in C void ng p private int datal l Definition of class B class B public void fC p private int data2 implementation of method ffrom class B void BfC p cout ltlt pgtdatal ltlt endl or Bfrdata l does not support cout ltlt Cdatal ltlt endl the following is not allowed C is not a friend of B void CgB p cout ltlt pgtdata2 friendship is neither inherited nor transitive class A friend class B int a l class B friend class C l C is a friend of B B is a friend of A does not make C a friend ofA class C void fA p pa error i class D public B friendship can not be inherited void fA p pa error one more example friend class include ltiostreamgt using namespace std class CSquare class CRectangle private int width height public int area return width height void convert CSquare a l class CSquare private int side public void setside int a sidea friend class CRectangle
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'