access control under inheritance tmyn1 access control under inheritance the private data members of...
TRANSCRIPT
Access Control Under Inheritance
tMyn 1
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
tMyn 2
…class Box{public: Box(); ~Box();private: double length; double breadth; double height;};…
Access Control Under Inheritance
tMyn 3
…class Carton:public Box{public: Carton(); ~Carton(); double volume();private: double weight;};…
Access Control Under Inheritance
tMyn 4
…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
tMyn 5
• However, it is legal to use the volume() function if it is a base class member:
Access Control Under Inheritance
tMyn 6
…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
tMyn 7
…class Carton:public Box{public: Carton(); ~Carton();private: double weight;};…
Access Control Under Inheritance
tMyn 8
…int main(array<System::String ^> ^args){ Box first=Box(); cout<<"Volume is "<<first.volume()<<endl; Carton second=Carton(); cout<<"Volume is "<<second.volume()<<endl; return 0;}
The volume() function defined in the Box class works equally well for objects of the Carton class.
Access Control Under Inheritance
tMyn 9
Access Control Under Inheritance
tMyn 10
• 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
tMyn 11
• 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
tMyn 12
class Box{public: Box(); ~Box();protected: double length; double breadth; double height;};
Access Control Under Inheritance
tMyn 13
…class Carton:public Box{public: Carton(); ~Carton(); double volume();private: double weight;};……double Carton::volume(){ return length*breadth*height;}…
Those came as beingprotected from the baseclass
Access Control Under Inheritance
tMyn 14
int main(array<System::String ^> ^args){
Box first=Box(); Carton second=Carton(); cout<<"Volume is "<<second.volume()<<endl; return 0;}
Access Control Under Inheritance
tMyn 15
Access Control Under Inheritance
tMyn 16
• Notice: you are not able to have the line
cout<<first.volume();
because the Box class now has no function volume() defined.
• However, this works well:
cout<<second.volume();• This is because the function volume() accesses the
inherited members length, breadth and height to produce the result.
• These members were declared as protected in the base class and remain protected in the derived class.
Access Control Under Inheritance
tMyn 17
• You can’t have the line
second.length=5.5;
because the protected members of the base class remain protected in the derived class.