Guest User

Untitled

a guest
Jul 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Bar {
  6. private:
  7.     int y;
  8. public:
  9.     Bar(int why);
  10. };
  11.  
  12. Bar::Bar(int why) : y(why) {}
  13.  
  14. class Foo {
  15. private:
  16.     int prix;
  17. protected:
  18.     int prox;
  19.     Bar b;
  20. public:
  21.     int pubx;
  22.     Foo(int x);
  23.     virtual void print_base();
  24. };
  25.  
  26. Foo::Foo(int x) : b(x+3)  // Have to use initializer list here.
  27. {
  28.     this->prix = x;
  29.     this->prox = x + 1;
  30.     this->pubx = x + 2;
  31.     //this->b(x+3); // doesn't work
  32. }
  33.  
  34. void Foo::print_base() {
  35.     cout << "Base: private : " << this->prix << " protected : " << this->prox << " public : " << this->pubx << endl;
  36. }
  37.  
  38. class DerFoo : public Foo {
  39. private:
  40.     int dprix;
  41. protected:
  42.     int dprox;
  43.     Bar db;
  44. public:
  45.     int dpubx;
  46.     DerFoo(int x);
  47.     virtual void print_base();
  48.     void print_derived();
  49. };
  50.  
  51. DerFoo::DerFoo(int x) : Foo(x), dprix(x),
  52.     dprox(x+1), dpubx(x+2),
  53.     db(x+3) // (Bar)(int) being called, works fine
  54.     // b(x-3) // doesn't work class DerFoo does not have any field named 'b'
  55. {
  56.     // this->dprix = x;
  57.     // this->dprox = x + 1;
  58.     // this->dpubx = x + 2;
  59.     this->prox = x - 1;
  60.     this->pubx = x - 2;
  61.     //this->b(x - 3); //  Doesn't work, error no match for call to (Bar)(int)
  62.     //this->db(x + 3); // Doesn't work, error no match for call to (Bar)(int)
  63. }
  64.  
  65. void DerFoo::print_base() {
  66.     cout << "Derived: protected : " << this->prox << " public : " << this->pubx << endl;
  67. }
  68.  
  69. void DerFoo::print_derived() {
  70.     cout << "Derived: private : " << this->dprix << " protected : " << this->dprox << " public : " << this->dpubx << endl;
  71. }
  72.  
  73. int main() {
  74.     Foo bar(10);
  75.     DerFoo dbar(20);
  76.     bar.print_base();
  77.     dbar.print_base();
  78.     dbar.print_derived();
  79. }
Add Comment
Please, Sign In to add comment