I have the following code:
#include <iostream>
using namespace std;
int a, b, sqr;
const int P = 3.14; //Later for circles...
string s1;
class MathsFunctions{
public:
virtual void square(int a, int b)=0;
};
class TriangleFunc: public MathsFunctions{
public:
void square(int a, int b){
sqr = (a * b)/2;
cout << "Square of triangle is: "<< sqr << endl;
}
};
class RectangleFunc: public MathsFunctions{
public:
void square(int a, int b){
sqr = a * b;
cout << "Square of rectangle is: "<< sqr << endl;
}
};
void getNumbers(){
cout << "Enter the first number: "<<endl;
cin >> a;
cout << "Enter the second number: "<< endl;
cin >> b;
}
void chooseTheFigure(){
cout << "Choose the figure (rectangle or triangle): "<< endl;
cin >> s1;
}
int main(){
chooseTheFigure();
getNumbers();
if(s1 == "rectangle" || "Rectangle"){
RectangleFunc r;
MathsFunctions * m = &r;
m -> square(a,b);
};
if (s1 == "triangle" || "Triangle"){
TriangleFunc t;
MathsFunctions *m = &t;
m -> square(a,b);
};
}
I created a program which is count the square of rectangle or triangle. There is a condition in main() but in the end program shows both results. How can I improve that?
Screenshot of output of the program: