array of objects

Hello!
We have a class: product
and 3 objects: toy, book and dress.
How can we reach objects as elements of an array?
Many thanks!

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include<iostream>
using namespace std;


class product {
   
  public:
    int ID;
    string name;
    double price;
    void set(int, string, double);
    int getID(){return ID;};
    string get_name(){return name;};
    double get_price(){return price;};
    
    
};

void product::set (int a, string b, double c) {
  ID = a;
  name = b;
  price=c;
}


int main(){    

  product toy;
  product book;
  product dress;

  toy.set (1, "Toy", 11.22);
  book.set (2, "Book", 22.33);
  dress.set (3, "Dress", 33.44);

         cout<<toy.ID<<endl<<toy.name<<endl<<toy.price<<endl<<endl<<book.ID<<endl<<book.name<<endl<<book.price<<endl<<endl<<dress.ID<<endl<<dress.name<<endl<<dress.price<<endl;
cout<<endl;
cout<<toy.getID()<<" "<<book.getID()<<" "<<dress.getID()<<endl;
cout<<toy.get_name()<<" "<<book.get_name()<<" "<<dress.get_name()<<endl;
cout<<toy.get_price()<<"   "<<book.get_price()<<"   "<<dress.get_price()<<endl;

  product array[3];
    for (int i=1;i<4;i++){
      array[i]=
return 0;

}
Last edited on
1
2
3
array[i].set(/*...*/)
//or
array[i].name = //... etc. 
1
2
  product array[3];
    for (int i=1;i<4;i++){// error: indexing is 0..n-1, not 1..n 

No. You cannot copy values from individual variables into array elements with a loop. You can:
1
2
3
array[0] = toy;
array[1] = book;
...

However, why to have separate variables in the first place, when you could read directly into array elements with a loop?
Thanks all!!!
First of all, it was said, make an array of OBJECTS!!!
But, kill me if I am aware what is that ment to be!!!

I have 3 objects, toy, book and dress.
If I put array[0] = toy, what do I get when I write:
,
cout<<array[0];?


P.s. As far as I understood structures ( I mean structures as structures, not structures as classes!) then I can get & of an object, but value on that & is just the value of its first element, isnt't it?

(Is the element=member treated in that sequence I put them one after another at alll???


MANY THANKS FOLKS!

OBJECTS
Object is an instance of class/structure. So by declaring product array[3] you're creating array of objects.
what do I get when I write:
cout<<array[0];?
Compilation error, unless you define operator<< for ostream and product class.
I can get & of an object, but value on that & is just the value of its first element, isnt't it?
(Is the element=member treated in that sequence I put them one after another at alll???
It depends. On if your class is POD or not, on inheritance, on your compiler and other things.

What you can do to make output operator work:
1
2
3
4
std::ostream& operator<<(std::ostream& lhs, const product& rhs)
{
    return lhs << rhs.getID() << '\n' << rhs.get_name() << '\n' << std::get_price() << '\n';
}
and then std::cout << array[i] will work
Last edited on
What is lhs? Seems still too complicate for me at the moment...
lhs is a parameter name just like in any function. lhs is short for left-hand-side, meaning that it is what appears tot the left from <<.

Read about operator overloading here: http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
Topic archived. No new replies allowed.