|
Answer» Encapsulation : Encapsulation is the mechanism by which data and associated operations/methods are BOUND together and thus hide the data from outside world. It's also called data hiding. In c++, encapsulation achieved using the access specifiers (private, PUBLIC and protected). Data members will be declared as private (thus protecting from direct access from outside) and public methods will be provided to access these data. Consider the below class class Person { private: int age; public: int getAge(){ return age; } int setAge(int value){ if(value > 0){ age = value; } } };
In the class Person, access to the data field age is protected by declaring it as private and providing public access methods. What would have happened if there was no access methods and the field age was public? Anybody who has a Person object can set an invalid value (negative or very large value) for the age field. So by encapsulation we can preventing direct access from outside, and thus have complete control, protection and integrity of the data. Data abstraction : Data abstraction refers to hiding the internal implementations and show only the necessary DETAILS to the outside world. In C++ data abstraction is implemented using interfaces and abstract classes. class Stack { public: virtual void push(int)=0; virtual int pop()=0; }; class MyStack : public Stack { private: int arrayToHoldData[]; //Holds the data from stack public: void push(int) { // implement push operation using array } int pop(){ // implement pop operation using array } }; In the above example, the outside world only need to KNOW about the Stack class and its push, pop operations. Internally stack can be implemented using arrays or linked lists or queues or anything that you can think of. This means, as long as the push and pop method performs the operations work as expected, you have the freedom to change the internal implementation with out affecting other applications that use your Stack class. Inheritance : Inheritance allows one class to inherit PROPERTIES of another class. In other words, inheritance allows one class to be defined in terms of another class. class SymmetricShape { public: int getSize() { return size; } void setSize(int w) { size = w; } protected: int size; }; // Derived class class Square: public SymmetricShape { public: int getArea() { return (size * size); } };
In the above example, class Square inherits the properties and methods of class SymmetricShape. Inheritance is the one of the very important concepts in C++/OOP. It helps to modularise the code, improve reusability and reduces tight coupling between components of the system. Encapsulation : Encapsulation is the mechanism by which data and associated operations/methods are bound together and thus hide the data from outside world. It's also called data hiding. In c++, encapsulation achieved using the access specifiers (private, public and protected). Data members will be declared as private (thus protecting from direct access from outside) and public methods will be provided to access these data. Consider the below class class Person { private: int age; public: int getAge(){ return age; } int setAge(int value){ if(value > 0){ age = value; } } }; In the class Person, access to the data field age is protected by declaring it as private and providing public access methods. What would have happened if there was no access methods and the field age was public? Anybody who has a Person object can set an invalid value (negative or very large value) for the age field. So by encapsulation we can preventing direct access from outside, and thus have complete control, protection and integrity of the data. Data abstraction : Data abstraction refers to hiding the internal implementations and show only the necessary details to the outside world. In C++ data abstraction is implemented using interfaces and abstract classes. class Stack { public: virtual void push(int)=0; virtual int pop()=0; }; class MyStack : public Stack { private: int arrayToHoldData[]; //Holds the data from stack public: void push(int) { // implement push operation using array } int pop(){ // implement pop operation using array } }; In the above example, the outside world only need to know about the Stack class and its push, pop operations. Internally stack can be implemented using arrays or linked lists or queues or anything that you can think of. This means, as long as the push and pop method performs the operations work as expected, you have the freedom to change the internal implementation with out affecting other applications that use your Stack class. Inheritance : Inheritance allows one class to inherit properties of another class. In other words, inheritance allows one class to be defined in terms of another class. class SymmetricShape { public: int getSize() { return size; } void setSize(int w) { size = w; } protected: int size; }; // Derived class class Square: public SymmetricShape { public: int getArea() { return (size * size); } }; In the above example, class Square inherits the properties and methods of class SymmetricShape. Inheritance is the one of the very important concepts in C++/OOP. It helps to modularise the code, improve reusability and reduces tight coupling between components of the system.
|