Let’s assume we have a class ‘Arithmetic’ with two member functions Add() and Subtract(). Suppose Add() function is defined within class then how will the subtract() function be defined out of the class boundary. Explain it with the help of some suitable program.
#include <iostream>
using namespace std;
class Arithmetic{
int x, y;
public:
Arithmetic(int a, int b){
x = a; y = b;
}
int Add(){return x + y;}
int Subtract();
};
int Arithmetic::Subtract(){
return x - y;
}
int main(){
int x, y;
cout<<"Enter first integer: ";
cin>>x;
cout<<"Enter second integer: ";
cin>>y;
Arithmetic arithmetic(x, y);
cout<<x<<" + "<<y<<" = "<<arithmetic.Add()<<endl;
cout<<x<<" - "<<y<<" = "<<arithmetic.Subtract()<<endl;
return 0;
}
Comments
Leave a comment