Home > Mobile >  c entering values two times instead of one
c entering values two times instead of one

Time:05-18

I have to solve this " create class for describing triangle and trapeze with ability to return values and finding S of the figures.. declare function which allows comparing S of the both figures.. in main function declare object triangle and trapeze and compare their areas .. " - im trying to translate it from Bulgarian to English sorry if its not translated correctly .. Anyways I came up with a solution, but when it asks me to enter value for trapeze x2 times and I can't understand why... it always takes the first 3 entered numbers but I want it to ask for input only once .. sorry if the answer is obvious enter image description here

//
//  main.cpp
//  compare S of 2 figures
//
//  Created by Георгиос Семерджиев on 17/05/22.
//

#include <iostream>
#include <cmath>
using namespace std;

class Trap // trap class with declared functions inside
{
protected:
    double a, c, h;
    void setValueTrap();
public:
    Trap();
    void Print();
    virtual double S();
};  // end trap class

class Triangle : public Trap  // triangle class with declared function for finding s() print and setting value
{
    double b;
    void setValueTriangle();
public:
    Triangle();
    void Print();
    virtual double S();
    double p(); // returning P/2
}; // end triangle class

// trap functions ...

Trap:: Trap()
{
    setValueTrap();
}

void Trap::setValueTrap()      // trap input function
{
    cout << "Trap enter a = "; cin >> a;
    cout << "Trap enter c = "; cin >> c;
    cout << "Trap enter h = "; cin >> h;
    cout << endl;
}

double Trap::S() // trap calculating and returning s()
{
    return ( (a c) * h ) / 2;
}

void Trap::Print() // printing S() for trap
{
    cout << "Trap S = " << S();
    cout << endl;
}

// Triangle functions ..

Triangle::Triangle():Trap()
{
    setValueTriangle();
}

void Triangle::setValueTriangle()  // setting value for triangle a,b,c
{
    cout << "Triangle a = "; cin >> a;
    cout << "Triangle b = "; cin >> b;
    cout << "Triangle c = "; cin >> c;
    cout << endl;
}

double Triangle::p() // returning P / 2 = p
{
    return (a b c) / 2;
}

double Triangle::S() // calculating S() of triangle
{
    return sqrt(p() * (p() - a) * (p()-b) * (p()-c));
}

void Triangle::Print()
{
    cout << "Triangle S = " << S();
    cout << endl;
}

// creating function to compare Trap S() and Triangle S()

bool Compare (Trap *F1, Trap *F2)
{
    return F1 -> S() < F2 -> S();
} // compare function


int main()
{
    Trap* trap = new Trap();
    Triangle* triangle = new Triangle();
    
    trap -> Print(); triangle -> Print();
    cout << endl;
    
    if (Compare(trap, triangle))
        cout << "the Trap S < Triangle S.." << endl;
    
    else
    {
        cout << "The Trap S > Triangle S.." << endl;
    }
    
    return 0;
}

CodePudding user response:

For the Triangle constructor you have:

Triangle::Triangle():Trap()
{
    setValueTriangle();
}

That will explicitly invoke the Trap constructor which read input. Then you will read the input for the triangle.

It's often not a good idea to read input in a constructor. It's usually better to only do basic initialization in the constructor and then get input using the fully constructed object.


Note that even if you don't have the explicit invokation of the Trap constructor, this will happen implicitly.

CodePudding user response:

Triangle inherits from Trap, and both of their constructors are called when constructing Triangle object. To avoid this set up a virtual function setValue and call it only in Trap constructor.

  •  Tags:  
  • c
  • Related