1 / 16

CS225: Data Structures and Software Principles Section 4

CS225: Data Structures and Software Principles Section 4. C++ Inheritance. Outline – C++ Inheritance. Access Permissions Constructors and Initializer Lists Dynamic Binding and Virtual Functions Destructors Abstract Classes. Base and Derived Classes. class Coord { public: int getX();

abia
Download Presentation

CS225: Data Structures and Software Principles Section 4

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. CS225: Data Structures and Software PrinciplesSection 4 C++ Inheritance

  2. Outline – C++ Inheritance • Access Permissions • Constructors and Initializer Lists • Dynamic Binding and Virtual Functions • Destructors • Abstract Classes

  3. Base and Derived Classes class Coord { public: int getX(); int getY(); protected: void setX(); void setY(); private: int x, y; } class LabeledCoord: public class Coord { public: … protected: … private: String label }

  4. C++ Inheritance Syntax • Public versus private inheritance • The difference between “is a” – relationship and “is implemented as” class LabeledCoord: public Coord { … } LabeledCoord::labelPrint() { coordPrint(); cout<<label<<endl; } int main() { LabeledCoord l; l.coordPrint(); } class LabeledCoord : private Coord { … } LabeledCoord::LabeledCoord() { coordPrint(); cout<<label<<endl; }

  5. Simple Public InheritanceIs-a Relationship LabeledCoord.h class LabeledCoord : public Coord { public: LabeledCoord(); LabeledCoord(int xInit, int yInit, String theLabel); String const & getLabel() const; void print() const; private: String label; }; Coord.h class Coord { public: Coord(); Coord(int xInit, int yInit); int getX() const; int getY() const; void print() const; private: int xCoord; int yCoord; };

  6. Simple Public Inheritance LabeledCoord.h class LabeledCoord : public Coord { public: LabeledCoord(); LabeledCoord(int xInit, int yInit, String theLabel); String const & getLabel() const; void print() const; private: String label; }; Coord.h class Coord { public: Coord(); Coord(int xInit, int yInit); int getX() const; int getY() const; void print() const; protected: int xCoord; int yCoord; };

  7. C++ Access Permissions • Three types of access permissions: • Public accessible to any class • Private invisible to derived class • Protected only derived classes can use/access

  8. More Syntax Issues • Initializer list syntax LabeledCoord::LabeledCoord() : Coord(), label() { … } LabeledCoord::LabeledCoord(int xInit, int yInit, String theLabel) : Coord(xInit, yInit), label(theLabel) { …. } • Calls to base methods void LabeledCoord::print() const { Coord::print(); std::cout << "Label is: " << label << std::endl; }

  9. Virtual Functions and Dynamic Binding LabeledCoord.h class LabeledCoord : public Coord { public: void print() const { Coord::print(); cout << label << endl;} … }; Coord.h class Coord { public: void print() const { cout << xCoord << endl; cout<< yCoord << endl; } … }; Main.cpp int main() { Coord * cPtr = new Coord(3,4); cPtr->print(); //3, 4 Coord * cPtr = new LabeledCoord(3,4,”Boston”); cPtr->print(); //3, 4 }

  10. Virtual Functions and Dynamic Binding * Virtual is only specified in the header file LabeledCoord.h class LabeledCoord : public Coord { public: virtual void print() const { Coord::print(); cout << label << endl;} … }; Coord.h class Coord { public: virtual void print() const { cout << xCoord << endl; cout<< yCoord << endl; } … }; Main.cpp int main() { Coord * cPtr = new Coord(3,4); cPtr->print(); //3,4 Coord * cPtr = new LabeledCoord(3,4,”Boston”); cPtr->print(); //3,4,Boston }

  11. Virtual Destructors LabeledCoord.h class LabeledCoord : public Coord { public: ~LabeledCoord() { delete label; } private: String * label; … }; Coord.h class Coord { public: ~Coord() {delete xCoordPtr; delete yCoordPtr;} private: int* xCoordPtr; int* yCoordPtr; }; Main.cpp int main() { Coord* cPtr = new LabeledCoord(); //… delete cPtr; //what happens? }

  12. Virtual Destructors LabeledCoord.h class LabeledCoord : public Coord { public: virtual ~LabeledCoord() { delete label; } private: String * label; … }; Coord.h class Coord { public: virtual ~Coord() {delete xCoordPtr; delete yCoordPtr;} private: int* xCoordPtr; int* yCoordPtr; }; Main.cpp int main() { Coord* cPtr = new LabeledCoord(); //… delete cPtr; //what happens? }

  13. Base and Derived Class Destructors • Never need to make explicit calls to base destructors • Rule of thumb : class destructor should be virtual if the class has any virtual functions defined.

  14. Virtual Functions and VTables • Non-virtual functions are resolved at compile time, virtual functions are resolved at run-time • For each class that has a virtual function the compiler creates a pointer to a v-table. • Vtable is a look-up table for the functions of the class.

  15. Pure Virtual Functions and Abstract classes • Coord is an abstract class (Coord cannot be instantiated) • All derived classes from Coord must provide a definition for print(), otherwise they become abstract also Coord.h class Coord { public: … // There is no definition in the //.cpp file virtual void print() const = 0; private: .. };

  16. Sample Code • _syntax directory : simple class inheritance • _threeGood directory: example of inheritance when base and derived have dynamic memory allocation • _dynamicBinding directory: examples of virtual functions and their use • _pure directory: abstract class

More Related