Monday, June 29, 2009

ENCAPSULATION: public, protected and private



NOTE : -
THIS CONTENT IS NOT ORIGINAL

30. ENCAPSULATION: public, protected and private

The public: directive means the variables or the methods below can be accessed and used everywhere in the program.

If you want the variables and methods to be accessible only to methods of the class AND to methods of derived classes, then you must put the keyword
protected:before them.

If you want variables or methods to be accessible ONLY to methods of the class, then you must put the keyword
private: before them.

The fact that variables or methods are declared private or protected means that nothing external to the class can access or use them. That's ENCAPSULATION. (If you want to give a specific function the right to access those variables and methods, then you must include that function's prototype inside the class definition, preceded by the keyword
friend.)

Good practice is to encapsulate all the variables of a class. This can sound strange if you're used to structs in C. Indeed a struct only makes sense if you can access its data... In C++ you have to create methods to acces the data inside a class. The example below uses the basic example of chapter 17, yet declares the class data to be protected:


using namespace std;
#include

class vector
{
protected:

double x;
double y;

public:

void set_x (int n)
{
x = n;
}
   void set_y (int n)
{
y = n;
}
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};

int main ()
{
vector a;

a.set_x (3);
a.set_y (4);

cout << "The surface of a: " << a.surface() << endl;

return 0;
}


Output
The surface of a: 12



The example above is a bit odd since the class data x and y can be set but they cannot be read back. Any attempt in function main () to read
a.x or a.y will result in a compilation error. In the next example, x and y can be read back:


using namespace std;
#include

class vector
{
protected:

double x;
double y;

public:

void set_x (int n)
{
x = n;
}
   void set_y (int n)
{
y = n;
}

double get_x ()
{
return x;
}
   double get_y ()
{
return y;
}
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};

int main ()
{
vector a;

a.set_x (3);
a.set_y (4);

cout << "The surface of a: " << a.surface() << endl;
cout << "The width of a: " << a.get_x() << endl;
cout << "The height of a: " << a.get_y() << endl;

return 0;
}


Output
The surface of a: 12
The width of a: 3
The height of a: 4



In C++ one is not supposed to access the data of a class directly. Methods have to be declared. Why is this? Many reasons exist. One is that this allows you to change the way the data is represented within the class. Another reason is this allows data inside the class to be cross-dependent. Suppose
x and y must always be of the same sign, otherwise ugly things can happen... If one is allowed to access the class data directly, it would be easy to impose say a positive x and a negative y. In the example below, this is strictly controlled:


using namespace std;
#include

int sign (double n)
{
if (n >= 0) return 1;
return -1;
}

class vector
{
protected:

double x;
double y;

public:

void set_x (int n)
{
x = n;
if (sign (x) != sign(y)) y = -y;
}
   void set_y (int n)
{
y = n;
if (sign (y) != sign(x)) x = -x;
}

double get_x ()
{
return x;
}
   double get_y ()
{
return y;
}
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};

int main ()
{
vector a;

a.set_x (-3);
a.set_y (4);

cout << "The surface of a: " << a.surface() << endl;
cout << "The width of a: " << a.get_x() << endl;
cout << "The height of a: " << a.get_y() << endl;

return 0;
}


Output
The surface of a: 12
The width of a: 3
The height of a: 4




BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:

3G S

JAIL BREAK

iPHONE

BILL GATES

StEvE JoBs

steve jobs

STEVE JOBS

Steve Jobs

Love Country

SD

Burroughs

Lift

Joseph Johnson

Apple Rainbow

Apple Blue

Apple Laser Printer