{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

PrivateInheritance

PrivateInheritance - Private and Protected Inheritance So...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
SPM Private Inheritance 5 int main( int argc, char * argv[ ]) { Base b = Base( 0 ); Derived1 d1 = Derived1( 1 ); Derived2 d2 = Derived2( 2 ); b.print( ); d1.print( ); d2.print( ); } I'm not doing anything particularly useful with the inheritance, and this all works.
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern