n This will hide the member function outputinfo which was declared in class

N this will hide the member function outputinfo which

This preview shows page 27 - 35 out of 54 pages.

n This will hide the member function output_info, which was declared in class Person. n If we write e.output_info(), where e is an object of type Employee, the output_info that was declared in class Employee will be called.
Image of page 27
28 Name Conflicts o The definition of output_info for class Employee looks like this: n The second line is interesting. It shows how we can access a hidden member using the scope resolution operator ( :: ). n To access a hidden member with the name, say m that has been declared in a base class B, we write B::m. n Here we write Person::output_info() , which means that the output_info declared in base class Person will be called. void Employee::output_info() const { Person::output_info(); cout << "Salary: " << salary << endl; }
Image of page 28
29 class D : public B { private : float x; float y; public : D() : x(10.0), y(20.0) { cout << "Derived class constructor" << endl; } void f() { cout << "Derived class: " << x << ", " << y << endl; B::f(); } }; class B { private : int x; int y; public : B() : x(1), y(2) { cout << "Base class constructor" << endl; } void f() { cout << "Base class: " << x << ", " << y << endl; } }; void smart(B* z) { cout << "Inside smart(): " << endl; z->f(); } int main() { B base; B* b = &base; D derived; D* d = &derived; base.f(); derived.f(); b = &derived; b->f(); smart(b); smart(d); return 0; }
Image of page 29
30 Inheritance – Member Access Control o So far, we have been seeing how, in a class definition, we can use the access specifiers: public and private to indicate whether the members of a class will be accessible outside the class. o There is a third possibility, a sort of compromise between public and private, the reserved word protected.
Image of page 30
31 Member Access Control o A class definition can have the form n If no access specifier is written, by default all the members will be private . class C { public: // declaration of visible members protected: // declaration of protected members private: // declaration of private members };
Image of page 31
32 Member Access Control o If a member is declared in a class C and is private (declared in the private section), it can only be used by n member functions in C, n the friends of class C. o If a member is declared in a class C and the member is protected (declared in the protected section), it can only be used by n member functions in C, n friends of C and member functions and friends of classes derived from C. o If a member is public (declared in public section), it can be used everywhere, without restriction.
Image of page 32
33 Example – Protected Data Members o A protected member is accessible within the inheritance hierarchy but not outside it. o We will use class Clock to give an example: class Clock { protected: int h, m, s; public: Clock(); Clock(int hour, int min, int sec); void set(int hour, int min, int sec); int get_ hour() const { return h; } int get_ min() const { return m; } int get_sec() const { return s; } void write(bool write_sec = true) const; void tick(); };
Image of page 33
34 Example – Protected Data Members o An alarm clock has the same properties as an ordinary clock but we have added the possibility of having an alarm at a specific time.
Image of page 34
Image of page 35

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture