Problem using a Loop with Classes

Hi, I am trying to loop 5 something times. The loop number would update what it is working on. For example is there a way to make the loop put the values 1-5 after t, to get t1 to t5( to use with functions in the implementation file) ie. t1.area or t1.perimeter listed below and second iteration would give t2 to use with t2.area, t2.perimter, so on.? Any pointers are most apprecitated.

Main.cpp
...
Traingle t1;
Triangle t2; // etc to t5

...
// Loop here?
// for(int i=1;5;i++){

cout << "Triangle t1 properties:" << endl ;
cout << "The sides are:" << t1.getSide1() << " " << t1.getSide2() << " " << t1.getSide3();
cout << endl;
cout << "The perimeter is: " << t1.perimeter() << endl ;
cout <<"The area is: "<<t1.area() <<endl;
}
...
All I can think of is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

using namespace std;

class test
{
public:
  int value;
  test(int i) : value(i) {}
};

int main()
{
  test t1(1), t2(2), t3(3), t4(4), t5(5);
  test *tPtr[] = {&t1,&t2,&t3,&t4,&t5};
  for(int i = 0; i < 5; i++)
    cout << "value is: " << tPtr[i]->value << endl;
  return 0;
}


Output:

value is: 1
value is: 2
value is: 3
value is: 4
value is: 5
Last edited on
You could just use an array of classes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// etc

Triangle t[5]; // calls default constructor for each arrary element

// etc

// array indices are 0-based, so add 1 if want to display 1-based indices to user
for(int i=0; i<5; i++){
    cout << "Triangle t" << (i + 1) " << properties:" << endl ;
    cout << "The sides are:" << t[i].getSide1() << endl; // etc
    // etc
}

// etc 


Andy
Last edited on
Hi Andy,
I was thinking I needed an array but all the constructors have different parameters so I cant say Triangle t[5] so then the t[i] is undeclared.
What I exactly have is in the implementation file
...
Triangle::Triangle()
{s1=1;s2=1;s3=1;}

Triangle::Triangle(int x)
{
s1=x;
s2=x;
s3=x;
}
Triangle::Triangle(int x, int y , int z)
{
s1=x, s2=y, s3=z;

and in the Main
...
int main(){
Triangle t1 ;
cout<<"Enter side for triangle t2..." ;
int s ; cin >> s ;
Triangle t2(s) ;
Triangle t3(33, 10, 5);

Triangle t4(10,33,5) ;
// generate random number between 50+75
int rt5 =(rand()%(75-50)+50);
Triangle t5(rt5);
int rt6a=(rand()%(75-50)+50);
int rt6b=(rand()%(75-50)+50);
int rt6c=(rand()%(75-50)+50);
Triangle t6(rt6a,rt6b,rt6c);
int rt7a=(rand()%(75-50)+50);
int rt7b=(rand()%(75-50)+50);
int rt7c=(rand()%(75-50)+50);
Triangle t7(rt7a,rt7b,rt7c);

// display each triangles characteristics


cout << "Triangle t1 properties:" << endl ;
cout << "The sides are:" << t1.getSide1() << " " << t1.getSide2() << " " << t1.getSide3();
cout <<"The area is: "<<t1.area() <<endl; (etc and for each 7)

return 0;
}
I was thinking I needed an array but all the constructors have different parameters

The array of pointers that Texan40 suggested would work, yes?

I only suggested the object array as an alternative as your initial example only showed triangles being default constructed. (There other approaches you could use, but I think Texan40's is the most straightforward for now.)

And on the subject of alternatives, you should generally use the initializer list to set member varibles in a constructor, e.g.

1
2
3
4
Triangle::Triangle(int x) : s1(x), s2(x), s3(x)
{
    // don't just set members here!
}


Andy

PS Also see the article "How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

Tags make it easier for us to understand your code!

(Going back and editing your earlier posts to use tags would be cool!)
Last edited on
Topic archived. No new replies allowed.