Inheritance

freakyboard's Avatar, Join Date: Jan 2011
Newbie 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:ublic 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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
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 like this