Addition of Two numbers using Data Abstraction
In this, we are going to see a program on Addition of Two numbers using data abstraction in C++ Programming Language. 
 


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

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

class Sum
{
    private:
    int n1, n2, res; // private variables which cannot be accessed normally by non member functions
    public:
    void add()
    {
        cout << "Enter two numbers: ";
        cin >> n1 >> n2;
        res = n1 + n2;
        cout << "Sum of two number is: " << res << endl;
    }
};

void main()
{
    clrscr();
    Sum s;
    s.add(); // public member accessed from outside
    getch();
}

//.......Coded by SAHIL SHAIKH


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

#include <iostream>
using namespace std;

class Sum
{
    private:
    int n1, n2, res; // private variables which cannot be accessed normally by non member functions
    public:
    void add()
    {
        cout << "Enter two numbers: ";
        cin >> n1 >> n2;
        res = n1 + n2;
        cout << "Sum of two number is: " << res << endl;
    }
};

int main()
{
    Sum s;
    s.add(); // public member accessed from outside
    return 0;
}

//.......Coded by SAHIL SHAIKH


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post