Presentation is loading. Please wait.

Presentation is loading. Please wait.

Access Control Under Inheritance tMyn1 Access Control Under Inheritance The private data members of a base class are also members of the derived class,

Similar presentations


Presentation on theme: "Access Control Under Inheritance tMyn1 Access Control Under Inheritance The private data members of a base class are also members of the derived class,"— Presentation transcript:

1 Access Control Under Inheritance tMyn1 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:

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

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

4 Access Control Under Inheritance tMyn4 … 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'

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

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

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

8 Access Control Under Inheritance tMyn8 … int main(array ^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.

9 Access Control Under Inheritance tMyn9

10 Access Control Under Inheritance tMyn10 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.

11 Access Control Under Inheritance tMyn11 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.

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

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

14 Access Control Under Inheritance tMyn14 int main(array ^args) { Box first=Box(); Carton second=Carton(); cout<<"Volume is "<<second.volume()<<endl; return 0; }

15 Access Control Under Inheritance tMyn15

16 Access Control Under Inheritance tMyn16 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.

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


Download ppt "Access Control Under Inheritance tMyn1 Access Control Under Inheritance The private data members of a base class are also members of the derived class,"

Similar presentations


Ads by Google