-2

So i have a simple class like this:

class Tperson
{
 private:
 ...
 public:
 void input(){...}
 void output(){...}
}

Inside the main function i created there objects of class Tperson:

Tperson p1, p2, p3;

Now normally if i wanted to give them values and print them on the screen i would do this:

p1.input();
p2.input();
p3.input();
p1.output();
p2.output();
p3.output();

But that way i have to type two much and in cases where there're more objects it'd be very inconvinient so i tried this:

for(int i = 1; i <= 3; i++)
{
    pi.input();
    pi.output();
}

^ It doesn't work, why is that? And also is there a better way to call input and output for multiple objects of a class, rather than typing them one by one manually?

1 Answers1

1

Use vectors:

std::vector<Tperson> persons(3);

for (auto& person : persons)
{
    person.input();
    person.output();
}
bolov
  • 72,283
  • 15
  • 145
  • 224