6 Replies - 485 Views - Last Post: 29 January 2010 - 02:49 PM Rate Topic: -----

#1 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

i don't understand the difference between protected member , publi

Post icon  Posted 29 January 2010 - 07:15 AM

i want example in my question in oop and c++
want samply example to understand
the protected and public and private member function
please very quickly

This post has been edited by mon mon: 29 January 2010 - 07:21 AM

Is This A Good Question/Topic? 0
  • +

Replies To: i don't understand the difference between protected member , publi

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 929
  • View blog
  • Posts: 6,316
  • Joined: 18-October 08

Re: i don't understand the difference between protected member , publi

Posted 29 January 2010 - 07:38 AM

You are in the wrong forum, you want this in the C++ forum so I'll move this there.
Was This Post Helpful? 0
  • +
  • -

#3 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: i don't understand the difference between protected member , publi

Posted 29 January 2010 - 07:43 AM

View PostSixOfEleven, on 29 Jan, 2010 - 06:38 AM, said:

You are in the wrong forum, you want this in the C++ forum so I'll move this there.

i don't understand what you mean?
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 929
  • View blog
  • Posts: 6,316
  • Joined: 18-October 08

Re: i don't understand the difference between protected member , publi

Posted 29 January 2010 - 07:47 AM

You question is about classes in C++ so it belongs in the C and C++ forum, not the Site Questions & Support forum. I just moved the topic to the appropriate place so you might get the answer to your question.
Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: i don't understand the difference between protected member , publi

Posted 29 January 2010 - 07:52 AM

http://stackoverflow...ted-and-nothing

Read there, and I'm gonna give you a short example (I assume that you know something about classes and inheritance):
// base class
class T
{
private: // private section
   int a;
public: // public section
   int c;
protected: // protected section
   int b;
};

// derived class from T
// derived classes inherits the public member functions/variables,
// and the protected member functions/variables (private is still
// available only for the base class) from the base class
class D : public T
{
public:
   void do_smth_with_b()
   {
	  b = 4; // ok, b is in protected area of base class
	  c = 5; // ok, c is in public area of base class
	  a = 100; // ERROR, a is private area of base class
   }
};

// main
int main()
{
	T t; // create an object of T class
	t.c = 3; // ok, c is in public area of T class, can be accesed directly
	t.a = 2; // ERROR, a is in private area, can't access directly
	t.b = 10; // ERROR, b is in protected area, acts like a private for the base class

	return 0;
}


Hope you got the idea, though ...

This post has been edited by sarmanu: 29 January 2010 - 07:53 AM

Was This Post Helpful? 1
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: i don't understand the difference between protected member , publi

Posted 29 January 2010 - 07:54 AM

Have you tried google for an answer to this question?

http://lmgtfy.com/?q...rotected+public
Was This Post Helpful? 0
  • +
  • -

#7 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: i don't understand the difference between protected member , publi

Posted 29 January 2010 - 02:49 PM

View Postsarmanu, on 29 Jan, 2010 - 06:52 AM, said:

http://stackoverflow...ted-and-nothing

Read there, and I'm gonna give you a short example (I assume that you know something about classes and inheritance):
// base class
class T
{
private: // private section
   int a;
public: // public section
   int c;
protected: // protected section
   int b;
};

// derived class from T
// derived classes inherits the public member functions/variables,
// and the protected member functions/variables (private is still
// available only for the base class) from the base class
class D : public T
{
public:
   void do_smth_with_b()
   {
	  b = 4; // ok, b is in protected area of base class
	  c = 5; // ok, c is in public area of base class
	  a = 100; // ERROR, a is private area of base class
   }
};

// main
int main()
{
	T t; // create an object of T class
	t.c = 3; // ok, c is in public area of T class, can be accesed directly
	t.a = 2; // ERROR, a is in private area, can't access directly
	t.b = 10; // ERROR, b is in protected area, acts like a private for the base class

	return 0;
}


Hope you got the idea, though ...

thanks
i understand the difference.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1