0

The if loop doesn't work and i don't know how to fix it, i have to verify the entered numbers read from the KBD to see if they can form a triangle, but it's not working as it should.. I wrote the code in Visual Studio 2019, separately in a header file and a source file.

#include<iostream>
using namespace std;

class Triangle
{
protected:
    int a, b, c;
public:
    Triangle(int x = 0, int y = 0, int z = 0)
    {
        a = x;
        b = y;
        c = z;
    }
    float perim()
    {
        return (a + b + c) / 2;
    }
};

class Triangle_extended :public Triangle //derived class in public method
{
public:
    Triangle_extended(int x, int y, int z) :Triangle(x, y, z) {}
    float area()
    {
        float p;
        p = perim();
        return sqrt(p * (p - a) * (p - b) * (p - c));
    }
};

The problem looks like is in the main(), but i don't know where Also, when i use ob1 to find the perimeter, it gives 0 all the time, if i use ob2 for that too it gives the correct value. I have to make it work with object 1

#include"Header.h"

int main()
{
    int a, b, c;
    Triangle ob1; //object for the base class
    cout << "Enter 3 sides:";
    cin >> a >> b >> c;

    if ((a + b > c) || (a + c > b) || (b + c > a))
    {
        Triangle_extended ob2(a, b, c); //object for the derived class
        cout << "\nPerimeter is:" << ob1.perim();
        cout << "\nArea is:" << ob2.area();
    }
    else
        cout << "\nNumbers can't form a triangle!";

}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
vivi25-5
  • 55
  • 5
  • `Triangle::perim` will always return an integer, rewrite it this way for example: `(a + b + c) / 2.0;` https://stackoverflow.com/q/12447325/3365922 – fas May 14 '20 at 08:58
  • So, you've showed the working code. If you want help with code then you should show the code that isn't working. – john May 14 '20 at 08:58
  • Where did you entered the values `a` `b` `c` into `ob1` ? seems like `ob1` set to 0 in the constructor and you never updated those values – dt170 May 14 '20 at 08:58
  • if `a`, `b`, `c` are sides of triangle, why perimeter have `/ 2`. call it semi-perimeter, or do the division in Area method. – Jarod42 May 14 '20 at 09:30
  • @Jarod42 yeah,you're right, i put the division in area() method. Thank you – vivi25-5 May 14 '20 at 10:20
  • @dt170 yes, that was the problem, now everything's ok. thank you ! – vivi25-5 May 14 '20 at 10:27
  • @vivi25-5 Glad to hear that I added it as an answer so you may mark it and others will see – dt170 May 14 '20 at 11:00

2 Answers2

1

You can just pass on the values of a,b and c to the Triangle constructor via ob1, i.e,

cout << "Enter 3 sides:";
cin >> a >> b >> c;
 Triangle ob1(a,b,c);

that's it. Also, you can do p = perim()/2 in the area() method as perimeter is just the sum of all sides.So, no need to do return (a + b + c)/2; in the perim() method.

Siddhant
  • 626
  • 1
  • 7
  • 20
1

Where did you entered the values a b c into ob1 ?

#include"Header.h"

int main()
{
    int a, b, c;
    Triangle ob1; //object for the base class
    cout << "Enter 3 sides:";
    cin >> a >> b >> c;

You can either use setter to do so or just change it into

#include"Header.h"

int main()
{
    int a, b, c;

    cout << "Enter 3 sides:";
    cin >> a >> b >> c;
    Triangle ob1(a,b,c); 
    .
    .
    .
 }
dt170
  • 417
  • 2
  • 12