Sign in to follow this  

Class Inheritance [C++]

This topic is 4109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Im learning about inheritance, but I have a problem. Im trying to use operator overloading with them, but when I try and print out the values, i get a really large negitive number. Please help! Note: I get no errors. Source
#include <iostream>

using namespace std;

class Enemy
{
public:
	int x;

	Enemy operator =( const Enemy& enemy ) {
		Enemy temp;

		temp.x = enemy.x;

		return temp;
	}
};

class Boss : public Enemy
{
	friend ostream& operator <<( ostream& os, const Boss& boss ) {
		os << boss.y;

		return os;
	}

public:
	int y;

	Boss operator +( const Boss& boss ) {
		Boss temp;

		temp.y = y + boss.y;

		return temp;
	}

	Boss operator =( const Boss& boss ) {
		Enemy::operator =( boss );

		Boss temp;

		temp.y = boss.y;

		return temp;
	}
};

int main()
{
	Boss boss1;
	Boss boss2;
	Boss boss3;

	boss1.x = 9;
	boss1.y = 1;

	boss2.x = 1;
	boss2.y = 2;

	boss3 = boss1;

	cout << "boss3 = " << boss3.y << "\n\n";

	return 0;
}

Share this post


Link to post
Share on other sites
Your assignment operators aren't implemented correctly. A simple assignment operator looks something like this:


Enemy& operator=(const Enemy& other)
{
this->x = other.x;
return *this;
}


That makes sure that the 'x' variable of the current object is set to whatever you make it equal to. When you use a temporary object, it just gets returned and never seen again! The current object isn't changed.

Share this post


Link to post
Share on other sites
For starters, you need to ask yourself, what exactly should operator=() do? What effect should it have on the object on which it's called? When you write:
int x = 1;
int y = 2;
x = y;
What happens to x in the third line?

Then, look at your =() operator, look at how it's being called in main, and think about what effect it will have. Notice, among other things, that your operator=() returns a value which is being ignored. In other words, not only does it not do what you want, it doesn't do anything at all in the given context.

Operator overloading is a bit tricky and can take some time to understand, but if you can bring some critical thinking to bear on it I think it will start to make more sense. It would be easy to offer examples or correct your code or whatever, but IMO you'll benefit more from looking at the code you've written methodically and carefully and figuring out for yourself why it isn't doing what you expect.

Share this post


Link to post
Share on other sites

This topic is 4109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this