120 likes | 158 Views
Learn about constructors, destructors, and inheritance in C++ programming language with examples and rules for building class hierarchies and over-written functions.
E N D
Inheritance in C++ -2 CSC1201: Programming Language 2
Constructors, Destructors, and Inheritance • When an object of a derived class is created, the base class’ constructor will be called first, followed by the derived class’ constructor. • When a derived object is destroyed, its destructor is called first, followed by the base class' destructor.
Constructors, Destructors, and Inheritance class base { public: base() { cout << "Constructing base\n"; } ~base() { cout << "Destructing base\n"; } }; class derived: public base { public: derived() { cout << "Constructing derived\n"; } ~derived() { cout << "Destructing derived\n"; } • }; int main() { derived ob; // do nothing but construct and destruct ob return 0; • }
Constructors, Destructors, and Inheritance • When executed, this program displays: Constructing base Constructing derived Destructing derived Destructing base
Rules for building a class hierarchy • Derived classes are special casesof base classes • A derived class can also serve as a base class for new classes. • There is no limit on the depth of inheritance allowed in C++ • It is possible for a class to be a base class for more than onederived class
Example ( public access) class boxType: publicrectangleType //derived class { private: double height; public: boxType(); boxType( double L, double w, double h); ~boxType(); voidsetDimension ( double L, double w, double h); doublegetHeight(); double area(); double volume(); void print(); }; boxType::boxType() { rectangleType(); height = 0 ; } boxType::boxType( double L, double w, double h) { setDimension( L, w, h); } #include<iostream> using namespacestd; classrectangleType // base class { protected: double length; double width; public: rectangleType(); rectangleType( double L, double w); ~rectangleType(); voidsetDimension ( double L, double w); doublegetLength(); doublegetWidth(); double area(); double perimeter(); void print(); };
Cont. Example boxType::~boxType() {} voidboxType::setDimension( double L, double w, double h) {rectangleType::setDimension( L , w ); if ( h >= 0) height = h; else height = 0; } doubleboxType::getHeight() {return height; } doubleboxType::area() {return 2 * ( length * width + length * height + width * height ); } doubleboxType::volume() {returnrectangleType::area() * height; } voidboxType::print() {rectangleType::print(); cout << " ; Height = " << height; } int main() { rectangleType myRectangle1; rectangleType myRectangle2(8, 6); boxType myBox1; boxType myBox2(10, 7, 3); cout << "\n myRectangle1: "; myRectangle1.print(); cout << endl; cout << " Area of myRectangle1: " << myRectangle1.area() << endl; cout << "\n myRectangle2: "; myRectangle2.print(); cout << endl; cout << " Area of myRectangle2: " << myRectangle2.area() << endl; }
Over-written Functions • These functions are NOT overloaded, since they have exactly the same prototype (and header), and they are not in the same class. • They are over-written functions. • The over-written function that is closest to the object defined takes precedence.
Example: using protected member • public: • // derived may access base’s i and j • void setK() { k = i * j; } • void showK(){ cout<<k << “\n”; • } • }; void main(){ derived ob; ob.set(1,2); // ok , known to derived ob.show(); // ok , known to derived ob.setK(); ob.showK(); } #include<iostream> using namespacestd; classbase // base class {protected: inti, j; // private to base, but accessible to derived public: void set(int a, int b) { i=a; j=b;} void show(){ cout<< i << “ “ << j << “\n”; } }; classdrived: public base //drivedclass { int k;
Example: using protected member // I & j inherited indirectly through derived1 class drived2: public derived1 { intm; public: void setM() { m = i - j; } // legal void showM(){ cout<< m << “\n”;} }; void main(){ derived1 ob1; derived2 ob2; ob1.set(1,2); ob.1setK(); ob.1showK(); ob2.set(4,7); ob2.show(); ob.2setK(); ob2.showK(); ob.2setM(); ob2.showM(); } #include<iostream> using namespacestd; classbase // base class {protected: inti, j; // private to base, but accessible to derived public: void set(int a, int b) { i=a; j=b;} void show(){ cout<< i << “ “ << j << “\n”; } }; classdrived1: public base // I & j inherited as protected { int k; public: void setK() { k = i * j; } void showK(){ cout<<k << “\n”;} };
Example: using protected member // access to i& j & set() & show() not inherited class drived2: public derived1 { intm; public: // illegal because I and j are private to derived1 void setM() { m = i - j; } void showM(){ cout<< m << “\n”;} }; void main(){ derived1 ob1; derived2 ob2; ob1.set(1,2); // error ob.1showK(); // error ob2.set(4,7); // error ob2.show(); // error } // this program won’t compile #include<iostream> using namespacestd; classbase // base class {protected: inti, j; // private to base, but accessible to derived public: void set(int a, int b) { i=a; j=b;} void show(){ cout<< i << “ “ << j << “\n”; } }; // Now, all elements of base are private in derived1 classdrived1: private base {intk; public: void setK() { k = i * j; } // OK void showK(){ cout<<k << “\n”;} };