PrivateInheritance

PrivateInheritance - SPM Private Inheritance 1 Private and...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: SPM Private Inheritance 1 Private and Protected Inheritance So far, all of our discussion has been about public inheritance. It is possible to specify private and protected inheritance, as in: class Derived : private Base { and class Derived : protected Base { Thursday, October 27, 2011 SPM Private Inheritance private and protected inheritance • This provides implementation inheritance • Can use the code and implementation in the Base class, but restrictions are placed on over riding and further inheritance. 2 SPM Private Inheritance private and protected inheritance • This provides implementation inheritance • Can use the code and implementation in the Base class, but restrictions are placed on over riding and further inheritance. 2 Thursday, October 27, 2011 SPM Private Inheritance 3 #include <string> #include <iostream> using namespace std; class Base { public: int iBasePublic; Base(int i) {iBasePublic = i;} virtual void print( ) { cout << "Base: " << iBasePublic << endl;} virtual ~Base() { }; private: int iBasePrivate; }; Thursday, October 27, 2011 SPM Private Inheritance 4 class Derived1 : private Base { public: int iDerived1Public; Derived1(int i) : Base(-i) {iDerived1Public = i;} virtual void print( ) {Base::print( ); cout << "Derived1: " << iDerived1Public << endl;} virtual ~Derived1() { }; private: int iDerived1Private; }; class Derived2 : public Derived1 { public: int iDerived2Public; Derived2(int i) : Derived1(-i) {iDerived2Public = i;} virtual void print( ) {Derived1::print( ); cout << "Derived2: " << iDerived1Public << endl;} virtual ~Derived2() { }; private: int iDerived2Private; }; Thursday, October 27, 2011 SPM Private Inheritance 5 int main( int argc, char * argv[ ]) { Base b = Base( ); Derived1 d1 = Derived1(...
View Full Document

Page1 / 22

PrivateInheritance - SPM Private Inheritance 1 Private and...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online