Overloading operators

So I wrote some code so using std::cout I can print out an object of my own class. Like std::cout << myObject.

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

class Class
{
  private:

	unsigned int length;

  public:

        void setLength(unsigned int aVal) { length = aVal; }

	unsigned int getLength()
	{
		return length;
	}

	unsigned int operator+(const Class &rOperand)
	{
		return this->length + rOperand.length;
	}

	Class(unsigned int iVal) : length(iVal) { }
	Class() : length(0) { }
};

decltype(std::cout << "s") operator<<(const std::ostream &cout, Class &out)
{
    return std::cout << out.getLength();
}

int main()
{
	Class object(3), object2(4);
	Class object3(object + object2);

	std::cout << object3;

	return 0;
}

So then, could I overload operators so I can read input from std::cin to my own object?

Also, with templates and overloading operators you could pretty much use std::cout to print out any type right?

I'm learning about the object-oriented bits of C++ and other related bits. Messing around, haha.
Last edited on
So then, could I overload operators so I can read input from std::cin to my own object?
Sure: Overload the operator>>(std::istream &...)

Normally the operator<<(...) does not look like that on line 27.

See this:
1
2
3
4
5
std::ostream &operator<<(std::ostream &out, const Class &out)
{
    out << out.getLength(); // unsigned int getLength() const is required
    return out;
}
This way you can use it for all stream type (e.g. file) and not only for cout.

coder777 wrote:
// unsigned int getLength() const is required

Do you mean that getLength() should return const unsigned int?

coder777 wrote:
Normally the operator<<(...) does not look like that on line 27.

What do you mean?
Last edited on
Maybe like this
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
#include <iostream>

#include <sstream>

class Class
{
  private:

	unsigned int length;

  public:

    void setLength(unsigned int aVal) { length = aVal; }

	unsigned int getLength() const
	{
		return length;
	}

	Class operator+(const Class &rOperand) const
	{
		return Class(length + rOperand.length);
	}

	Class(unsigned int iVal) : length(iVal) { }
	Class() : length(0) { }
	
	friend std::istream& operator>>(std::istream& is, Class& c);
};

std::ostream& operator<<(std::ostream& os, const Class& c)
{
    return os << c.getLength();
}

std::istream& operator>>(std::istream& is, Class& c)
{
    return is >> c.length;
}

int main()
{
	Class object1(3), object2(4);
	Class object3 = object1 + object2;

	std::cout << object3;

    // output to stringstream, then display contents
    std::ostringstream ssout;
    ssout << "\n object1 = " << object1 << "\n";
    std::cout << ssout.str();
    
    // use stringstream for input
    std::istringstream ssin("345 71");
    Class a, b;
    ssin >> a >> b;
    
    // display 
    ssout.str("");
    ssout << " a = " << a;
    std::cout << " b = " << b << ssout.str() << '\n';	
}
Last edited on
@Chervil

What does putting const after the function parantheses do?

I also do not understand keyword friend since I didn't learn that yet.

What does ssout.str("") on line 59 do?

On line 22 what does
Class(length + rOperand.length) do?
Last edited on
Do you mean that getLength() should return const unsigned int?
No. See Chervils post.

What do you mean?
You definition of the operator is actually not like it is normall done.



What does putting const after the function parantheses do?
It tells the compiler that the function does not modify any member and thus can be used with a const object (such as const Class& c).
I also do not understand keyword friend since I didn't learn that yet.

A function declared a friend is not part of the class, it is a separate stand-alone function. Because it is a friend of the class, it is allowed access to the private members of the class. In this case, it is allowed to access and modify the Class.length. You could here instead use the setLength() function and then there would be no need for the operator>> function to be a friend.

What does ssout.str("") on line 59 do?

That's just a quick way of emptying the stringstream, by assigning an empty string.


On line 22 what does
Class(length + rOperand.length) do?

Here I tweaked your operator+ function quite a lot.
What you had was in fact ok, but I wanted the function to return another object of the same class, rather than an integer. So first I added the two lengths, then created a new instance of the class using this constructor Class(unsigned int iVal). If you don't do that, the compiler will supply an automatically-generated conversion for you. I wanted to make it explicit so it was clear what was happening.
Last edited on
What does ssout.str("") on line 59 do?

It clears the contents of ssout.


On line 22 what does
Class(length + rOperand.length) do?

It instantiates a temporary Class object initialised with length + rOperand.length.
Chervil wrote:
So first I added the two lengths, then created a new instance of the class using this constructor Class(unsigned int iVal).

I thought you couldn't explicitly call the constructer and only when creating a new object?
I thought you couldn't explicitly call the constructer and only when creating a new object?

Yes. That code does create a new object.

integralfx wrote:
It instantiates a temporary Class object initialised with length + rOperand.length.
Topic archived. No new replies allowed.