Bharathiar University
Programming in C++ LabC++ Program 2Write a C++ Program to create a class ARITHMETIC which consists of a FLOAT and an INTEGER variable. Write member functions ADD(), SUB(), MUL(), DIV() to perform addition, subtraction, multiplication and division respectively. Write a member function to get and display values.
In this page
- C++ Source Code for Visual Studio Code
- C++ Source Code for MS Dos Turbo C++
FOR VISUAL STUDIO CODE#include<iostream>#include<iomanip>using namespace std;class Arithmetic{ public: int a,b,ans; float q;
void getvalues() { cout<<"Enter two numbers:"; cin>>a>>b; } void display(int result) { cout<<result<<endl; } void displayFloat(float result) { cout<<setprecision(3)<<result<<endl; } int add() { ans=a+b; return ans;
} int sub() { ans=a-b; return ans;
} int mul() { ans=a*b; return ans;
} float div() { q=a/b; return q; }
};
int main(){ class Arithmetic arith; int r; float f; arith.getvalues(); r=arith.add(); cout<<"Addition:"; arith.display(r); r=arith.sub(); cout<<"Subtraction:"; arith.display(r); r=arith.mul(); cout<<"Multiplication:"; arith.display(r); f=arith.div(); cout<<"Division:"; arith.displayFloat(f); return 0;}
OUTPUT
Enter two numbers:209Addition:29Subtraction:11Multiplication:180Division:2.22
SOURCE CODE FOR TUBO C++(MS DOS)#include<iostream.h>
#include <iomanip.h>
#include<conio.h>
class Arithmetic
{
public:
int a,b,ans;
float q;
void getvalues()
{
cout<<"Enter
two numbers:";
cin>>a>>b;
}
void display(int
result)
{
cout<<result<<endl;
}
void
displayFloat(float result)
{
cout<<setprecision(3)<<result<<endl;
}
int add()
{
ans=a+b;
return
ans;
}
int sub()
{
ans=a-b;
return
ans;
}
int mul()
{
ans=a*b;
return
ans;
}
float div()
{
q=(float)a/b;
return
q;
}
};
int main()
{
class Arithmetic
arith;
int r;
float f;
clrscr();
arith.getvalues();
r=arith.add();
cout<<"Addition:";
arith.display(r);
r=arith.sub();
cout<<"Subtraction:";
arith.display(r);
r=arith.mul();
cout<<"Multiplication:";
arith.display(r);
f=arith.div();
cout<<"Division:";
arith.displayFloat(f);
getch();
return 0;
}
OUTPUT
#include<iostream.h>
#include <iomanip.h>
#include<conio.h>
class Arithmetic
{
public:
int a,b,ans;
float q;
void getvalues()
{
cout<<"Enter
two numbers:";
cin>>a>>b;
}
void display(int
result)
{
cout<<result<<endl;
}
void
displayFloat(float result)
{
cout<<setprecision(3)<<result<<endl;
}
int add()
{
ans=a+b;
return
ans;
}
int sub()
{
ans=a-b;
return
ans;
}
int mul()
{
ans=a*b;
return
ans;
}
float div()
{
q=(float)a/b;
return
q;
}
};
int main()
{
class Arithmetic
arith;
int r;
float f;
clrscr();
arith.getvalues();
r=arith.add();
cout<<"Addition:";
arith.display(r);
r=arith.sub();
cout<<"Subtraction:";
arith.display(r);
r=arith.mul();
cout<<"Multiplication:";
arith.display(r);
f=arith.div();
cout<<"Division:";
arith.displayFloat(f);
getch();
return 0;
}
No comments:
Post a Comment