CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2003
    Location
    India
    Posts
    12

    Whats the problem in the following code?

    I am new to C++ and tried the following code on LINUX using g++.

    #include <stdio.h>
    #include <iostream>
    #include <string>
    using namespace std;

    class mom
    {
    public:
    void display()
    {cout<<"MOM is here..."; }
    };

    class son: private mom
    {
    public:
    void display(){mom:isplay();}
    friend void g2(mom*);
    };

    void g2(mom* s)
    {
    s->display();
    }


    int main()
    {
    son s1;
    g2(&s1);
    cout<<"\n";
    return 0;
    }


    It gives me an error which says mom is an inaccessible base class of son.

    But according to me if the base class is declared private then its public and protected members are accessible to members and friends of the derived class and only members and friends have the premission( or can change) D* to B*.

    So dows it means i have a bugged version of g++ but the same message is printed on VC++ and Dev C++ also.

    Pls help me.

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    First, declare display() as virtual.

    Secondly. post the exact error.

    Kuphryn

  3. #3
    Join Date
    Dec 2000
    Location
    Greece, Athens
    Posts
    219
    You cannot convert a derived class to a pointer to a base class (as you try to do in son s1;g2(&s1) when you have declared the inheritance private or protected.

    try public inheritance instead
    Code:
    class son: public mom

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635
    Further, you don't need to create another Display method in Son if all it does is call Mom's Display. If you want Son to do something different, declare Mom's Display as virtual and have Son do something different. As it is, the code is redundant and therefore potentially confusing.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    GCDEF: son does need a display() function in order to make it visible. All the members of mom are private to son, so virtualising display still wouldn't work, and is unnecessary.

    Private inheritance means that you can't pass a son object to a mom* argument.

    But according to me if the base class is declared private then its public and protected members are accessible to members and friends of the derived class and only members and friends have the premission( or can change) D* to B*.
    Not really. Private inheritance means that public and protected members of the base class have their access specifier changed to private in the derived class. Friends of a derived class have the same access rights to memebrs of a base class as do the members of the derived class. There is no defined conversion between D* and B* for private (or protected) inheritance (which is why you can't call g2() with a son object - only public inheritance gives you the IS-A relationship).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured