Tuesday, 1 August 2017

Write a C++ program that performs operations like Calculator by using Classes

Write a C++ program that performs operations like Calculator by using Classes.

What is a Class in C++ ?

The essential reason of C++ programming is to add object orientation to the C programming language and Classes are the principal feature of C++ that supports Object-oriented programming and are often called user-described types.

Before you create an object in C++, you need to define a class.
This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

Code:


#include<iostream>
#include<conio.h>
using namespace std;
class calculator
{
private:
float a,b;
public:
calculator()
{
a=0;
b=0;
}
calculator(float c,float d)
{
a=c;
b=d;
}
void diff()
{
cout<<a-b;
}
void showvalue()
{
cout<<a<<endl;
cout<<b<<endl;
}
void add(calculator ,calculator);
void sub(calculator ,calculator);
void mul(calculator ,calculator);
};
void calculator::add(calculator l,calculator m)
{
a=l.a+m.a;
b=l.b+m.b;
}
void calculator::mul(calculator l,calculator m)
{
a=l.a*m.a;
b=l.b*m.b;
}
void calculator::sub(calculator l,calculator m)
{
a=l.a-m.a;
b=l.b-m.b;
}
int main()
{
calculator i;
calculator j(4,6);
calculator k(5,4);
i.add(j,k);
i.showvalue();
i.sub(j,k);
i.showvalue();
i.mul(j,k);
i.showvalue();
return 0;
}

Output:















Try it yourself.
If you have any problem you can tell me on comment box i will help you.
thanks.

Viral Programs

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment