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


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

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

class Squarenum
{
    private:
    int num; // private variables which cannot be accessed normally by non member functions
    public:
    Squarenum(int n)
    {
        num = n;
        num = pow(num, 2);
    }
    void display()
    {
        cout << "The square of number is " << num;
    }
};

void main()
{
    clrscr();
    int number;
    cout << "Enter the number: ";
    cin >> number;
    Squarenum s(number);
    s.display(); // public member accessed from outside
    getch();
}

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



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

#include <iostream>
#include <math.h>
using namespace std;

class Squarenum
{
    private:
    int num; // private variables which cannot be accessed normally by non member functions
    public:
    Squarenum(int n)
    {
        num = n;
        num = pow(num, 2);
    }
    void display()
    {
        cout << "The square of number is " << num;
    }
};

int main()
{
    int number;
    cout << "Enter the number: ";
    cin >> number;
    Squarenum s(number);
    s.display(); // 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