I am designing and implementing a class where I have to include two initialization operations using the class's constructors. One is the default initialization (that I think I have done properly) and the other one is the initialization from the user inputs which is supposed to be in the constructor itself(where I still have trouble to write it). I am using separate compilation, so I show the code from the file with the class and the main function from the .cpp file. I am using Dev-C++ and part of the code is below. Thanks for your help.

#include <iostream>
#include <exception>
#include <math.h>


///#include "Exception.h"


#ifndef TRIANGLE_H
#define TRIANGLE_H

using namespace std;

class Triangle

{
   private:
       double side1;
       double side2;
       double side3;

       double angle_side1_side2;
       double angle_side1_side3;
       double angle_side2_side3;


  public:
    //default constructor with default initialization   
    Triangle::Triangle(): side1(0), side2(0), side3(0), angle_side1_side2(0), angle_side1_side3(0), angle_side2_side3(0)
       {

       }

//constructor with user inputs, but I know there is something wrong...
Triangle::Triangle(double s1, double s2, double s3)
  { 
   cout<<"enter your own values:"<<endl;
   cin>>s1>>s2>>s3;
   side1=s1;
   side2=s2;
   side3=s3;
   cout<<"the current lengths of sides of your triangle are: " <<endl;
  }

 double display_triangle_sides()
   {    
        cout<<side1<<" "<<side2<<" "<<side3<<" ";                               

   }

 double display_triangle_Area()
   {    
        double S=(side1+side2+side3)/2; //semiperimeter
        double T=sqrt(S*(S-side1)*(S-side2)*(S-side3));      //Heron formula to calculate Area of triangle

       return T;
   }
};

endif


     //*****************************main function below************************

 #include <iostream>
 #include <exception>



 #include "Untitled1.h"


 using namespace std;

 int main()
 {


  Triangle tr;


  cout<<" length of each side of the current triangle : ";
  tr.display_triangle_sides();  //I want to show here the values entered by the users



  cout<<endl<<"Here is the Area of the current triangle : " <<    tr.display_triangle_Area()<<endl;
  cout<<"type of triangle:"<<endl;
  tr.Type_of_Triangle();


   system("Pause");
   return 0;
 }
3

There are 3 best solutions below

2
On BEST ANSWER

A constructor where the user has to enter values manually is appalling design. Are you sure you have been told to do that?

The correct way to do this is something like this

class Triangle
{
    Triangle::Triangle(): side1(0), side2(0), side3(0)
    {
    }

    Triangle::Triangle(double s1, double s2, double s3) : side1(s1), side2(s2), side3(s3)
    { 
    }

    ...

};

int main()
{
   double s1, s2, s3;
   cin >> s1 >> s2 >> s3;
   Triangle t(s1, s2, s3);

   ...

}

In order words you enter values in the main function, and then you pass the values to the constructor.

The reason that your way of doing things is so bad is that it makes your Triangle constructor only useable in the specific context of the program you are writing now. You should design your classes so that they are reusable in different programs. This is probably a hard thing for you to appreciate now when you're just beginning, since you are focused on just getting one program to work, not thinking about programs you might write in the future.

If you really have been told that you must do this, then you are being taught by an incompetant.

0
On

You need to do like this:

int main()
 {

  cout<<"enter your own values"<<endl;
  double s1,s2,s3;
  cin>>s1>>s2>>s3;
  Triangle tr(s1,s2,s3);

  cout<<" length of each side of the current triangle : ";
  tr.display_triangle_sides();  //I want to show here the values entered by the users



  cout<<endl<<"Here is the Area of the current triangle : " <<        tr.display_triangle_Area()<<endl;
  cout<<"type of triangle:"<<endl;
  tr.Type_of_Triangle();


   system("Pause");
   return 0;
 }

Also change consrtructor to

//constructor with user inputs, but I know there is something wrong...
Triangle::Triangle(double s1, double s2, double s3):side1(s1),side2(s2)side3(s3)
  { 
  }
0
On

According to your task description you need an opportunity to create different configurations of the object class instances, configurartion of particular instance depends on user input.
So as one of the method to approach your task I advice your to read about programming pattern builder.
Object of class builder is responsible for creation of instances of particular class it offers such advantages as flexiable objects creation and error prevention. I wrote a small example for you:

class Triangle
{
    friend class TriangleBuilder;
public:
    double display_triangle_sides()
    {    
        cout<<side1;                               
    }
private:
      double side1;
      Triangle::Triangle(): side1(0){}
      Triangle::Triangle(int v): side1(v){}



};
class TriangleBuilder
{
public:
    void BuildDefaultTriangle(void)
    {
        this->m_instance = new Triangle;
    }
    void BuildCustomTriangle(void)
    {
        cout << "All right!! Enter side length)\n" << endl;
        int tmp;
        cin >> tmp;
        this->m_instance = new Triangle(tmp);
    }
    Triangle* getTriangle(void)
    {
        return this->m_instance;
    }
private:
    Triangle* m_instance;
};