page 134: #include using namespace std ; class Base { protected: int i ; public: void push() { cout << "base" << endl ;} } ; class Derv: public Base { private: int j ; public: void push() {cout << "derived" << endl ;} } ; int main() { Base * ptb ; Base b1 ; ptb = &b1 ; ptb->push() ; Derv d1 ; ptb = &d1 ; ptb->push() ; return 0 ; } page 134: Base * ptb ; Base b1 ; ptb = &b1 ; ptb->push() ; Derv * ptd ; Derv d1 ; ptd = &d1 ; ptd->push() ; page 135: #include using namespace std ; class Base { protected: int i ; public: virtual void push() { cout << "base" << endl ;} } ; class Derv: public Base { private: int j ; public: void push() {cout << "derived" << endl ;} } ; int main() { Base * ptb ; Base b1 ; ptb = &b1 ; ptb->push() ; Derv d1 ; ptb = &d1 ; ptb->push() ; return 0 ; } page 136: #include using namespace std ; class Base { protected: int i ; public: virtual void push()= 0 ; //{ cout << "base" << endl ;} } ; class Derv: public Base { private: int j ; public: void push() {cout << "derived" << endl ;} } ; int main() { Base * ptb ; //Base b1 ; //ptb = &b1 ; //ptb->push() ; Derv d1 ; ptb = &d1 ; ptb->push() ; return 0 ; } page 137: #include using namespace std ; /////////////////////////////////////////// class Parent { protected: int i ; public: Parent() : i(1) { ;} }; /////////////////////////////////////////// class Child1: public Parent { } ; /////////////////////////////////////////// class Child2: public Parent { } ; /////////////////////////////////////////// class Grand: public Child1, public Child2 { public: void show() {cout << i << endl ;} }; /////////////////////////////////////////// int main() { Grand g1 ; g1.show() ; return 0 ; } /////////////////////////////////////////// page 138: #include using namespace std ; /////////////////////////////////////////// class Parent { protected: int i ; public: Parent() : i(1) { ;} }; /////////////////////////////////////////// class Child1: virtual public Parent { } ; /////////////////////////////////////////// class Child2: virtual public Parent { } ; /////////////////////////////////////////// class Grand: public Child1, public Child2 { public: void show() {cout << i << endl ;} }; /////////////////// int main() { Grand g1 ; g1.show() ; return 0 ; } /////////////////// page 139: #include using namespace std ; ///////////////////////// class part { private: int i ; friend int square(int x) { x= x * x ; return x ; } } ; ///////////////////////// int main() { int y ; y = square(3) ; cout << y << endl ; return 0 ; } ///////////////////////// #include using namespace std ; ///////////////////////// class part { private: int i ; public: part():i(3) { ;} friend part square(part p) { p.i= p.i * p.i ; return p ; } } ; ///////////////////////// int main() { part p1 ; p1 = square(p1) ; return 0 ; } ///////////////////////// page 140: #include using namespace std ; class PartB ; ///////////////////////////////////// class PartA { private: int a ; public: PartA() : a(3) { ;} friend int frAandB(PartA, PartB) ; } ; ///////////////////////////////////// class PartB { private: int b ; public: PartB() : b(4) { ;} friend int frAandB(PartA, PartB) ; } ; ///////////////////////////////////// int frAandB(PartA pA, PartB pB) { return(pA.a + pB.b) ; } ///////////////////////////////////// int main() { PartA pA1 ; PartB pB1 ; cout << frAandB(pA1, pB1) << endl; return 0 ; } ///////////////////////////////////// page 141: #include using namespace std ; class part { private: int i ; public: part() :i() { ;} part(int j) :i(j) { ;} part operator + (part p) { return part(i + p.i) ; } }; ///////////////////////// int main() { part p1(1), p3 ; p3 = p1 + 2 ; return 0 ; } ///////////////////////// page 142: #include //using namespace std ; class part { private: int i ; public: part() :i() { ;} part(int j) :i(j) { ;} friend part operator + (int k, part p) { return part(k + p.i) ; } }; ///////////////////////// int main() { part p1(1),p3 ; p3 = 2 + p1 ; return 0 ; } ///////////////////////// page 142: #include //using namespace std ; class part { private: int i ; public: part() :i() { ;} part(int j) :i(j) { ;} part operator + (part p); friend part operator + (int k, part p); }; ///////////////////////// int main() { part p1(1),p3 ; p3 = p1 + 2 ; p3 = 2 + p1 ; return 0 ; } ///////////////////////// part part::operator + (part p) { return part(i + p.i) ; } ///////////////////////// part operator + (int k, part p) { return part(k + p.i) ; } ///////////////////////// page 143: #include using namespace std ; class beta ; ///////////////////////// class alpha { private: int data1 ; public: alpha() :data1(2) { ;} friend class beta ; }; ///////////////////////// class beta { public: void func1(alpha a) {cout << a.data1 << endl;} }; ///////////////////////// int main() { alpha a1 ; beta b1 ; b1.func1(a1) ; return 0 ; } ///////////////////////// page 144: class Part { private: static int i ; public: Part() { i++ ;} }; ///////////////////////// int Part::i=0 ; ///////////////////////// int main() { Part p1 ; Part p2 ; Part p3 ; return 0 ; } ///////////////////////// page 144: #include using namespace std ; class Part { private: static int i ; public: Part() { i++ ;} static void show() { cout << i << endl ; } }; ///////////////////////// int Part::i=0 ; ///////////////////////// int main() { Part p1 ; Part p2 ; Part p3 ; Part::show() ; return 0 ; } ///////////////////////// page 145: class Part { private: int i ; public: Part() :i() { } Part(int j) :i(j) { } } ; /////////////////////////////// int main() { Part p1(4) ; Part p2 ; p2 = p1 ; return 0 ; } /////////////////////////////// page 145: class Part { private: int i ; public: Part() :i() { } Part(int j) :i(j) { } }; /////////////////////////////// int main() { Part p1(4) ; Part p2 = p1 ; return 0 ; } /////////////////////////////// page 146: #include using namespace std ; class Part { private: int i ; public: Part() :i() { } Part(int j) :i(j) { } Part operator = (Part p) { i = p.i ; cout << "extra ops" << endl ; return Part(i) ; } } ; ///////////////////////////////////// int main() { Part p1(4) ; Part p2 ; p2 = p1 ; return 0 ; } ///////////////////////////////////// page 146: #include using namespace std ; class Part { private: int i ; public: Part() :i() { } Part(int j) :i(j) { } Part (Part& p) { i = p.i ; cout << "extra ops" << endl ; } } ; ///////////////////////////////////// int main() { Part p1(4) ; Part p2 = p1 ; return 0 ; } ///////////////////////////////////// page 147: #include using namespace std ; class part { private: int i ; public: part():i(2) { ;} void access(void) { cout << this << endl ; cout << this->i << endl ; } } ; ///////////////////////// int main() { part p1 ; p1.access() ; return 0 ; } ///////////////////////// page 148: class Part { private: int i ; public: Part() :i() { } Part(int j) :i(j) { } Part add() { ++i ; return *this ; } } ; ///////////////////////// int main() { Part p1(4) ; Part p2 ; p2 = p1.add() ; return 0 ; } /////////////////////////