Displaying an array of classes

Hello!

Does anyone know how to go about displaying a class into an array? For example, I mad a class, and want to print it out as an array of maybe 10. I've looked online, and the examples are not very clear.

Thank you


It would be the same as creating an array of any other type then printing that.
1
2
3
foo arr[10] = {/* Fill in the contents here */};
for (int i = 0; i < 10; i++)
     cout << arr[i];


Have you defined output, constructors, and assignment for your class?
For example, this is the class that I have, and I want to display it using arrays 10 times (I know the class needs work, i'm not fully done with it yet). 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef DVD_H
#define DVD_H

#include <string>

class DVD
{
  
public:
   
   DVD();

   
   DVD(std::string title, 
       std::string genre,
       std::string description,
       std::string rentdate,
       std::string datereturn,
       std::string rating);
   ~DVD() {} 

public:
   void setTitle(std::string title)
   {
       itsTitle = title;
   }
   std::string getTitle() const
   {
       return itsTitle;
   }

   void setGenre(std::string genre)
   {
       itsGenre = genre;
   }
   std::string getGenre() const
   {
       return itsGenre;
   }

   void setDescription(std::string description)
   {
       itsDescription = description;
   }
   std::string GetDescription() const
   {
       return itsDescription;
   }

   void setRentDate(std::string checkout_date)
   {
       itsDate_rented = checkout_date;
   }
   std::string getRentDate() const
   {
       return itsDate_rented;
   }


   void setDateReturn(std::string checkin_date)
   {
       itsDate_returned = checkin_date;
   }
   std::string getRentReturn() const
   {
       return itsDate_returned;
   }

   void setRating(std::string rating)
   {
       itsRating = rating;
   }
   std::string getRating() const
   {
       return itsRating;
   }

   // setters needed so you can change something if needed
   // one example, you can hopefully figure out the rest :)

   // data members
private:
   std::string itsTitle;
   std::string itsGenre;
   std::string itsDescription;
   std::string itsDate_rented;
   std::string itsDate_returned;
   std::string itsRating;
};

#endif 
closed account (48T7M4Gy)
You need to create the array of objects first.

1
2
DVD library[] = 
{ {"itsTitle","itsGenre","itsDescription","itsDate_rented","itsDate_returned","itsRating}, ...repeat another 9 times  ... };  


You will have to dream up all the relevant data.

Once you've got that you can design a dedicated overloaded <<operator or
cout << library[0].getTitle() << ' ' << library[0].getGenre() etc etc

and loop through the library one at a time.
Last edited on
I showed you how to create an array of DVDs and how to create both a read and a print function in your previous thread.
http://www.cplusplus.com/forum/beginner/195459/#msg940210

Why are you asking the question again?
If you did not understand what I posted, then please indicate what you did not understand.

The only thing I did not show you in that post was how to print the array of DVDs.
1
2
3
4
5
6
  DVD library[10];  // Using kemort's name for the array

  //  Load the data to the array somehow
  //  Print each element of the array
  for (int i=0; i<10; i++)
    library[i].Print(cout);

Oh man, you did? Sorry! I didn't know, my mistake! I'll go check it out! My apologies man, I honestly had no clue
My apologies man, I honestly had no clue


What, from 4 days ago?
closed account (48T7M4Gy)
:(
Yea I didn't know he had posted back into the post. My mistake guys! Sorry!
closed account (48T7M4Gy)
LIBRIUM

I suggest you close this thread off with the solved green tick. Then only use the other thread.

Please don't open any other threads on any other assignment problems until that one is solved and green ticked. :)
Great, thanks Kemort! I'll do that!
Topic archived. No new replies allowed.