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




Posted 29 January 2010 - 07:15 AM
This post has been edited by mon mon: 29 January 2010 - 07:21 AM
Posted 29 January 2010 - 07:38 AM
Posted 29 January 2010 - 07:47 AM
Posted 29 January 2010 - 07:52 AM
// 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;
}
This post has been edited by sarmanu: 29 January 2010 - 07:53 AM
Posted 29 January 2010 - 02:49 PM
sarmanu, on 29 Jan, 2010 - 06:52 AM, said:
// 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;
}
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
