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

Collect Collect::operator+(const Collect& c)

should this be const since you are changing the object;

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

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?

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)

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 ...

#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;
#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;}
#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;
#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++;}
#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 :(

deleted

deleted what? ... you dont want to sol any more?

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

To me the usage of operator overloading to do this doesnt seem good.. you have a collect class and that has an array of movie class. simply write an addMovie function in collect class, pass on the movie object to it and insert it in the array. neat and much easy to understand. have you been speicifcally asked to do it using operator overloading?

commented: really took the time and tried to solve my problem....thanks! +1

To me the usage of operator overloading to do this doesnt seem good.. you have a collect class and that has an array of movie class. simply write an addMovie function in collect class, pass on the movie object to it and insert it in the array. neat and much easy to understand. have you been speicifcally asked to do it using operator overloading?

yes

its a progressive project it used to be as you said but now i have been asked to overload the operator and i feel as if i have searched everywhere and am just stuck

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

collect class doesnt seem to have these members at all(director,title etc..)

what you should, if you have no option but to use the operator overloading

void Collect::operator+(const Movie& c){

this.m[x] = c; 

;}

i dont think you need to return anything either in this case, simply add the movie to the array ... you will have to keep a tab of the number of elems in the array so that you add it at the right position..

one correction ..

this.m[x] = c;

should be

this->m[x] = c;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.