Thursday, July 31, 2025

HYBRID CALCULATOR WITH TEMPLATES(C++)

#include <iostream>
#include<cmath>
 using namespace std;
 template <class T>
  class calculator{//making base class
public:
T sum(T a,T b){
return a+b;
}
T difference(T a,T b){
   
    return a-b;
}
T product(T a,T b){
    return a*b;
}
T quotient(T a,T b){
   
    return a/b;
}
T remainder(T a,T b){
   
return a%b;
}
 virtual void display(T a,T b){//displaying main arithemetic operations-addition,subtraction,division and multiplication
    cout<<" the sum of two numbers "<<a<<" and " <<b<<" is "<<sum(a,b)<<endl;
    cout<<" the difference of two numbers " <<a<<" and "<< b<< " is "<<difference(a,b)<<endl;
    cout<<" the product of two numbers "<<a<< " and "<< b<<" is "<<product(a,b)<<endl;
    cout<<" the quotient of two numbers "<<a<<" and "<< b<<" is  "<<quotient(a,b)<<endl;
    cout<<" the remainder of two numbers "<<a<<" and "<< b<<" is "<<remainder(a,b)<<endl;
}
 };
 template <class T>
 class scientificCalcualtor:virtual public calculator<T>{//making derived class
public:
T square(T a){
    return pow(a,2);
}
T cube(T a){
    return pow(a,3);
}
T squareroot(T a){
return sqrt(a);
}
void display(T a){//displaying sceintific operations implemented using cmath
cout<<" the square of "<<a<<" is "<<square(a)<<endl;
cout<<" the cube of " <<a<<" is "<<cube(a)<<endl;
cout<<" the squareroot of "<<a<< " is "<<squareroot(a)<<endl;
}
void display(T a,T b){//drawing from base class
    calculator<T>::display(a,b);
}

 };
 template <class T>
 class hybridCalc:virtual public calculator<T>,virtual public scientificCalcualtor<T>{//multilevelinheritance
    public:
void display(T a,T b){//using display function from display classes
calculator<T>::display(a,b);
scientificCalcualtor<T>::display(a);
scientificCalcualtor<T>::display(b);

}



 };
 
 int main(){
hybridCalc<int> H;
H.display(76,45);

 return 0;
 }

No comments:

Post a Comment

list in c#

  using System ; using System . Collections . Generic ; class Program {     static void Main () {         // Write your C# code here  ...