about inheritance, please help

This is a discussion on about inheritance, please help within the C++ Programming forums, part of the General Programming Boards category; Code: class Base { public: std::size_t size() const { return n; } protected: std::size_t n; }; class Derived : private ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    about inheritance, please help

    Code:
         class Base {
         public:
             std::size_t size() const { return n; }
         protected:
             std::size_t n;
         };
         class Derived : private Base { . . . };
    In this hierarchy, size is public in Base but private in Derived. To make size public in Derived we can add a using declaration for it to a public section in Derived:
    Code:
         class Derived : private Base {
         public:
             using Base::size;
         protected:
             using Base::n;
         };
    Above is what my book tells me. But I found that even if "using" is omitted, size is still made public and n is still made protected. So my question is, is this "using" neccesary? If I omit the "using", what problems will arise?

    I'm on WinXp, using msvc++6.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    To make size public in Derived we can add a using declaration for it to a public section in Derived.
    How does that make size public? Can you do this?
    Code:
    Derived myD;
    cout<<myD.size<<endl;
    It doesn't work for me.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    How do you know that size is public in Derived without the using statement? That might just be a bug in your version of VC++ 6.0. I think I have SP5 and it correctly returns an error if I call size() from a Derived object.

    If you omit the using declarations, and you want size and n to be public in your derived class, then your code won't be portable to a compiler that doesn't have that bug.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Since size is inherited by the Derived class, it is a member of the Derived class, so why would you need a Base:: access specifier to access it in the first place? For instance, a getSize() method in the Derived class does not need a Base:: access specifier to get the size:
    Code:
    #include <iostream>
    using namespace std;
    
    class Base {
    public:
         size_t size() const 
    	 { 
    		 return n; 
    	 }
    	 
    	 Base(size_t num)
    	 {
    		 n = num;
    	 }
    	 
    	 Base()
    	 {
    		 n = 0;
    	 }
     
    protected:
         size_t n;
    };
    
    
    class Derived : private Base 
    { 
    public:
    	size_t getSize()
    	{
    		return n;
    	}
    	
    
    };
    
     
    
    int main()
    {
    	Base myBase(10);
    	cout<<myBase.size()<<endl;
    
    	Derived myD;
    	cout<<myD.getSize()<<endl;
    
    
    	return 0;
    }

    What is the name of your book?
    Last edited by 7stud; 08-10-2005 at 11:42 AM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    It is private inheritance, so all the member variables and methods in the Base class should become private in the Derived class.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I was assuming that Antigloss was referring to accessing size and n from outside the Derived class. Of course they should be accessible without the using declaration from within Derived class methods.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Like this?
    Code:
    cout<<myD.n<<endl;
    If I put the using declarations in Derived, I get the error:

    :\Beginning C++\test1\main.cpp(50) : error C2248: 'n' : cannot access protected member
    declared in class 'Base'
    C:\Beginning C++\test1\main.cpp(22) : see declaration of 'n'
    Last edited by 7stud; 08-10-2005 at 12:05 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Not exactly, like this:
    Code:
    #include <iostream>
    
    class Base {
    public:
        Base() : n(10) { }
        std::size_t size() const { return n; }
    protected:
        std::size_t n;
    };
    
    class Derived : private Base {
    public:
    //    using Base::size;
    protected:
    //    using Base::n;
    };
    
    int main()
    {
        Derived d;
        std::cout << d.size() << std::endl;
    }
    If you uncomment the using lines then it will build. You shouldn't be able to access n from main() because it will be private without the using declaration and protected with it where it is. You would have to put the using declaration into the public section to make the Base member public.

  9. #9
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote Originally Posted by Daved
    How do you know that size is public in Derived without the using statement? That might just be a bug in your version of VC++ 6.0. I think I have SP5 and it correctly returns an error if I call size() from a Derived object.

    If you omit the using declarations, and you want size and n to be public in your derived class, then your code won't be portable to a compiler that doesn't have that bug.
    I compiled the following code using vc++6 and g++3.2, it works and print 10.
    Code:
    #include <cstddef>
    #include <iostream>
    
    class Base {
         public:
             size_t size;
         protected:
             size_t n;
    };
    class Derived : private Base {
         public:
             Base::size; // without using
         protected:
             Base::n; // no using here
    };
    
    int main()
    {
    	Derived d;
    	d.size = 10;
    	std::cout << d.size << '\n';
    	return 0;
    }
    I did't mean to omit the using statement, just to omit "using". Just like the above code show, if I omit "using", what problems will arise?
    Last edited by Antigloss; 08-10-2005 at 06:58 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Ok, without the word "using", that is an access declaration. They have been deprecated by the C++ standard, which is why I get the following warning with that code on VC++ 7.1:
    Code:
    warning C4516: 'Base::Base::size' : access-declarations are deprecated;
    	  member using-declarations provide a better alternative
    So just leave the word "using" in there even though both methods are technically valid and equivalent.
    Last edited by Daved; 08-10-2005 at 07:09 PM.

  11. #11
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Thanks Daved. I got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM

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