overloading operator +

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 20
Reputation: blcase is an unknown quantity at this point 
Solved Threads: 0
blcase blcase is offline Offline
Newbie Poster

overloading operator +

 
0
  #1
Feb 7th, 2008
I am trying to overload the + and - operator for my class Collect

class Collect is an array of movies (class Movie) so i need to add a movie to the array and subtract a movie from the array.

the only way i know how to do code this (and i know its wrong) is:
Collect Collect::operator+(const Collect& c)const{
Collect add;
add.director=director+c.director; \\string
add.title=title+c.title; \\string
add.genre=genre+c.genre; \\string
add.length=length+c.length; \\int
add.rating=rating+c.rating; \\string
add.release=release+c.release; \\string
return add;}
which should work if i was just adding ints or doubles....any help is greatly appreciated
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 425
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 49
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training

Re: overloading operator +

 
0
  #2
Feb 7th, 2008
Collect Collect::operator+(const Collect& c)

should this be const since you are changing the object;
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 425
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 49
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training

Re: overloading operator +

 
0
  #3
Feb 7th, 2008
Collect Collect::operator+(const Collect& c)

should be Collect Collect::operator+(const moive& m)

or some thing close to this since you are adding a movie to the object
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 462
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: overloading operator +

 
0
  #4
Feb 8th, 2008
dont seem to get it ... you have a class called collect.. the attributes if that class are initialized from some attributes of the movie class, rt? where is the array? can you put the code where you are using this operator?
Last edited by Agni; Feb 8th, 2008 at 12:04 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 20
Reputation: blcase is an unknown quantity at this point 
Solved Threads: 0
blcase blcase is offline Offline
Newbie Poster

Re: overloading operator +

 
0
  #5
Feb 8th, 2008
Originally Posted by chandra.rajat View Post
dont seem to get it ... you have a class called collect.. the attributes if that class are initialized from some attributes of the movie class, rt? where is the array? can you put the code where you are using this operator?
c1+m1;c1+m2;c1+m3; (etc)
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 462
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: overloading operator +

 
0
  #6
Feb 8th, 2008
when you add the next movie to the same collect object doesnt it override the existing values? how are you storing it? if possible plz post a bigger fragment of the code, its difficult to understand like this ...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 20
Reputation: blcase is an unknown quantity at this point 
Solved Threads: 0
blcase blcase is offline Offline
Newbie Poster

Re: overloading operator +

 
0
  #7
Feb 8th, 2008
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. #ifndef MOVIE
  6. #define MOVIE
  7.  
  8. class Movie {
  9. private:
  10. string director, title, genre, rating, release;//private part of class
  11. int length;
  12. public:
  13. Movie();//default constructor prototype defined outside of class
  14. Movie (const Movie& copy);//copy constructor prototype defined outside of class
  15. Movie(string d, string t, string g, int l, string r, string re);//standard constructor prototype;defined outside of class
  16. string getDirector();//accessors
  17. string getTitle();
  18. string getGenre();
  19. int getLength();
  20. string getRating();
  21. string getRelease();
  22. void setDirector(string d);//mutators
  23. void setTitle(string t);
  24. void setGenre(string g);
  25. void setLength(int l);
  26. void setRating(string r);
  27. void setRelease(string re);
  28. bool compareDirector(Movie b);//compare functions type bool
  29. bool compareGenre(Movie c);
  30. void show();//converts length of movie from #of min to hours:min
  31. void critic();//reads in an object and makes a critique of that object
  32. void trailer (int a);//announces the length of the trailer associated with that object
  33. void addCast (string name);//adds a cast member to the cast array
  34. friend ostream& operator<<(ostream& os, const Movie&);
  35. friend istream& operator>>(istream& is, Movie&);
  36. void set(string d,string t,string g,int l,string r,string re);
  37. };
  38. #endif;
  1. #include "Movie.h"
  2.  
  3. Movie::Movie (const Movie& copy){//copy constructor
  4. director=copy.director;title=copy.title;genre=copy.genre;length=copy.length;rating=copy.rating;release=copy.release;}
  5. Movie::Movie(string d, string t, string g, int l, string r, string re){//constructor
  6. director=d;title=t;genre=g;length=l;rating=r;release=re;}
  7. Movie::Movie(){//default constructor
  8. director=="",title=="",genre=="",length=0,rating=="",release=="";}
  9. void Movie::setDirector(string d){//mutators
  10. director=d;}
  11. void Movie::setTitle(string t){
  12. title=t;}
  13. void Movie::setGenre(string g){
  14. genre=g;}
  15. void Movie::setLength(int l){
  16. length=l;}
  17. void Movie::setRating(string r){
  18. rating=r;}
  19. void Movie::setRelease(string re){
  20. release=re;}
  21. string Movie::getDirector(){//accessors
  22. return director;}
  23. string Movie::getTitle(){
  24. return title;}
  25. string Movie::getGenre(){
  26. return genre;}
  27. int Movie::getLength(){
  28. return length;}
  29. string Movie::getRating(){
  30. return rating;}
  31. string Movie::getRelease(){
  32. return release;}
  33. void Movie::set(string d,string t,string g,int l,string r,string re){
  34. setDirector(d);setTitle(t);setGenre(g);setLength(l);setRating(r);setRelease(re);}
  35.  
  36. ostream& operator<<(ostream& os, const Movie& m){
  37. os<<m.director<<" "<<m.title<<" "<<m.genre<<" "<<m.length<<" "<<m.rating<<" "<<m.release<<endl;
  38. return os;}
  39. istream& operator>>(istream& is, Movie& m){
  40. is>>m.director;
  41. is>>m.title;
  42. is>>m.genre;
  43. is>>m.length;
  44. is>>m.rating;
  45. is>>m.release;
  46. return is;}
  1. #include <iostream>
  2. #include <string>
  3. #include "Movie.h"
  4. using namespace std;
  5. #ifndef COLLECT
  6. #define COLLECT
  7.  
  8. class Collect{
  9. private:
  10. Movie m[10];
  11. int total;
  12. int time;
  13. string title;
  14. public:
  15. Collect ();
  16. void setTitle(string t);
  17. int getMovieTotal();
  18. string getTitle();
  19. int getTotalTime();
  20. Collect operator+(const Collect& c)const;
  21. Collect operator-(const Collect& c)const;
  22. friend ostream& operator<<(ostream& os, const Collect&);
  23. friend istream& operator>>(istream& is, Collect&);
  24. };
  25. #endif;
  1. #include "Movie.h"
  2.  
  3. Collect::Collect():total(0), time(0), title(""){}
  4. int Collect::getMovieTotal(){
  5. return total;}
  6. void Collect::setTitle(string t){
  7. title=t;}
  8. string Collect::getTitle(){
  9. return title;}
  10. int Collect::getTotalTime(){
  11. if (total==0){
  12. return 0;}
  13. for (int i=0; i<total; i++){
  14. time=time+m[i].getLength();}
  15. return time;}
  16. ostream& operator<<(ostream& os, const Collect& c){
  17. for(int i=0;i<c.total;i++){
  18. os<<c.m[i];}
  19. return os;
  20. }
  21. Collect Collect::operator+(const Collect& c)const{
  22. Collect add;
  23. add.m[total]=c.m;
  24. total++;}
  1. #include "Collect.h"
  2. using namespace std;
  3.  
  4. int main() {
  5. Collect c1;
  6. c1.setTitle("BestofBest");
  7. Movie m1, m2, m3("John Ford", "Stagecoach", "Western", 96, "G", "1939");
  8. m1.set("David Lean", "Doctor Zhivago", "Historical", 200, "G", "1965" );
  9. m2.setDirector("John Avildsen");
  10. m2.setTitle("Rocky");
  11. m2.setGenre("Sports");
  12. m2.setLength(120);
  13. m2.setRating("G");
  14. m2.setRelease("1976");
  15. Movie m4(m2);
  16. cout << m4.getDirector() << " " << m4.getTitle() << " " << m4.getGenre() << " " << m4.getRating()<< endl;
  17. Movie m5;
  18. cin >> m5;
  19. cout << m1 << m2 << m3 << m4 << m5;
  20. c1 + m1; // adds a Movie to the Collection c1
  21. cout << c1;
  22. c1 + m2; c1 + m3; c1 + m4; c1 + m5;
  23. cout << c1;
  24. return 0;
  25. }

i know the guidelines are to shorten the code but i didn't know what was important and couldn't be cut out. sorry if this upsets you admins
Last edited by blcase; Feb 8th, 2008 at 1:30 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 20
Reputation: blcase is an unknown quantity at this point 
Solved Threads: 0
blcase blcase is offline Offline
Newbie Poster

Re: overloading operator +

 
0
  #8
Feb 8th, 2008
deleted
Last edited by blcase; Feb 8th, 2008 at 1:16 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 462
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: overloading operator +

 
0
  #9
Feb 8th, 2008
deleted what? ... you dont want to sol any more?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 20
Reputation: blcase is an unknown quantity at this point 
Solved Threads: 0
blcase blcase is offline Offline
Newbie Poster

Re: overloading operator +

 
0
  #10
Feb 8th, 2008
i didn't have the complete program loaded and i had it split between 2 posts....the full code is loaded in the post before the deleted post
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC