Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Bar {
- private:
- int y;
- public:
- Bar(int why);
- };
- Bar::Bar(int why) : y(why) {}
- class Foo {
- private:
- int prix;
- protected:
- int prox;
- Bar b;
- public:
- int pubx;
- Foo(int x);
- virtual void print_base();
- };
- Foo::Foo(int x) : b(x+3) // Have to use initializer list here.
- {
- this->prix = x;
- this->prox = x + 1;
- this->pubx = x + 2;
- //this->b(x+3); // doesn't work
- }
- void Foo::print_base() {
- cout << "Base: private : " << this->prix << " protected : " << this->prox << " public : " << this->pubx << endl;
- }
- class DerFoo : public Foo {
- private:
- int dprix;
- protected:
- int dprox;
- Bar db;
- public:
- int dpubx;
- DerFoo(int x);
- virtual void print_base();
- void print_derived();
- };
- DerFoo::DerFoo(int x) : Foo(x), dprix(x),
- dprox(x+1), dpubx(x+2),
- db(x+3) // (Bar)(int) being called, works fine
- // b(x-3) // doesn't work class DerFoo does not have any field named 'b'
- {
- // this->dprix = x;
- // this->dprox = x + 1;
- // this->dpubx = x + 2;
- this->prox = x - 1;
- this->pubx = x - 2;
- //this->b(x - 3); // Doesn't work, error no match for call to (Bar)(int)
- //this->db(x + 3); // Doesn't work, error no match for call to (Bar)(int)
- }
- void DerFoo::print_base() {
- cout << "Derived: protected : " << this->prox << " public : " << this->pubx << endl;
- }
- void DerFoo::print_derived() {
- cout << "Derived: private : " << this->dprix << " protected : " << this->dprox << " public : " << this->dpubx << endl;
- }
- int main() {
- Foo bar(10);
- DerFoo dbar(20);
- bar.print_base();
- dbar.print_base();
- dbar.print_derived();
- }
Add Comment
Please, Sign In to add comment