constructor ????

i have written this program and made 3 constructors in the class and i want to call them in the main and i cant can any one help me and tell me how could i call them in the main

# include <iostream>
using namespace std ;

class Triangle {

private :
float side1 ;
float side2 ;
float side3 ;
public :
Triangle () ;
Triangle (float , float , float ) ;
Triangle ( float[3] ) ;
float printAverage () ;
};

Triangle :: Triangle ()
{
float firstSide , secondSide , thirdSide ;

side1 = firstSide ;
side2 = secondSide ;
side3 = thirdSide ;
}

Triangle :: Triangle ( float firstSide , float secondSide , float thirdSide )
{
side1 = firstSide ;
side2 = secondSide ;
side3 = thirdSide ;
}

Triangle :: Triangle ( float side[3] )
{
side1 = side[0] ;
side2 = side[1] ;
side3 = side[2] ;
}

float Triangle::printAverage ()
{
return ( side1 + side2 + side3 ) / 3 ;
}

int main ()
{
float firstSide , secondSide , thirdSide ;

cout << "Enter First Side : " ;
cin >> firstSide ;
cout << "Enter Second Side : " ;
cin >> secondSide ;
cout << "Enter Third Side : " ;
cin >> thirdSide ;

Triangle tri1 = Triangle () ;
Triangle tri2 = Triangle () ;
Triangle tri3 = Triangle () ;


system("pause");
return 0;

}
In the future, please use code tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main ()
{
float firstSide , secondSide , thirdSide ;

cout << "Enter First Side : " ;
cin >> firstSide ;
cout << "Enter Second Side : " ;
cin >> secondSide ;
cout << "Enter Third Side : " ;
cin >> thirdSide ;

Triangle tri1;  // calls default constructor.
Triangle tri2(firstSide , secondSide , thirdSide);
float sides[3] = { firstSide , secondSide , thirdSide };
Triangle tri3 = Triangle(sides) ;

system("pause");
return 0;

}
The default constructor makes no sense. You are using uninitialized variables to initialize the values.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
Triangle :: Triangle ()
{
// wrong.  They aren't initialized you have no idea what you are assigning
float firstSide , secondSide , thirdSide ;

side1 = firstSide ;
side2 = secondSide ;
side3 = thirdSide ;
}

// do this instead so that the sides are default initialized to zero.
Triangle :: Triangle () : side1(), side2(), side3()
{
}
Last edited on
can you give more help about what i should write in the main ??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main ()
{
    float firstSide , secondSide , thirdSide ;
    
    cout << "Enter First Side : " ;
    cin >> firstSide ;
    cout << "Enter Second Side : " ;
    cin >> secondSide ;
    cout << "Enter Third Side : " ;
    cin >> thirdSide ;
    
    Triangle tri1 = Triangle () ;
  
   system("pause");
    return 0;
    
}


how to call the constructor in the main ???????
I already wrote the code for you. Reread my first post. All three constructors were called.
and how can i print them on the screen
cout << ????
this is the default constructor
1
2
3
4
Triangle :: Triangle () : side1(), side2(), side3()
{
         
}


this is the main it runs but the default constructor always give me 0 why ???


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main ()
{
    float firstSide , secondSide , thirdSide ;
    
    cout << "Enter First Side : " ;
    cin >> firstSide ;
    cout << "Enter Second Side : " ;
    cin >> secondSide ;
    cout << "Enter Third Side : " ;
    cin >> thirdSide ;
    
    Triangle tri1 ;

    Triangle tri2(firstSide , secondSide , thirdSide);

    float sides[3] = { firstSide , secondSide , thirdSide };
    Triangle tri3 = Triangle(sides) ;
    
    cout << endl << "The Average of default constructor : " << tri1.printAverage() << endl << endl ;

    cout << endl << "The Average of second constructor : " << tri2.printAverage() << endl << endl ;

    cout << endl << "The Average of third constructor : " << tri3.printAverage() << endl << endl ;
    
    system("pause");
    return 0;
}
Because the default constructor of an int gives 0 in the constructor?
more declartion please
what should i write in the code to be wright and when take the sides give me the average
What is the problem? The code works fine...
no the default constructor doesnt work
Define "work". What do you expect it to do? It simply zero initializes the attributes of the class. What else could it do? if you want it to do something different put numbers within the parens to give the values whatever default values you wish. As it is currently written, it does exactly what it was written to do.
Topic archived. No new replies allowed.