1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Inheritance

Discussion in 'C++' started by freakyboard, Jan 1, 2011.

  1. freakyboard

    freakyboard New Member

    Code:
    class x{int a;};
    class y:public x{};
    main(){y k;  k.a=3; }
    
    How can I access "k.a"?
    PS: You can't change or omit any part of the code.
    a have to be private.
    Member access specifier have to be public ( class y::public x ).
    You can add functions to x class but you can't add data members to x class.
    You can add functions to y class too.
    -> Maybe we need to type a friend function.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Add a public get and set function to x, which will be inherited by y:
    Code:
    class x
    {
    private:
      int a;
    
    public:
      int get_a() { return a; }
      void set_a(int new_a) { a=new_a; }
    };
    
     
    shabbir likes this.

Share This Page