calculating Area of rectangle using Data Encapsulation || Data Encapsulation || C++





In this program, we are going to find Area of Rectangle using Data Encapsulation in C++ Programming Language. 
 


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

class Rectangle
{
    public:
    int length;
    int breadth;
    Rectangle(int len, int brth) : length(len), breadth(brth) {}
    int getArea()
    {
        return length * breadth;
    }
};

int main()
{
    int l, b;
    cout << "Enter the length and breadth: ";
    cin >> l >> b;
    Rectangle rect(l, b);
    cout << "Area = " << rect.getArea();
    return 0;
}

//.......Coded by RISHAB NAIR

The Code given below can be used in Turbo C++ Compilers: -

#include <iostream.h>
#include <conio.h>

class Rectangle
{
    public:
    int length;
    int breadth;
    Rectangle(int len, int brth) : length(len), breadth(brth) {}
    int getArea()
    {
        return length * breadth;
    }
};

void main()
{
    clrscr();
    int l, b;
    cout << "Enter the length and breadth: ";
    cin >> l >> b;
    Rectangle rect(l, b);
    cout << "Area = " << rect.getArea();
    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

Previous Post Next Post