Thread: Operator=

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Operator=

    In this program my operator+ first creates a Meal with the default constructor. The default constructor makes exam.entree equal "Pizza" and exam.calorie equal 25. Then exam.calorie is set to the sum of calories.

    I need to define an operator= so the computer will simply copy all members from the source object to the destination object (total).

    However I can not figure out how to put this in my program. Can anyone help me create an assignment operator for Meal which only copies the calorie member and not the entree member?

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    
    class Meal
    {
    	friend ostream& operator<<(ostream &out, const Meal &aMeal);
    	friend istream& operator>>(istream &in, Meal &aMeal);
    private:
    	char *entree;
    	int calorie;
    public:
    	Meal(char *ent = "Pizza ", int cal = 25);
    	Meal operator+(Meal &aMeal);
    	void operator=(Meal &otherMeal);//is this right?
    	void displayMeal();
    };
    
    Meal::Meal(char *ent, int cal )
    {
    	strcpy(entree,ent);
    	calorie = cal;
    };
    
    
    Meal Meal::operator+(Meal &aMeal)
    {
    	Meal exam;
    	exam.calorie = calorie + aMeal.calorie;
    	return(exam);
    };
    
    void Meal::operator = (Meal &otherMeal)
    {
    	//if so, what here?
    };
    
    void Meal::displayMeal()
    {
    	cout<<"The entree: "<<entree<<"has"<<calorie<< " calories."<<endl;
    };
    
    ostream& operator<<(ostream &out, const Meal &aMeal)
    {
    	out<<aMeal.entree<<aMeal.calorie<<" calories "<<endl;
    	return(out);
    };
    
    istream& operator>>(istream &in, Meal &aMeal)
    {
    	cout<<endl; //clears
    	cout<<"Enter the entree name: ";
    	in>>aMeal.entree;
    	cout<<"Enter the amount of calories: ";
    	in>>aMeal.calorie;	
    	return(in);
    };
    
    void main()
    {
    	Meal breakfast("Bagel ", 100);
    	Meal lunch("Hamburger ", 325);
    	Meal dinner("Steak ", 350);
    	Meal total("Daily Total: ", 0);
    	total = breakfast + lunch + dinner;
    	cout<<"Breakfast: "<<breakfast<<endl;
    	cout<<"Lunch: "<<lunch<<endl;
    	cout<<"Dinner: "<<dinner<<endl;
    	cout<<total<<endl;
    	getch();
    };

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    You say you want to do two different things:
    I need to define an operator= so the computer will simply copy all members from the source object to the destination object (total).

    However I can not figure out how to put this in my program. Can anyone help me create an assignment operator for Meal which only copies the calorie member and not the entree member?
    Assignment operators should always return a reference to the object. Why? Because sometimes you'll have code like this:

    a=b=c

    so, the return value must be an l-value, and returning an object that is not a reference is not an l-value because it's a temporary copy. You're also going to be returning the object that the function is called on so your return statement will be:

    return *this;

    Code:
    void Meal operator = (Meal &otherMeal)
    {
    	//if so, what here?
    };
    You can define your function to do whatever you want. If you only want one data member of the object you called the function on to change, then only change that member:

    a_dataMember = otherMeal.a_dataMeber;
    Last edited by 7stud; 04-02-2003 at 09:57 PM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    Thank you both very much. I used the following code and everything displayed fine:

    Code:
    public:
        Meal(char ent[] = "Pizza ", int cal = 25);
        Meal operator+(Meal &aMeal);
    	//this is the part that was getting me, took me awhile to figure it out
    	//really have never used it before
         Meal( const Meal& toCopy){
             copy(toCopy);
         }
         const Meal& operator=(const Meal& rhs){
             copy(rhs);
             return *this;
         }
        void displayMeal();
    
    protected:
        void copy( const Meal& rhs){
            // do not copy entree
            calorie = rhs.calorie;
        }
    };

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Always assign to all members in an assignment. You leave an object in an inconsistent state if you dont. Why are you not copying the array?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LinkedList operator= and deep copy
    By sethjackson in forum C++ Programming
    Replies: 11
    Last Post: 02-28-2008, 12:54 PM
  2. SLList operator= overloading problems
    By JoeS in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 11:16 AM
  3. class template and operator=()
    By ichijoji in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2003, 11:42 PM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM