954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

accessing base class methods in a derived class

How do I access the base class protected/public methods in a derived object ? For example A and B are as defined below...

class A {
  int a;
  protected:
    int set(int v){ a = v; };
  public:
    A(){};
    int foo(){ return 0;};
};

class B : A {
   int b;
   public:
    B(){};
};

int main(int a, char *argv[])
{
  B *b = new B();
  b->set(3); // I cannot access this method?
  return 0;
}
madhavb
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Of course, you can't access protected method in main! Only member functions of derived classes can access protected members.
Re-read your C++ textbook...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

Ouch.

tazboy
Newbie Poster
19 posts since Jun 2009
Reputation Points: 6
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You