Which among the following should be encapsulated?

(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

#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


 

 

 

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.