Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Protected Members

Status
Not open for further replies.

joeGrammar

Programmer
Jun 4, 2001
162
CA
Interesting Tidbit I discovered:

I have 2 files, one which has a class declaration and another which creates a class object, the object then tried to use a protected member and it cannot.

example:

header file:
#include <iostream.h>

class Protected
{
protected:

int printer(){ cout << &quot;hey!!&quot;; }

}

main.cpp

#include <iostream.h>
#include &quot;protect.h&quot;


int main()

{

Protected* p;

p->printer();

}

Why won't this work?
 
protected members are treated as private. The way calling protected members are done is through inheritance (or friends but that give full access to public/protected/private

this is an example of inheritance


class Protected
{
protected:

int printer(){ cout << &quot;hey!!&quot;; }

}

class usage: public Protected
{
public:
int print()(return printer();}
}


int main()

{

usage p;// if you have a pointer you must &quot;new&quot;

p->print();

}


matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top