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{
private:
int a, b;
public:
Arithmetic(int a, int b){
this->a = a; this->b = b;
}
int Add(){
return this->a + this->b;
}
int Subtract(); //Subtract declared here
};
int Arithmetic::Subtract(){ //Subtract defined outside the class
return this->a - this->b;
}
int main(){
int a, b;
cout<<"input first number: ";
cin>>a;
cout<<"input second number: ";
cin>>b;
Arithmetic arithmetic(a, b);
cout<<a<<" + "<<b<<" = "<<arithmetic.Add()<<endl;
cout<<a<<" - "<<b<<" = "<<arithmetic.Subtract()<<endl;
return 0;
}
Comments
Leave a comment