160 likes | 291 Views
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();
E N D
CS225: Data Structures and Software PrinciplesSection 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(); int getY(); protected: void setX(); void setY(); private: int x, y; } class LabeledCoord: public class Coord { public: … protected: … private: String label }
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; }
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; };
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; };
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
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; }
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 }
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 }
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? }
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? }
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.
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.
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: .. };
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