Thread: Inheritance

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    10

    Post Inheritance

    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.
    Last edited by freakyboard; 01-01-2011 at 05:57 AM. Reason: wrong typing

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need a setter (ie make a function SetA). And make main return int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Elysia, can you help me more?
    Coz I typed k.a not k.a() so we can't call it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The root issue is that you need to create a setter function. There isn't one. That's your job.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    That's easy for me. I had created more and more like those functions GetA, SetA... They didn't work.
    Code:
    int& x::GetA(){return a;}
    void x::SetA(){cout<<"Enter the value of a: ";  cin>>a; }
      OR   void x::SetA(int value){ a=value; }
    Last edited by freakyboard; 01-01-2011 at 06:55 AM. Reason: wrong typing

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The first setter is wrong. A setter accepts a value to set and returns nothing.
    Otherwise they're valid. Put them in your class and call them.
    Last edited by Elysia; 01-01-2011 at 07:02 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    We can't use those functions in main() so how can we call them?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course you can. Post your current attempt so we can see what's wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    We are not allowed to use those functions in main().

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you're screwed.
    Unless you can change the access to the member data, you must change the code in main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    The problem you are experiencing is that x::a is private, which means nothing in y can access it due to public inheritance. Either you make a protected and create public setter and getter in y or you create public setter and getter in x.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    There is an answer coz it's an exam question. I made the code smaller for you.
    Somebody told me about friend functions. What about friend functions?

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Post entire code, you may have missed something you deemend irrelevant but that is actually very relevant to the issue.

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    If you are interested in, I can post the whole code. It's really long.

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Its the only way for us to help you I think since Elysia and I presented you with the obvious solutions to the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

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