Operator overloading in classes

first Thanks to everyone here ,
I just want to know what the meaning and why we use this symbol '&' when we we need to compare 2 things in classes by using the overloading operators in classes, and also what the benefits of declaring the 'displayMydog' and not using it because instead I used the << ostream to ouput the last 3 lines .
thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  // main.cpp
#include "Dog.h"

int main ()
{
	Dog myDog("Spot",5.5,3);
	Dog yourDog("Jack",4.4,3);
	{
	if (myDog >= 2)
	
		cout <<"the dog is at least 2 years old." <<endl;
	else
		cout <<"the dog is less than 2 years old." <<endl;
	}

	{
		if (yourDog < myDog)
		cout <<"Your dog  weighs less than my dog."<<endl;
		else
        cout <<"Your dog does not weigh than my dog."<<endl;
	}
	{
		if (myDog == yourDog)
		cout <<"they have the same name."<<endl;
		else
        cout <<"they do not have the same name."<<endl;
	}
	return 0;
}

//Dog.h
#include<iostream>
#include<string>
using namespace std;

class Dog
{
private:
	string _name;
	float _weight;
	int  _age;
public:
	Dog(string name,float weight,int  age)
	{

		_name = name;
		_weight = weight;
		_age = age ;
	}
	//==================
	~Dog()
	{}
	//================
	void displayDog()
	{cout <<"NAME : "<<endl;
	 cout <<"WEIGHT : " <<endl;
	 cout <<"AGE : " <<endl;
	}
	bool operator >=( int num)
	{
        if( _age >= num)
			return true;
		else
			return false;
	} 
	//===========================
	bool operator < (Dog & dog)
	{ 
		if (_weight < dog._weight)
	     return true;
	else 
		return false;
	}
	//=============================
	bool operator ==(Dog & dog)
	{
		if (_name == dog._name)
			return true;
		else
			return false ;
	}
	//=======================================
	friend ostream & operator << (ostream & stream, Dog & dog)
	{
		stream<<"NAME : "<< dog._name<<endl <<"WEIGHT : "<<dog._weight <<endl<<"AGE : "<<dog._name<<endl;
		return stream;
	}
};
Dog & dog passes a reference to a Dog object. That is, it doesn't copy the object to be compared, it passes the location in memory to an already existing Dog object. Otherwise a temporary duplicate Dog object would be created, which doesn't need to happen. For the sake of argument, pretend your Dog class took up megabytes of memory because it stores a ton of information. We don't want to copy that around willy nilly, that would wreak havoc on our system. So you pass a reference to prevent from copying.

As for you displayDog vs. operator<< overload question.
It would make sense to have one or the other if you can guarantee that you will only ever want to use it in one way.
However, if you can't guarantee that, it would make more sense to have operator<< call displayDog to prevent code duplication. For example:
1
2
3
4
5
6
7
8
9
10
11
12
ostream &displayDog(ostream &stream)
{
    stream << "NAME: "   << _name   << endl
           << "WEIGHT: " << _weight << endl
           << "AGE: "    << _age    << endl;
    return stream;
}

friend ostream &operator<<(ostream &stream, Dog &dog)
{
    return dog.displayDog(stream);
}
Last edited on
thanks a lot ,
about the last point you explaind I agree with you , but in the homework page, the teacher want us to declar displayDog without any passed argument , so that;s why I get confused
Well that doesn't make sense. Do it the way your assignment calls for it, though, even though it seems counter intuitive.
Topic archived. No new replies allowed.