(A). The data members that are members of some another class
(B). The data members which are likely to change in long terms
(C). The data members which are intended to be changed
(D). The data members which are likely to change in the near future
MCQ Answer: The data members which are likely to change in the near future
C++ Example of data members encapsulation in a class
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#include <iostream> using namespace std; class Rectangle { private: // Variables required for area calculation int Rectangle_Length; int Rectangle_Width; public: // Setter function for Rectangle_Length void setRectangle_Length(int len) { Rectangle_Length = len; } // Setter function for Rectangle_Width void setRectangle_Width(int Width2) { Rectangle_Width =
Width2; } // Getter function for Rectangle_Length int getRectangle_Length() { return Rectangle_Length; } // Getter function for Rectangle_Width int getRectangle_Width() { return Rectangle_Width; } // Function to calculate area int getArea() { return Rectangle_Length * Rectangle_Width; } }; int main() { // Create object of Rectangle class Rectangle rectangle1; // Initialize Rectangle_Length using Setter function rectangle1.setRectangle_Length(4); // Initialize Rectangle_Width using Setter function rectangle1.setRectangle_Width(3); // Access Rectangle_Length using Getter function cout << "Rectangle_Length = " << rectangle1.getRectangle_Length() << endl; // Access Rectangle_Width using Getter function cout << "Rectangle_Width = " << rectangle1.getRectangle_Width() << endl; // Call getArea() function cout << "Area = " << rectangle1.getArea(); return 0; } |
Output
Rectangle_Length = 4
Rectangle_Width = 3
Area = 12
Which of the following can be encapsulated?
(A). data members
(B). member functions
(C). both of these
(D). None of these
MCQ Answer: (C). both of these