Abstract Data Type – ADT Design a class (ADT) that can represent for any numeric
ID: 3688400 • Letter: A
Question
Abstract Data Type – ADT Design a class (ADT) that can represent for any numeric value. Write a complete C++ program to demonstrate for how to use the ADT.
The program should have a class, public, private, and then using ADT the program should ask user to enter two values. Once the values are entered arithmetic operations addition,subtraction,multiplication, division must be carried out. I wrote a basic program that does these but this is not what my teacher wants. He wants the above description so that the program is using ADT's. Thank you
Explanation / Answer
Arithmetic_operations.h :
class Arithmetic_operations
{
public:
//Arithmetic_operations(); // A constructor for this class.
void set(int,int);
int addition ();
int subtraction();
int multiplication();
float division();
private:
int first;
int sec;
};
Arithmetic_operations.cpp :
#include "Arithmetic_operations.h"
#include<iostream>
/*
Arithmetic_operations::Arithmetic_operations()
{
first=0;
sec=0;
} */
void Arithmetic_operations::set(int a,int b)
{
first=a;
sec=b;
}
int Arithmetic_operations::addition () {
return first+sec;
}
int Arithmetic_operations::subtraction() {
return first-sec;
}
int Arithmetic_operations::multiplication()
{
return first*sec;
}
float Arithmetic_operations::division()
{
return first/sec;
}
main.cpp :
#include<iostream>
using namespace std;
#include "Arithmetic_operations.h"
int main()
{
int a,b;
//Arithmetic_operations a1;
cout<<"enter first number";
cin>>a;
cout<<"enter second number";
cin>>b;
set(a,b);
cout<<"ADDITION"<<a<<"+"<<b<<"= "<<addition ();
cout<<"SUBTRACTION"<<a<<"-"<<b<<"= "<<subtraction ();
cout<<"MULTIPLICATION"<<a<<"*"<<b<<"= "<<multiplication ();
cout<<"DIVITION"<<a<<"/"<<b<<"= "<<divition ();
return 0;
}
output:
enter first number 10
enter second number 5
ADDITION 10+5= 15
SUBTRACTION 10-5= 5
MULTIPLICATION 10*5= 50
DIVISION 10/5= 2