| | |
overloading operator +
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 20
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jan 2008
Posts: 20
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; #ifndef MOVIE #define MOVIE class Movie { private: string director, title, genre, rating, release;//private part of class int length; public: Movie();//default constructor prototype defined outside of class Movie (const Movie& copy);//copy constructor prototype defined outside of class Movie(string d, string t, string g, int l, string r, string re);//standard constructor prototype;defined outside of class string getDirector();//accessors string getTitle(); string getGenre(); int getLength(); string getRating(); string getRelease(); void setDirector(string d);//mutators void setTitle(string t); void setGenre(string g); void setLength(int l); void setRating(string r); void setRelease(string re); bool compareDirector(Movie b);//compare functions type bool bool compareGenre(Movie c); void show();//converts length of movie from #of min to hours:min void critic();//reads in an object and makes a critique of that object void trailer (int a);//announces the length of the trailer associated with that object void addCast (string name);//adds a cast member to the cast array friend ostream& operator<<(ostream& os, const Movie&); friend istream& operator>>(istream& is, Movie&); void set(string d,string t,string g,int l,string r,string re); }; #endif;
C++ Syntax (Toggle Plain Text)
#include "Movie.h" Movie::Movie (const Movie& copy){//copy constructor director=copy.director;title=copy.title;genre=copy.genre;length=copy.length;rating=copy.rating;release=copy.release;} Movie::Movie(string d, string t, string g, int l, string r, string re){//constructor director=d;title=t;genre=g;length=l;rating=r;release=re;} Movie::Movie(){//default constructor director=="",title=="",genre=="",length=0,rating=="",release=="";} void Movie::setDirector(string d){//mutators director=d;} void Movie::setTitle(string t){ title=t;} void Movie::setGenre(string g){ genre=g;} void Movie::setLength(int l){ length=l;} void Movie::setRating(string r){ rating=r;} void Movie::setRelease(string re){ release=re;} string Movie::getDirector(){//accessors return director;} string Movie::getTitle(){ return title;} string Movie::getGenre(){ return genre;} int Movie::getLength(){ return length;} string Movie::getRating(){ return rating;} string Movie::getRelease(){ return release;} void Movie::set(string d,string t,string g,int l,string r,string re){ setDirector(d);setTitle(t);setGenre(g);setLength(l);setRating(r);setRelease(re);} ostream& operator<<(ostream& os, const Movie& m){ os<<m.director<<" "<<m.title<<" "<<m.genre<<" "<<m.length<<" "<<m.rating<<" "<<m.release<<endl; return os;} istream& operator>>(istream& is, Movie& m){ is>>m.director; is>>m.title; is>>m.genre; is>>m.length; is>>m.rating; is>>m.release; return is;}
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include "Movie.h" using namespace std; #ifndef COLLECT #define COLLECT class Collect{ private: Movie m[10]; int total; int time; string title; public: Collect (); void setTitle(string t); int getMovieTotal(); string getTitle(); int getTotalTime(); Collect operator+(const Collect& c)const; Collect operator-(const Collect& c)const; friend ostream& operator<<(ostream& os, const Collect&); friend istream& operator>>(istream& is, Collect&); }; #endif;
C++ Syntax (Toggle Plain Text)
#include "Movie.h" Collect::Collect():total(0), time(0), title(""){} int Collect::getMovieTotal(){ return total;} void Collect::setTitle(string t){ title=t;} string Collect::getTitle(){ return title;} int Collect::getTotalTime(){ if (total==0){ return 0;} for (int i=0; i<total; i++){ time=time+m[i].getLength();} return time;} ostream& operator<<(ostream& os, const Collect& c){ for(int i=0;i<c.total;i++){ os<<c.m[i];} return os; } Collect Collect::operator+(const Collect& c)const{ Collect add; add.m[total]=c.m; total++;}
C++ Syntax (Toggle Plain Text)
#include "Collect.h" using namespace std; int main() { Collect c1; c1.setTitle("BestofBest"); Movie m1, m2, m3("John Ford", "Stagecoach", "Western", 96, "G", "1939"); m1.set("David Lean", "Doctor Zhivago", "Historical", 200, "G", "1965" ); m2.setDirector("John Avildsen"); m2.setTitle("Rocky"); m2.setGenre("Sports"); m2.setLength(120); m2.setRating("G"); m2.setRelease("1976"); Movie m4(m2); cout << m4.getDirector() << " " << m4.getTitle() << " " << m4.getGenre() << " " << m4.getRating()<< endl; Movie m5; cin >> m5; cout << m1 << m2 << m3 << m4 << m5; c1 + m1; // adds a Movie to the Collection c1 cout << c1; c1 + m2; c1 + m3; c1 + m4; c1 + m5; cout << c1; return 0; }
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.
![]() |
Similar Threads
- overloading operator >> (C++)
- Overloading operator= (C++)
- overloading operator [] (C++)
- overloading Operator << to accept endl (C)
- need help with simple overloading operator+ for adding two object data. (C++)
Other Threads in the C++ Forum
- Previous Thread: need help with c++ problem
- Next Thread: How to access member of CoClass.
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





