Function Operators

I have some questions about a homework assignment. If someone could help me answer these questions, but also with an explanation to help me understand, I would be very appreciative. I don't really understand what functions will be called. Thanks so much.

1. What line number will be executed if we enter the code: if (mystra ==mystrb) {y = 2;}
2. What line number will be executed if we enter the code: mystra =mystrb;
3. What line number will be executed if we enter the code: mystra[2] = 'K';
4. What line number will be executed if we enter the code: ch = mystra[1];


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

class Mystr
 {
public:
    Mystr(const char * s);
    ~Mystr();
    Mystr(const Mystr & m); 
    Mystr();
    Mystr(int len, char* a);
    int length() const;
    char & operator [] (int n); 
    operator const char *() const;    
    Mystr operator = (const Mystr &operand);
    Mystr operator + (const Mystr &operand);
    friend Mystr operator + (const char * str,const Mystr &op);
    void  operator += (const Mystr &operand);
    bool operator == (const Mystr &m) const;
    bool operator != (const Mystr &m) const;
};

int main()
{
	Mystr mystra("xyz"), mystrb("XYZ");
int y = 0;
char ch = ' ';
return(0);
}
just wondering are you sure this is everything that is part of the assignment. it seems to me that your missing some stuff.

edit: never mind.

line 23 defines mystra and mystrb
so in question 1 and 2 what function will be called.

the questions is referring to operator overloading
this might be of some help http://www.tutorialspoint.com/cplusplus/cpp_overloading.htm

the other question want to know the same.
Last edited on
closed account (D80DSL3A)
I think it means "which operator overload would be invoked by such code".
For example, the answer to #1 is line 17. The overload for operator== would be invoked.
your right i guess i was not clear at all. i tried to explain without giving him an answer ;}
closed account (D80DSL3A)
Sorry. I hadn't seen your edit. I saw only the part now struck through.
no problem i was spacing out hehe
I mean that is what I was thinking, but is it that obvious??

If #1 is line 17,
#2-4 would be line 13?

Thanks again
closed account (D80DSL3A)
For #2 it is line 13. operator=
For #3,4 though it's about the [], not the =
Thanks a lot to both of you. I will look into operator overloading because I don't quite understand =P.

So 3 and 4 would be line 11?
indeed. that's correct. in c++ you can define what the operators do for your classes.

here is a simple example:

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
#include <iostream>

class vector
{
public:
	int x;
	int y;

	vector() : x(0), y(0) {}
	vector(int X, int Y) : x(X), y(Y) {}

	bool operator==(vector b)  // try commenting out this function and see if the "if (a == b)" still works
	{
		if (x == b.x && y == b.y)
			return true;
		else
			return false;
	}
};

int main()
{
	vector a(1, 1);
	vector b(1, -1);

	if (a == b) // <--- this will fail if == is not implemented
		std::cout << "do awesome code \n";
	else
		std::cout << "don't do awesome code \n";

	std::cin.get();
}


now the link i gave you earlier will probably help you more but i hope this givers you a basic idea what its used for and how handy it is.

Edit:
I don't want to confuse you now. but if you feel you understand the first part continue reading. with operator overloading you don't need follow the c++ rules as in "==" does not need to be a comparator.

here is an example of "==" used for returning the bigger of the 2 vectors.
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
#include <iostream>

class vector
{
public:
	int x;
	int y;

	vector() : x(0), y(0) {}
	vector(int X, int Y) : x(X), y(Y) {}

	vector operator==(vector b)  
	{
		if (x >= b.x && y >= b.y) // this is a bad implementation for getting the bigger of the 2
			return *this;
		else
			return b;
	}
};

int main()
{
	vector a(1, 1);
	vector b(-2, 1);
	
	vector c = a == b;

	std::cout << " A's X: " << a.x << " Y: " << a.y << "\n";
	std::cout << " B's X: " << b.x << " Y: " << b.y << "\n";

	std::cout << " the bigger of the two's X: " << c.x << " & Y: " << c.y << "\n";

	std::cin.get();
}


i do want to note it is probably not the best idea to do this because it will confuse people using your code ;} but its handy to know you can do it.
Last edited on
roy ,

I really appreciate your explanation of operator assignment. I do understand the first block of code you wrote, but I do not understand the second block. I understand in the first segment, "==" is being changed to be able to compare vectors. I don't know what the second part is doing. Really great, nonetheless.
disturbedfuel15: i was afraid i was confusing you, i am so sorry. let me try to explain it different.

in c++ you have certain operators that you are pretty familiar with. you expect them to so certain things. well with operator overloading you can implement those operators. but what i tried to illustrate in my second example. it is possible do make them do other stuff, things you would not expect them to. like i did.

but for now you can forget my second example and look back on it when you know a little more c++.
it is possible do make them do other stuff, things you would not expect them to. like i did.


You are correct this is certainly possible, but it should absolutely be avoided.

The whole point of overloading operators is to make code more intuitive. IE: do something you'd expect it to.

If you overload an operator to do something other what what you'd expect... or if it's not immediately and plainly obvious to anyone what an operator overload should do... then you should not overload it in that fashion.
Topic archived. No new replies allowed.