This is a very basic C++ program that demonstrates data protection in a very simple way.
Data protection in Object Oriented Programming is controlling access to all attributes is one of the most important concepts of object-oriented design. By using methods to control access to attributes you can provide a much higher level of security for your class as well as providing many programming advantages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream.h> class rectangle { // A simple class int height; int width; public: int area(void); // with two methods void initialize(int, int); }; int rectangle::area(void) //Area of a rectangle { return height * width; } void rectangle::initialize(int init_height, int init_width) { height = init_height; width = init_width; } struct pole { int length; int depth; }; main() { rectangle box, square; pole flag_pole; box.initialize(12, 10); square.initialize(8, 8); flag_pole.length = 50; flag_pole.depth = 6; cout << "The area of the box is " << box.area() << "\n"; cout << "The area of the square is " << square.area() << "\n"; } |
Output of the Program:
1 2 3 | The area of the box is 120 The area of the square is 64 |