1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

question on CONSTRUCTOR

Discussion in 'C++' started by ravindrareddy3, Mar 17, 2010.

  1. ravindrareddy3

    ravindrareddy3 New Member

    Joined:
    Mar 17, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    what will happen if we define a constructor in protected scope in a class?
    for eg
    class some
    {
    int x;
    protected:
    some()
    {
    x=90;
    }
    };
     
  2. itstimetojazz

    itstimetojazz New Member

    Joined:
    Sep 9, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    The constructor is never inherited by the derived class, so it doesnt matters wherever u write the constructor.
     
  3. ravindrareddy3

    ravindrareddy3 New Member

    Joined:
    Mar 17, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0

    i am not inheriting it i am using only it only in single class and i wil creat an object to it,then wat is the output?
     
  4. itstimetojazz

    itstimetojazz New Member

    Joined:
    Sep 9, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    well in this case, a compiler error will be thrown. Because u r trying to create a class object which is global to the code whereas the constructor is protected in the class.
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    If you're not deriving then don't use protected, because it only makes sense in the context of a derived class. Use private or public instead then your intent is clear. By using protected in a class that is not to be derived you are intentionally obfuscating the meaning of your code.

    Public is visible to all, private is visible only to the class, and protected is visible to the class and its derivatives. So this is the same as a private constructor, which means only the class itself and its friends can create an object of that class.

    Code:
    class some
    {
    	int x;
    protected: some() { x=90; }
    };
    
    void test16()
    {
    	some wibble;
    }
    
    There will be no output because this will not compile:
    error C2248: 'some::some' : cannot access protected member declared in class 'some'
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    This can be fixed by making test16 a friend:
    Code:
    class some
    {
    	int x;
    protected:
    	some() { x=90; }
    
    	friend void test16();
    };
    
    void test16()
    {
    	some wibble;
    	printf("%d\n",wibble.x);
    }
    
     
  7. itstimetojazz

    itstimetojazz New Member

    Joined:
    Sep 9, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    other method using inheritence
    Code:
    Class A
    {
    protected:
    int a;
    };
    
    class B:public A
    {
    public:
    int c;
    };
    
    int main()
    {
     B b;
    return 0;
    }
    Using this method you can use the protected member of base class using the object of derived class
     
    Last edited by a moderator: Mar 18, 2010

Share This Page