Answer to Question #185919 in C++ for saad

Question #185919

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. 


1
Expert's answer
2021-04-26T16:23:57-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS