Â
In this program, we are going to Learn a basic program of Data Encapsulation in C++ Programming Language.Â
Â
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
class Encapsulation
{
private:
int x;
public:
void set(int a)
{
x = a;
}
int get()
{
return x;
}
};
// main function
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
Encapsulation obj;
obj.set(num);
cout << "The number you entered is: " << obj.get();
return 0;
}
//.......Coded by RISHAB NAIR
The Code given below can be used in Turbo C++ Compilers: -
#include <iostream.h>
#include <conio.h>
class Encapsulation
{
private:
int x;
public:
void set(int a)
{
x = a;
}
int get()
{
return x;
}
};
// main function
void main()
{
clrscr();
int num;
cout << "Enter a number: ";
cin >> num;
Encapsulation obj;
obj.set(num);
cout << "The number you entered is: " << obj.get();
getch();
}
//.......Coded by RISHAB NAIR
#ENJOY CODING
Â
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP