How to create an array of objects of another class?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

47 Replies - 1185 Views - Last Post: 08 July 2013 - 01:34 PM Rate Topic: -----

#1 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 213
  • Joined: 06-July 12

How to create an array of objects of another class?

Posted 06 July 2013 - 03:57 PM

Okay, so first of all everything works for this but now I am supposed to make another class named something like dvdLib and it is supposed to hold a collection of movies. Its principal private variable will be an array of dvd objects that can hold atleast 200 movies. Then I should have a menu that keeps looping around until the user wants to quit. The menu should consist of letting the user insert a movie, remove a movie, see all the movies that are currently there, search for a movie by title, see all the movies with a certain rating, see all the movies with a certain year, and see all the movies sorted alphabetically. Also I need to write the dvd library out to a file so it stores them and then next time the user runs the program the same list will still be there so they don't have to re-add what they did last time. I have an idea how to do this but got a ton of errors that I didn't know how to fix so can someone help me understand this?

//dvd.h File

#ifndef DVD_H
#define DVD_H
#include <iostream>
#include <string>
using namespace std;

class dvd {
   public:
        void setTitle(string t);
        void setYear(int y);
        void setRunningTime(int rt);
        void setGenre(string g);
        void setRating(string r);

        void input();
        void output() const;

        string getTitle() const{return title;}
        int getYear() const{return year;}
        int getRunningTime() const{return runTime;}
        string getGenre() const{return genre;}
        string getRating() const{return rating;}

   private:
        int year;
        int runTime;
        string title;
        string genre;
        string rating;
};

istream& operator >>(istream& fin, dvd& d);
ostream& operator <<(ostream& fout, const dvd& d);

#endif



//dvd.cc File

#include <iostream>
#include <string>
#include <fstream>
#include "dvd.h"
using namespace std;

void dvd::setTitle(string t){
     title = t;
}

void dvd::setYear(int y){
     year = y;
}

void dvd::setRunningTime(int rt){
     runTime = rt;
}

void dvd::setGenre(string g){
     genre = g;
}

void dvd::setRating(string r){
     rating = r;
}

void dvd::input(){
     char junk;

     cout << "Enter a movie title:  ";
     getline(cin, title);

     cout << "Enter a movie year:  ";
     cin >> year;

     cout << "Enter a movie length:  ";
     cin >> runTime;

     cout << "Enter a movie genre:  ";
     cin.get(junk);
     getline(cin, genre);

     cout << "Enter a movie rating:  ";
     getline(cin, rating);
}

void dvd::output() const{
     cout << title << " " << year << " "
          << runTime << " " << genre
          << " " << rating << endl;
}

istream& operator >>(istream& fin, dvd& d){
     d.input();
     return fin;
}

ostream& operator <<(ostream& fout, const dvd& d){
     d.output();
     return fout;
}



//main.cc File

#include <iostream>
#include "dvd.h"
using namespace std;

int main(){
    dvd movie;

    cin >> movie;
    cout << movie;

    return 0;
}


This post has been edited by jordannn15: 06 July 2013 - 03:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to create an array of objects of another class?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 04:04 PM

If you got errors post the complete error messages exactly as they appear in your development environment. These messages have important information to aid in locating and fixing your errrors.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 213
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 04:31 PM

No errors on this it works perfectly but I need some help starting on the second part which I wrote in the main post.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 05:02 PM

So what's wrong with creating a class DvdLibrary that contains a std::vector<Dvd> and be done with it? To save the data iterate over the vector to output to a file stream, and to load the data keep reading from a file stream and stuff dvd's into the vector.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 05:12 PM

Quote

No errors on this it works perfectly but I need some help starting on the second part which I wrote in the main post.

Quote

I have an idea how to do this but got a ton of errors that I didn't know how to fix so can someone help me understand this?


So which is it?

Jim
Was This Post Helpful? 0
  • +
  • -

#6 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 213
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 05:45 PM

I don't know how to use vectors and I'm supposed to use an array of objects of the class dvd that is able to hold 200 "movies" in my new class dvdLib and I don't know how to do that?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 05:53 PM

std::vector<> stores an array. If you call the constructor that takes an integer you can pre-initialize it's capacity to 200 elements, but vectors can store much more than that.

http://en.cppreferen...ontainer/vector
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 06:01 PM

I don't see the aversion to using std::vector<>. I see that you are using std::string in your original post.

Anyway, if you really want a C style array, you simply declare it.
class Wheel
{
    std::string brand;
    double radius;
};

class Car
{
    Wheel wheels[4];
};


Was This Post Helpful? 1
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 06:03 PM

View Postjordannn15, on 07 July 2013 - 01:45 AM, said:

I don't know how to use vectors and I'm supposed to use an array of objects of the class dvd that is able to hold 200 "movies" in my new class dvdLib and I don't know how to do that?


For movies read dvds. So create an array of 200 dvds.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 06:05 PM

Do you know how to create an array of objects, like for instance an array of int? Creating an array of any type is basically the same, even for your user defined type.

Show your code where you are at least trying to create a class that has an array of your dvd class.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 213
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 06:29 PM

Okay, so here is my attempt at this but I get a decent amount of errors.

//dvdLib.h File

#ifndef DVDLIB_H
#define DVDLIB_H
#include <iostream>
#include <cstdlib>
#include "dvd.h"
using namespace std;

class dvdLib {
      public:
             static const size_t CAPACITY = 200;

             void seeAll(ostream& out);
             void sortA();
             void sortY();
             void sortR();
             
             void search(string target);
             void insert(dvd& item);
             void remove(dvd& item);
             
             void load_from_file(istream& ins);
             char menu(char& choice);
             void save_to_file(ostream& outs);
        
      private:
             dvd movies[CAPACITY];
             size_t used;
};

#endif



//dvdLib.cc File

#include <iostream>
#include <cstdlib>
#include <fstream>
#include "dvd.h"
#include "dvdLib.h"
using namespace std;

void dvdLib::seeAll(ostream& out){
     for(unsigned int i = 0; i < used; i++)
        cout << movies[i] << endl;
}

void dvdLib::sortA(){
     dvdLib tmp;
     
     for(unsigned int i = 0; i < used; i++){
                  for(unsigned int j = 0; j < used - 1; j++){
                               if(movies[i].getTitle() > movies[j + 1].getTitle()){
                                                       tmp = movies[i].getTitle();
                                                       movies[i].getTitle() = movies[j + 1].getTitle();
                                                       movies[j + 1].getTitle() = tmp;
                               }
                  }
     }
     
     tmp.seeAll(cout);
}
 
void dvdLib::sortY(){
     dvdLib tmp;
     
     for(unsigned int i = 0; i < used; i++){
                  for(unsigned int j = 0; j < used - 1; j++){
                               if(movies[i].getYear() > movies[j + 1].getYear()){
                                                       tmp = movies[i].getYear();
                                                       movies[i].getYear() = movies[j + 1].getYear();
                                                       movies[j + 1].getYear() = tmp;
                               }
                  }
     }
     
     tmp.seeAll(cout);
}

void dvdLib::sortR(){
     dvdLib tmp;
     
     for(unsigned int i = 0; i < used; i++){
                  for(unsigned int j = 0; j < used - 1; j++){
                               if(movies[i].getRating() > movies[j + 1].getRating()){
                                                       tmp = movies[i].getRating();
                                                       movies[i].getRating() = movies[j + 1].getRating();
                                                       movies[j + 1].getRating() = tmp;
                               }
                  }
     }
     
     tmp.seeAll(cout);
}

void dvdLib::search(string target){
     size_t i = 0;
     
     while(i < used && movies[i].getTitle() != target) ++i;
     if(i == used)
        cout << "Not found.\n";
     else
        cout << movies[i] << endl;
}
                             
void dvdLib::insert(dvd& item){
     if(used < CAPACITY){
             movies[used] = item;
             used++;
     }
     else
        cout << "Error. List is full.\n";
}

void dvdLib::remove(dvd& item){
     movies[item] = movies[used - 1];
     used--;
}

void dvdLib::load_from_file(istream& ins){
     dvd tmp;
     ifstream ifs("movies.dat");
     
     while(ins >> tmp)
        insert(tmp);
}

char menu(char& choice){
     dvd item;
     string target;
     
     while(choice != '8'){
                  cout << "1. Insert a movie.\n";
                  cout << "2. Remove a movie.\n";
                  cout << "3. Display all movies.\n";
                  cout << "4. Search for a movie by title.\n";
                  cout << "5. Display all movies alphabetically.\n";
                  cout << "6. Display all movies by year.\n";
                  cout << "7. Display all movies by rating.\n";
                  cout << "8. Quit.\n\n";
                  cout << "Enter your choice ->   ";
                  cin >> choice;
                  
     if(choice == '1')
       insert(item);
     else if(choice == '2')
       remove(item);
     else if(choice == '3')
       seeAll(cout);
     else if(choice == '4')
       search(target);
     else if(choice == '5')
       sortA();
     else if(choice == '6')
       sortY();
     else if(choice == '7')
       sortR();
     else if(choice == '8')
       cout << "Goodbye.\n";
     else
       cout << "Invalid choice. Pick a choice (1 - 8)\n";
     }
}

void dvdLib::save_to_file(ostream& outs){
     for(unsigned int i = 0; i < used; i++)
        outs << movies[i] << endl;
}



//main.cc File

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "dvd.h"
#include "dvdLib.h"
using namespace std;

int main(){
    dvdLib movies;
    char choice;
    
    ifstream ifs("movies.dat");
    if(!ifs.fail())
      movies.load_from_file(ifs);
    ifs.close();
    
    movies.menu(choice);
    
    ofstream ofs("movies.dat");
    if(!ofs.fail())
      movies.save_to_file(ofs);
    ofs.close();

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 07:05 PM

And the errors?

Jim
Was This Post Helpful? 0
  • +
  • -

#13 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 213
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 07:13 PM

oh sorry errors are:

C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp In member function `void dvdLib::sortA()':

19 C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp no match for 'operator=' in 'tmp = dvd::getTitle() const()'

note C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.h:8 candidates are: dvdLib& dvdLib::operator=(const dvdLib&)

21 C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp no match for 'operator=' in 'dvd::getTitle() const() = tmp'

note C:\Dev-Cpp\include\c++\3.4.2\bits\basic_string.h:422 candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

note C:\Dev-Cpp\include\c++\3.4.2\bits\basic_string.h:422 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp In member function `void dvdLib::sortY()':

35 C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp no match for 'operator=' in 'tmp = ((+(i * 20u)) + ((dvd*)((dvd (*)[200])this)))->dvd::getYear()'

note C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.h:8 candidates are: dvdLib& dvdLib::operator=(const dvdLib&)

36 C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp non-lvalue in assignment

37 C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp non-lvalue in assignment

C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp In member function `void dvdLib::sortR()':

51 C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp no match for 'operator=' in 'tmp = dvd::getRating() const()'

note C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.h:8 candidates are: dvdLib& dvdLib::operator=(const dvdLib&)

53 C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp no match for 'operator=' in 'dvd::getRating() const() = tmp'

note C:\Dev-Cpp\include\c++\3.4.2\bits\basic_string.h:422 candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

note C:\Dev-Cpp\include\c++\3.4.2\bits\basic_string.h:422 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

note C:\Dev-Cpp\include\c++\3.4.2\bits\basic_string.h:422 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp In member function `void dvdLib::remove(dvd&)':

81 C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp no match for 'operator[]' in '((dvdLib*)this)->dvdLib::movies[item]'

C:\All Coding Stuff\C++ Coding\Projects\Makefile.win [Build Error] [Project1/dvdLib.o] Error 1

This post has been edited by jordannn15: 06 July 2013 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 07:15 PM

You could comment out some of the code, in dvdLib.cc, while debugging eg the sort routines.

When you are sorting you are not swapping the movie just trying to swap the title :

021	     tmp = movies[i].getTitle();
022	     movies[i].getTitle() = movies[j + 1].getTitle();
023	     movies[j + 1].getTitle() = tmp;
024	                               }




You could have helper functions eg swap which swaps the position of two dvds in the array, find which finds the position of a dvd in the array (by name and year perhaps).
Was This Post Helpful? 1
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 07:29 PM

You seem to be adding much too much code before you compile you masterpiece. You need to start with a compilable program then compile much more often, I compile as early as after adding one line of code. The fewer lines of code you add before compiling will make locating the problems much easier.

Jim
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »