DEV Community

Urooz Fatima
Urooz Fatima

Posted on

ENCAPSULATION

Encapsulation means binding data and functions together, and restricting direct access to the data.

Encapsulation = wrapping data (variables) + code (functions) together inside a class.
The data is kept safe/hidden from outside and only accessible through specific functions (like getters and setters).
This helps in security, control, and organization.

CODE

#include<iostream>
using namespace std;
class Movieticket{
private:
    string moviename;
    int ticketprice;
    int seatsavailable;
public:
    void setdetail(){
        cout<<"Movie: ";
        cin>>moviename;
        cout<<"Ticket Price: ";
        cin>>ticketprice;
        cout<<"Avaialable seats: ";
        cin>>seatsavailable;
    }
   void Booking(int n){
        if(n<=seatsavailable){
            cout<<"Booking "<<n<<" tickets..."<<endl;
            seatsavailable-=n; 
            cout<<"Booking successful!❣️ <3"<<endl;
        }else{
            cout<<"Sorry,only " <<seatsavailable<<" seats available!"<<endl;
        }
    }
    void available(){
        cout<<"Available seats: "<<seatsavailable<<endl;
    }
};

int main(){
    Movieticket m;
    m.setdetail();
    int n;
    cout<<"How many tickets do you want to book?";
    cout<<endl;
    cin>>n;
    m.Booking(n);
    m.available();
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
akshat_tomar_38dfacfb9f9e profile image
Akshat Tomar

This is a fantastic breakdown of encapsulation.

Using real-world examples is so helpful for clarifying core programming concepts.