Skip to main content

Building a Binary Calculator With Object Oriented Programming in C++

Let's begin!

Creating a basic calculator is a no biggie! So we are applying the concept of object oriented programming and making this program work until the user ask to end the task, in this calculator we can also intake the value as much as we want without restarting the program!

It's one of basis and yet a core project where majority of your programming skills are used!

Happy Programming!

#include<iostream>
using namespace std;
class BinaryCalculator //Creating the Class
{
public:
int add(int a, int b) //Function to add the numbers
{
x=a;
y=b;
return a+b;
}
int pro(int a, int b) //Function to multiply the numbers
{
x=a;
y=b;
return a*b;
}
int sub(int a, int b) //Function to subtract the numbers
{
x=a;
y=b;
return a-b;
}
int div(int a,int b) //Function to divide the numbers
{
x=a;
y=b;
return a/b;
}
private:
int x,y; //Declaring the Class Variables
};
int main() //Main Function
{
int a,b;
cout<<"Input first number: "<<endl;
cin>>a;
cout<<"Input second number: "<<endl;
cin>>b;
BinaryCalculator BCal; //Declaring the object
int r=6;//Creating a variable with any random input to intialise the loop
while(r!=0) //To continue the program until the valu isn't zero
{
cout<<"1 for Sum"<<endl<<"2 for Subtraction"<<endl<<"3 for Product"<<endl<<"4 for Division"<<endl<<"5 to Renter the Value"<<endl<<"0 to Exit"<<endl;//Declaring the Commands
cin>>r;
if(r==1)//1 for Sum
{
cout<<"Sum: "<<BCal.add(a,b)<<endl; //Calling the function with the help of object
}
else if (r==2)//2 for Subtraction
{
cout<<"Subtraction: "<<BCal.sub(a,b)<<endl; //Calling the function with the help of object
}
else if (r==3)//3 for Product
{
cout<<"Multiply: "<<BCal.pro(a,b)<<endl; //Calling the function with the help of object
}
else if (r==4)//4 for Division
{
cout<<"Division: "<<BCal.div(a,b)<<endl; //Calling the function with the help of object
}
else if(r==5)//5 to Renter the Value
{
cout<<"Input first number: "<<endl;
cin>>a;
cout<<"Input second number: "<<endl;
cin>>b;
}
else if (r==0)//0 to Exit the loop
{
cout<<"Good Bye";
}
else//For any other input
{
cout<<"Wrong Input";
}
}
return 0;
}
view raw binary_cal.cpp hosted with ❤ by GitHub

Comments

Popular posts from this blog

Beginners Guide to Code

Coding  750+ hours and that's my advice! Which language? There's a lot of languages available you name it! Choosing the right language is a must because it will be responsible for your coding style, approach for problems solving and it's application. My first coding language was C++, if you see my code most of them follow's it style, my second language was Python, most of the approach for my problem solving follows it. Later on I started learning Kotlin and Dart and they both follow the same style as above! I will recommend you C++ if you are really into programming after that you will be experienced enough to choose any other language. Single or multiple language? Single language won't make you a good developer and that's a fact! Every language has it's unique style but why to limit yourself only to that? Programmer is analyzed by it's approach to solve a problem. Why Iron Man uses multiple armors? Because every armors has it's unique specialty. Sam...

Taking Input and Printing the Result in C++

  Let's begin: C++ has many datasets like char, float, int and string etc. Here provided the complete code: