Help with classes.

Hello guys, I have an issue with my classes, when I try to add and subtract it does not update the number of the fish I have. It runs fine until I start selling the goldfish. How would I go about it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Main.cpp

#include <iostream>
#include "FishPond.h"
using namespace std;
int main()
{
	FishPond p;
	short int BGF = p.Buy12GoldFish();
	int SGF = p.Sell10GoldFish();
	cout << "You Have " << p.getGoldFish() << " GoldFish." << endl;
	cout << "After Buying A Dozen Goldfish You Currently Own " << p.Buy12GoldFish() << " Goldfish." << endl;
	p.setGoldFish(BGF);
	cout << "After Buying A Dozen Goldfish You Currently Own " << p.Buy12GoldFish() << " Goldfish." << endl;
	cout << "After Selling 10 Goldfish You Currently Own " << p.Sell10GoldFish() << " Goldfish." << endl;
	return 0;
}


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
FishPond.cpp

#include <iostream>
#include "FishPond.h"
int FishPond::getGoldFish()
{
	return this->myGoldFish;
}
int FishPond::getCatFish()
{
	return this->myCatFish;
}
double FishPond:: getDepth()
{
	return this->myPondDepth;
}
int FishPond::setGoldFish(short int GF)
{
	myGoldFish = GF;
	return GF;
}
int FishPond::setCatFish(short int CF)
{
	myCatFish = CF;
	return CF;
}
double FishPond::setDepth(double D)
{
	return this->myPondDepth = D;
}
int FishPond::Buy12GoldFish()
{
	return myGoldFish + 12;
}
int FishPond::Sell10GoldFish()
{
	return myGoldFish - 10;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
FishPond.h

#ifndef FISHPOND
#define FISHPOND
class FishPond
{
private:
	short int myGoldFish = 13;
	short int myCatFish = 8;
	double myPondDepth = 7.25;
public:
	int getGoldFish();
	int getCatFish();
	double getDepth();
	int setGoldFish(short int GF);
	int setCatFish(short int CF);
	double setDepth(double D);
	int Buy12GoldFish();
	int Sell10GoldFish();
};
#endif
Last edited on
1
2
3
4
int FishPond::Buy12GoldFish()
{
	return myGoldFish + 12;
}


Shouldn't this function change the value of myGoldFish?
Yeah it's supposed to change the value by adding 12 to it
Hi,

The value returned by the function is different, but the value of myGoldFish remains unchanged. So what do you need to do to fix that?

Why are the set functions returning values? None of the set functions are being used anywhere.

The get functions should be marked const, they don't change the state of the class. Edit: the parameters to the functions should be const as well, we make a promise not to change their value inside the function.

There is no need for the this-> in your functions, member functions have direct access to member variables.

Could the type of myGoldFish and myCatFish be changed to unsigned short int?

Last edited on
Yeah it's supposed to change the value by adding 12 to it


Here is how to do that, with an example variable x.
x = x+12;

Compare with your code; spot the difference.
Thanks everyone! sorry aha i figured it out after @Moschops gave me a hint :P. on his first comment. aha
@TheIdeasMan aha yeah my code was not complete i just wanted to find out about the issue I had :D
Topic archived. No new replies allowed.