Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Inheritance (http://www.go4expert.com/forums/inheritance-t24336/)

freakyboard 1Jan2011 17:35

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.

xpi0t0s 2Jan2011 16:50

Re: Inheritance
 
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; }
};



All times are GMT +5.5. The time now is 14:57.