Write a C++ program that defines the value-returning functions for the arithmeti
ID: 3567258 • Letter: W
Question
Write a C++ program that defines the value-returning functions for the arithmetic integer...
Write a C++ program that defines value-returning functions for the arithmetic integer binary operations plus, minus. multiply, and divide. You should also write a value returning function for the unary operation negate. Use your function declarations to compute the following as a single expression: (((4 *(R/2 ))+7) Your main function should not declare a single variable, but it should print as output the result of computing this expression. Your implementation is correct if your output yields the value -11.Explanation / Answer
Program code:
// FunctionsOperatorOverloading.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
//class Exprssion
class Expression
{
int a;
int b;
public:
//constructor
Expression()
{
a=1;
}
//single parameterised constructor
Expression(int lhs)
{
a=lhs;
}
//double parameterised constructor
Expression(int lhs, int rhs)
{
a=lhs;
b=rhs;
}
//get functions
int Geta() { return a; }
int Getb() { return b; }
//operators overloading function
friend Expression operator+(const Expression &a, const Expression &b);
friend Expression operator-(const Expression &a, Expression &b);
friend Expression operator-(const Expression &a, int b);
friend Expression operator*(const Expression &a, const Expression &b);
friend Expression operator/(const Expression &a, const Expression &b);
friend Expression operator~(const Expression &a);
friend Expression operator^(const Expression &a, int pow);
//friend Expression operator*(const Expression, const Expression &cM);
};
Expression operator+(const Expression &a, const Expression &b)
{
return (a+b);
}
Expression operator-(const Expression &a, Expression &b)
{
return (a-b);
}
Expression operator*(const Expression &a, const Expression &b)
{
return (a*b);
}
Expression operator/(const Expression &a, const Expression &b)
{
return (a/b);
}
Expression operator-(const Expression &a)
{
return -(a);
}
Expression operator-(const Expression &a, int b)
{
return (a-b);
}
//additional factorial and power functions
int factorial(int a);
int power(int b, int n);
//main functions;
int main()
{
Expression cM1(5, 4);
Expression cM2(8, 2);
Expression cM3(7);
Expression cMFinal = -((cM1.Geta()-cM1.Getb())*(cM2.Geta()/cM2.Getb())+cM3.Geta());
cout << "Result: "<< cMFinal.Geta();
cout<<endl;
cout<<"Factorial and power result: "<<power((factorial(5)+factorial(4)),3);
cout<<endl;
system("pause");
return 0;
}
//definition of factorial
int factorial(int a)
{
return (a==1 || a==0)?1:a*factorial(a-1);
}
//definition of power function
int power(int b, int n)
{
int c=1;
for(int i=1;i<=n;i++)
{
c=c*b;
}
return c;
}
------------------------------------------------------------------------------------
Sample Output:
Result: -11
Factorial and power result: 2985984
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.