About inheritance and access specifier

This is a discussion on About inheritance and access specifier within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, I wanted to know about access specifier,if we have a class of "Teacher" as given below and its ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    About inheritance and access specifier

    Hi Guys,


    I wanted to know about access specifier,if we have a class of "Teacher" as given below and its data members are protected, will
    the child class "Student" be able to access the protected data members of the base class "Teacher" even if we make it private inheritance as:

    class Student : private Teacher


    ----------------------------------------------------------------------------
    Code:
     
    class Teacher
        {
        protected:
        int age;
    
        public:
     
        bool SetAge(int_age)
        {
         if(_age>=0 && _age<=125)
         {
          age =_age;
          return true;
         }
        return false;
        };
        class Student: private Teacher
         {
          public:
          boolSetAge(int_age)
          {
           if(_age>=18 && _age<=25)
           {
            age = _age;
           return true;
           }
           return false;
           }
          };
    -----------------------------------------------------------------------------

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    At first function SetAge one bracket is missing under return false;
    Since int age; is protected you can not just write

    Code:
     Student s;
    s.age=5;
    However you you could use SetAge of class Student,and as a result has access to the protected variable.
    Code:
     Student s;
    s.SetAge(5);

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Have you acquired a programming book yet?
    You might find some at your local library.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Until you find a book check out this short example i have in mind(finding a book though is strongly recommended )
    Code:
    class B : private A {...};
    Let PRA stand for private part of class A,PROTA for protected part of class A and PUBA for the public part of class A.
    Then the following will be true
    Code:
    PUBB = PUBB
    PROTB = PROTB 
    PRB = PRB + PROTA + PUBA

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About the Extern specifier...
    By Jony in forum C Programming
    Replies: 2
    Last Post: 08-26-2011, 11:42 AM
  2. access specifier of class
    By manish411 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2010, 02:42 AM
  3. Conversion Specifier
    By Nextstopearth in forum C Programming
    Replies: 4
    Last Post: 09-12-2008, 08:31 PM
  4. got confused which specifier to use ?!
    By Masterx in forum C Programming
    Replies: 12
    Last Post: 12-22-2007, 07:27 PM
  5. member access and inheritance
    By Laserve in forum C++ Programming
    Replies: 3
    Last Post: 08-27-2004, 04:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21