Answer to Question #231989 in C++ for mahnoor

Question #231989

Write a Math Class will perform the basic functions (+, -, *, /). Write two classes the SimpleCalculator and ScientificCalculator that will inherit the Math class.

 

Write specific methods, attributes to the classes and show the results.



1
Expert's answer
2021-09-01T23:58:07-0400


#include <iostream>


using namespace std;


class Math{
    private:
        int n1;
        int n2;
    public:
        void setNum1(int a){
            n1=a;
        }
        void setNum2(int b){
            n1=b;
        }
        int getNum1(){
            return n1;
        }
        int getNum2(){
            return n2;
        }
};
class SimpleCalculator: public Math{
    public:
    void add(){
        cout<<"\nThe sum of "<<getNum1()<<" and "<<getNum2()<<" is = "<<getNum1()+getNum2();
    }
    void sub(){
        cout<<"\nThe sub of "<<getNum1()<<" and "<<getNum2()<<" is = "<<getNum1()-getNum2();
    }
};


class ScientificCalculator: public Math{
    public:
    void mul(){
        cout<<"\nThe mul of "<<getNum1()<<" and "<<getNum2()<<" is = "<<getNum1()*getNum2();
    }
    void division(){
        cout<<"\nThe division of "<<getNum1()<<" and "<<getNum2()<<" is = "<<getNum1()/getNum2();
    }
};
int main()
{
    Math m;
    m.setNum1(10);
    m.setNum2(20);
    
    SimpleCalculator c1;
    c1.add();
    c1.sub();
    
    ScientificCalculator c2;
    c2.mul();
    c2.division();
    
    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