Скачать презентацию Access Control Under Inheritance The private data Скачать презентацию Access Control Under Inheritance The private data

ff2075f3517e59ae4c4e3abadefaabae.ppt

  • Количество слайдов: 17

Access Control Under Inheritance • The private data members of a base class are Access Control Under Inheritance • The private data members of a base class are also members of the derived class, but they remain private to the base class within the derived class. • This means that the inherited data members are accessible to the inherited function members from the base class, but they are not accessible to the member functions declared within the derived class definition. • So this does not work: Access Control Under Inheritance t. Myn 1

… class Box { public: Box(); ~Box(); private: double length; double breadth; double height; … class Box { public: Box(); ~Box(); private: double length; double breadth; double height; }; … Access Control Under Inheritance t. Myn 2

… class Carton: public Box { public: Carton(); ~Carton(); double volume(); private: double weight; … class Carton: public Box { public: Carton(); ~Carton(); double volume(); private: double weight; }; … Access Control Under Inheritance t. Myn 3

… double Carton: : volume() { return length*breadth*height; } … 'Box: : length' : … double Carton: : volume() { return length*breadth*height; } … 'Box: : length' : cannot access private member declared in class 'Box' 'Box: : breadth' : cannot access private member declared in class 'Box' 'Box: : height' : cannot access private member declared in class 'Box' Access Control Under Inheritance t. Myn 4

 • However, it is legal to use the volume() function if it is • However, it is legal to use the volume() function if it is a base class member: Access Control Under Inheritance t. Myn 5

… class Box { public: Box(); ~Box(); double volume(); private: double length; double breadth; … class Box { public: Box(); ~Box(); double volume(); private: double length; double breadth; double height; }; … … double Box: : volume() { return length*breadth*height; } … Access Control Under Inheritance t. Myn 6

… class Carton: public Box { public: Carton(); ~Carton(); private: double weight; }; … … class Carton: public Box { public: Carton(); ~Carton(); private: double weight; }; … Access Control Under Inheritance t. Myn 7

… int main(array<System: : String ^> ^args) { Box first=Box(); cout<< … int main(array ^args) { Box first=Box(); cout<<"Volume is "<

Access Control Under Inheritance t. Myn 9 Access Control Under Inheritance t. Myn 9

 • The private members of a base class are only accessible to function • The private members of a base class are only accessible to function members of the base class, but this isn’t always convenient. • There will doubtless be many occasions when we want the members of a base class to be accessible from within the derived class, but nonetheless protected from outside interference. • In addition to the public and private access specifiers for members of a class, you can also declare members of a class as protected. • Within the class, the keyword protected has exactly the same effect as the keyword private. Access Control Under Inheritance t. Myn 10

 • Members of a class that are protected can only be accessed by • Members of a class that are protected can only be accessed by member functions of the class (. . . friend classes and friend functions of the class). • These protected class members can’t be accessed from outside the class, so they behave like private class members. • The difference between protected and private members only becomes apparent in a derived class. • Members of a base class that are declared as protected are freely accessible in function members of a derived class, whereas the private members of the base class are not. Access Control Under Inheritance t. Myn 11

class Box { public: Box(); ~Box(); protected: double length; double breadth; double height; }; class Box { public: Box(); ~Box(); protected: double length; double breadth; double height; }; Access Control Under Inheritance t. Myn 12

… class Carton: public Box { public: Carton(); ~Carton(); double volume(); private: double weight; … class Carton: public Box { public: Carton(); ~Carton(); double volume(); private: double weight; }; … … double Carton: : volume() { return length*breadth*height; } … Access Control Under Inheritance t. Myn Those came as being protected from the base class 13

int main(array<System: : String ^> ^args) { Box first=Box(); Carton second=Carton(); cout<< int main(array ^args) { Box first=Box(); Carton second=Carton(); cout<<"Volume is "<

Access Control Under Inheritance t. Myn 15 Access Control Under Inheritance t. Myn 15

 • Notice: you are not able to have the line cout<<first. volume(); because • Notice: you are not able to have the line cout<

 • You can’t have the line second. length=5. 5; because the protected members • You can’t have the line second. length=5. 5; because the protected members of the base class remain protected in the derived class. Access Control Under Inheritance t. Myn 17