|
Anything (assignment, function call, etc) that you can do with a variable of the parent class, you can do it with a variable of the child class !!! |
class parent { public: int i1; void f1() { cout << "parent method" << endl; } }; class child : public parent { public: int i2; void f2() { cout << "child method" << endl; } }; |
parent *p; child y, *q; p = &y; // p is a (parent *) variable // But p points to a (child *) |
you can no longer access variables and functions in the child class
class parent { public: int i1; void f1() { cout << "parent method" << endl; } }; class child : public parent { public: int i2; void f2() { cout << "child method" << endl; } }; |
parent *p; child y, *q; p = &y; // p is a (parent *) variable // But p points to a (child *) |
you can convert (cast) the parent class reference to a child class reference using:
parent *p; child y, *q; p = &y; // p is a (parent *) variable // But p points to a (child *) q = (child *) p; // Forced conversion... |
class parent { public: int i1; void f1() { cout << "parent method" << endl; } }; class child : public parent { public: int i2; void f2() { cout << "child method" << endl; } }; |
class parent { public: int i1; void f1() { cout << "parent method" << endl; } }; class child : public parent { public: int i2; void f2() { cout << "child method" << endl; } }; |
parent x, *p; child *q; q = &x; q = p; // Not allowed q = (child *) &x; q = (child *) p; // Need explicit cast operation |