Results 1 to 12 of 12

Thread: Inherit private members of base class

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Answers
    3

    Inherit private members of base class

    Can some one here explain why private members of base class can't be inherited in derived class if derived publicly, Give some good explaination, I may know the answer, but i might be wrong, i just want to clear some doubt, asked in C++ online testing here.


  2. #2
    Junior Member
    Join Date
    Sep 2007
    Answers
    3

    Re: Inherit private members of base class

    private members exists only in the particular class.....so the private member cant be inherited


  3. #3
    Junior Member
    Join Date
    Aug 2007
    Answers
    3

    Re: Inherit private members of base class

    please.....no wrong answer...


  4. #4
    Junior Member
    Join Date
    Aug 2007
    Answers
    1

    Re: Inherit private members of base class

    private memebers cannot b accessed becoz of security.
    it is the basic functionality of OOPs tht, the memebers of a class can b accessed by its object


  5. #5
    Junior Member
    Join Date
    Dec 2007
    Answers
    3

    Re: Inherit private members of base class

    private member cannot access by another class besc the life time of variable is ended in that same class .so that we cannot access the variable in outside of class but in innerclasses especially


  6. #6
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: Inherit private members of base class

    C++ inheritance is very similar to a parent-child relationship. When a class is inherited all the functions and data member are inherited, although not all of them will be accessible by the member functions of the derived class. But there are some exceptions to it too.

    Some of the exceptions to be noted in C++ inheritance are as follows.

    The constructor and destructor of a base class are not inherited
    the assignment operator is not inherited
    the friend functions and friend classes of the base class are also not inherited.
    There are some points to be remembered about C++ inheritance. The protected and public variables or members of the base class are all accessible in the derived class. But a private member variable not accessible by a derived class.

    It is a well known fact that the private and protected members are not accessible outside the class. But a derived class is given access to protected members of the base class.


  7. #7
    Junior Member
    Join Date
    Nov 2007
    Answers
    5

    Re: Inherit private members of base class

    Private member of base class remain private to derived class even though it is publicly derived


  8. #8
    Junior Member
    Join Date
    May 2008
    Answers
    4

    Re: Inherit private members of base class

    this is d basic property of a class that its private members are not accesible outside its scope..........therefore they are not inheritted
    so to inherit them we use protected instead of private
    protected follows same rule + it can be inheritted to next immediate class


  9. #9
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Re: Inherit private members of base class

    the private members inherited in Implicitly way to save them private but we cannt access them Directly , we can acccess them by the function


  10. #10
    Junior Member
    Join Date
    Sep 2008
    Answers
    1

    Re: Inherit private members of base class

    I do not explain exactly that how can we access the private members of the class out from this class. But the concept of pointer can access it out of the class, because think !
    when the variable members are created the they are stored at some location then when we will be able to know that location then why we can not access the value at that location? because compilar never considers the pointers that where it is pointing out and accessing. Please try to do it and then if you get success then tell me also about it. Becoz i never tried it. Thanks
    sanyog tripathi

    Last edited by sanyogtripathi; 09-17-2008 at 03:05 AM.

  11. #11

    Re: Inherit private members of base class

    once a member of a class is declared as private, its visibility is restricted to a class.Only member functions of the same class can access those private members.


  12. #12
    Junior Member
    Join Date
    Aug 2007
    Answers
    3

    Re: Inherit private members of base class

    while inheriting private member in derived class,these private member become part of derived class object(note it down,but very much different from derived class own private member),but we can't direct access it thru derived class object pointer and ref and we can't write a public function in derived class to access these members(like we normally do if we have to access it thru public member).they can be accessed only by the public member function of base class in derived class object and derived class scope.

    here is the sample program for this.
    Just run it thru gdb and print the derived class object.
    #include <iostream>
    using namespace std;

    class Base
    {
    public:
    Base(int arg)ublic_data(arg),private_data(arg)
    {
    }
    ~Base(){}
    void set_data(int arg)
    {
    private_data = arg;
    }

    int get_data(void)const
    {
    return private_data;
    }

    int public_data;
    private:
    int private_data;
    };

    class Derived : public Base
    {
    public:
    Derived(int arg):Base(arg)
    {
    derived_data = arg;
    }
    ~Derived()
    {
    }
    int acces_base_private()
    {
    return Base::get_data();
    }
    private:
    int derived_data;
    };

    int main()
    {
    /* object creation */
    class Derived *d = new Derived(5);
    /* set the data member */
    d->set_data(10);
    /* get the data member */
    int x = d->get_data();
    cout <<"x = " << x << endl;
    /* direct access the public data member*/
    cout <<"public data : " << d->public_data << endl;
    /* direct access the private data member */
    //cout <<"private_data : " << d->private_data << endl;
    /* access base private thru derived public interface */
    cout <<" base private: " << d->acces_base_private() << endl;

    return 0;
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact